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

mybatis / generator / 1942

12 Jan 2026 05:01PM UTC coverage: 88.75% (+0.4%) from 88.365%
1942

push

github

web-flow
Merge pull request #1412 from jeffgbutler/jspecify

Adopt JSpecify

2331 of 3162 branches covered (73.72%)

1800 of 1949 new or added lines in 202 files covered. (92.36%)

18 existing lines in 10 files now uncovered.

11384 of 12827 relevant lines covered (88.75%)

0.89 hits per line

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

77.19
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/internal/util/StringUtility.java
1
/*
2
 *    Copyright 2006-2026 the original author or authors.
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
 *       https://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 org.mybatis.generator.internal.util;
17

18
import java.util.HashSet;
19
import java.util.Set;
20
import java.util.StringTokenizer;
21

22
import org.jspecify.annotations.Nullable;
23

24
public class StringUtility {
25

26
    /**
27
     * Utility class. No instances allowed
28
     */
29
    private StringUtility() {
30
        super();
31
    }
32

33
    public static @Nullable String trimToNull(@Nullable String s) {
34
        if (s == null) {
1!
NEW
35
            return null;
×
36
        } else {
37
            var d = s.trim();
1✔
38
            return d.isEmpty() ? null : d;
1✔
39
        }
40
    }
41

42
    public static @Nullable Boolean parseNullableBoolean(@Nullable String s) {
43
        return s == null ? null : Boolean.valueOf(s);
1✔
44
    }
45

46
    public static boolean stringHasValue(@Nullable String s) {
47
        return s != null && !s.isEmpty();
1✔
48
    }
49

50
    public static String composeFullyQualifiedTableName(@Nullable String catalog,
51
            @Nullable String schema, String tableName, char separator) {
52
        StringBuilder sb = new StringBuilder();
1✔
53

54
        if (stringHasValue(catalog)) {
1✔
55
            sb.append(catalog);
1✔
56
            sb.append(separator);
1✔
57
        }
58

59
        if (stringHasValue(schema)) {
1✔
60
            sb.append(schema);
1✔
61
            sb.append(separator);
1✔
62
        } else {
63
            if (!sb.isEmpty()) {
1✔
64
                sb.append(separator);
1✔
65
            }
66
        }
67

68
        sb.append(tableName);
1✔
69

70
        return sb.toString();
1✔
71
    }
72

73
    public static boolean stringContainsSpace(@Nullable String s) {
74
        return s != null && s.indexOf(' ') != -1;
1✔
75
    }
76

77
    public static String escapeStringForJava(String s) {
78
        StringTokenizer st = new StringTokenizer(s, "\"", true); //$NON-NLS-1$
1✔
79
        StringBuilder sb = new StringBuilder();
1✔
80
        while (st.hasMoreTokens()) {
1✔
81
            String token = st.nextToken();
1✔
82
            if ("\"".equals(token)) { //$NON-NLS-1$
1✔
83
                sb.append("\\\""); //$NON-NLS-1$
1✔
84
            } else {
85
                sb.append(token);
1✔
86
            }
87
        }
1✔
88

89
        return sb.toString();
1✔
90
    }
91

92
    public static String escapeStringForKotlin(String s) {
93
        StringTokenizer st = new StringTokenizer(s, "\"$", true); //$NON-NLS-1$
1✔
94
        StringBuilder sb = new StringBuilder();
1✔
95
        while (st.hasMoreTokens()) {
1✔
96
            String token = st.nextToken();
1✔
97
            if ("\"".equals(token)) { //$NON-NLS-1$
1✔
98
                sb.append("\\\""); //$NON-NLS-1$
1✔
99
            } else if ("$".equals(token)) { //$NON-NLS-1$
1✔
100
                sb.append("\\$"); //$NON-NLS-1$
1✔
101
            } else {
102
                sb.append(token);
1✔
103
            }
104
        }
1✔
105

106
        return sb.toString();
1✔
107
    }
108

109
    public static boolean isTrue(@Nullable String s) {
110
        return "true".equalsIgnoreCase(s); //$NON-NLS-1$
1✔
111
    }
112

113
    public static boolean stringContainsSQLWildcard(@Nullable String s) {
114
        if (s == null) {
×
115
            return false;
×
116
        }
117

118
        return s.indexOf('%') != -1 || s.indexOf('_') != -1;
×
119
    }
120

121
    /**
122
     * Given an input string, tokenize on the commas and trim all token. Returns an empty set if the input string is
123
     * null.
124
     *
125
     * @param in
126
     *            strong to tokenize.
127
     *
128
     * @return Set of tokens
129
     */
130
    public static Set<String> tokenize(String in) {
131
        Set<String> answer = new HashSet<>();
×
132
        if (StringUtility.stringHasValue(in)) {
×
133
            StringTokenizer st = new StringTokenizer(in, ","); //$NON-NLS-1$
×
134
            while (st.hasMoreTokens()) {
×
135
                String s = st.nextToken().trim();
×
136
                if (!s.isEmpty()) {
×
137
                    answer.add(s);
×
138
                }
139
            }
×
140
        }
141
        return answer;
×
142
    }
143

144
    public static String convertCamelCaseToSnakeCase(String in) {
145
        if (in.chars().anyMatch(Character::isLowerCase)) {
1✔
146
            return in
1✔
147
                    .replaceAll("([A-Z])(?=[A-Z])", "$1_") //$NON-NLS-1$ //$NON-NLS-2$
1✔
148
                    .replaceAll("([a-z])([A-Z])", "$1_$2") //$NON-NLS-1$ //$NON-NLS-2$
1✔
149
                    .toUpperCase();
1✔
150
        } else {
151
            // if all upper case, then return the string as is
152
            return in;
1✔
153
        }
154
    }
155
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc