• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

torand / openapi2java / 27619408421

16 Jun 2026 01:01PM UTC coverage: 84.049% (-0.4%) from 84.496%
27619408421

push

github

torand
fix: transform id or name into valid Java/Kotlin identifier

590 of 831 branches covered (71.0%)

Branch coverage included in aggregate %.

44 of 51 new or added lines in 3 files covered. (86.27%)

1739 of 1940 relevant lines covered (89.64%)

5.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.37
/src/main/java/io/github/torand/openapi2java/utils/IdentifierUtils.java
1
/*
2
 * Copyright (c) 2024-2026 Tore Eide Andersen
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package io.github.torand.openapi2java.utils;
17

18
import static io.github.torand.javacommons.contract.Requires.requireNonBlank;
19

20
/**
21
 * A collection of Java identifier related utilities.
22
 */
23
public class IdentifierUtils {
24
    private IdentifierUtils() {}
25

26
    /**
27
     * Transforms specified operation id, parameter name or property name into a valid Java identifier.
28
     * @param str the id or name to transform
29
     * @return a valid Java identifier
30
     */
31
    public static String toJavaIdentifier(String str) {
32
        requireNonBlank(str, "No string specified");
6✔
33

34
        StringBuilder sb = new StringBuilder(str.length());
6✔
35
        int i = 0;
2✔
36
        int cp;
37
        boolean capitalizeNext = false;
2✔
38

39
        // Skip leading characters that cannot start a Java identifier.
40
        while (i < str.length()) {
4!
41
            cp = str.codePointAt(i);
4✔
42
            i += Character.charCount(cp);
5✔
43
            if (Character.isWhitespace(cp)) {
3!
44
                // Leading whitespace: ignore, but remember to capitalize the
45
                // next valid character (only relevant if it's also a valid part
46
                // but not a valid start — in practice the first kept char stays
47
                // as-is here because we need a start char anyway).
NEW
48
                continue;
×
49
            }
50
            if (Character.isJavaIdentifierStart(cp)) {
3!
51
                cp = Character.toLowerCase(cp);
3✔
52
                sb.appendCodePoint(cp);
4✔
53
                break;
1✔
54
            }
55
        }
56

57
        // Keep only characters allowed inside a Java identifier;
58
        // upper-case the first valid char after any run of whitespace.
59
        while (i < str.length()) {
4✔
60
            cp = str.codePointAt(i);
4✔
61
            i += Character.charCount(cp);
5✔
62

63
            if (Character.isWhitespace(cp) || cp == '-') {
6!
64
                capitalizeNext = true;
2✔
65
                continue;
1✔
66
            }
67
            if (Character.isJavaIdentifierPart(cp)) {
3!
68
                if (capitalizeNext) {
2✔
69
                    cp = Character.toUpperCase(cp);
3✔
70
                    capitalizeNext = false;
2✔
71
                }
72
                sb.appendCodePoint(cp);
5✔
73
            }
74
        }
75

76
        return sb.toString();
3✔
77
    }
78
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc