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

mybatis / generator / 2202

05 May 2026 07:27PM UTC coverage: 91.746% (+0.04%) from 91.703%
2202

push

github

web-flow
Merge pull request #1508 from mybatis/renovate/com.github.javaparser-javaparser-core-3.x

Update dependency com.github.javaparser:javaparser-core to v3.28.1

2454 of 3154 branches covered (77.81%)

12038 of 13121 relevant lines covered (91.75%)

0.92 hits per line

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

81.82
/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.Collections;
19
import java.util.HashSet;
20
import java.util.List;
21
import java.util.Set;
22
import java.util.StringTokenizer;
23
import java.util.function.Consumer;
24
import java.util.function.Function;
25
import java.util.function.Supplier;
26
import java.util.function.UnaryOperator;
27

28
import org.jspecify.annotations.Nullable;
29

30
public class StringUtility {
31

32
    /**
33
     * Utility class. No instances allowed
34
     */
35
    private StringUtility() {
36
        super();
37
    }
38

39
    public static @Nullable String trimToNull(@Nullable String s) {
40
        if (s == null) {
1!
41
            return null;
×
42
        } else {
43
            var d = s.trim();
1✔
44
            return d.isEmpty() ? null : d;
1✔
45
        }
46
    }
47

48
    public static @Nullable Boolean parseNullableBoolean(@Nullable String s) {
49
        return s == null ? null : Boolean.valueOf(s);
1✔
50
    }
51

52
    public static boolean stringHasValue(@Nullable String s) {
53
        return s != null && !s.isEmpty();
1✔
54
    }
55

56
    public static String stringValueOrElse(@Nullable String s, String defaultValue) {
57
        if (stringHasValue(s)) {
1✔
58
            return s;
1✔
59
        } else {
60
            return defaultValue;
1✔
61
        }
62
    }
63

64
    public static <T extends Throwable> String stringValueOrElseThrow(@Nullable String s,
65
                                                                      Supplier<T> exceptionSupplier) throws T {
66
        if (stringHasValue(s)) {
1!
67
            return s;
1✔
68
        } else {
69
            throw exceptionSupplier.get();
×
70
        }
71
    }
72

73
    public static String stringValueOrElseGet(@Nullable String s, Supplier<String> supplier) {
74
        return mapStringValueOrElseGet(s, UnaryOperator.identity(), supplier);
1✔
75
    }
76

77
    public static <T> T mapStringValueOrElseGet(@Nullable String s, Function<String, T> mapper,
78
                                                Supplier<T> supplier) {
79
        if (stringHasValue(s)) {
1✔
80
            return mapper.apply(s);
1✔
81
        } else {
82
            return supplier.get();
1✔
83
        }
84
    }
85

86
    public static String mapStringValueOrElse(@Nullable String s, UnaryOperator<String> mapper, String defaultValue) {
87
        if (stringHasValue(s)) {
1!
88
            return mapper.apply(s);
1✔
89
        } else {
90
            return defaultValue;
×
91
        }
92
    }
93

94
    public static void ifStringHasValueElse(@Nullable String s, Consumer<String> consumer,
95
                                            Runnable runnable) {
96
        if (stringHasValue(s)) {
1✔
97
            consumer.accept(s);
1✔
98
        } else {
99
            runnable.run();
1✔
100
        }
101
    }
1✔
102

103
    public static String composeFullyQualifiedTableName(@Nullable String catalog,
104
            @Nullable String schema, String tableName, char separator) {
105
        StringBuilder sb = new StringBuilder();
1✔
106

107
        if (stringHasValue(catalog)) {
1✔
108
            sb.append(catalog);
1✔
109
            sb.append(separator);
1✔
110
        }
111

112
        if (stringHasValue(schema)) {
1✔
113
            sb.append(schema);
1✔
114
            sb.append(separator);
1✔
115
        } else {
116
            if (!sb.isEmpty()) {
1✔
117
                sb.append(separator);
1✔
118
            }
119
        }
120

121
        sb.append(tableName);
1✔
122

123
        return sb.toString();
1✔
124
    }
125

126
    public static boolean stringContainsSpace(@Nullable String s) {
127
        return s != null && s.indexOf(' ') != -1;
1✔
128
    }
129

130
    public static String escapeStringForJava(String s) {
131
        StringTokenizer st = new StringTokenizer(s, "\"", true); //$NON-NLS-1$
1✔
132
        StringBuilder sb = new StringBuilder();
1✔
133
        while (st.hasMoreTokens()) {
1✔
134
            String token = st.nextToken();
1✔
135
            if ("\"".equals(token)) { //$NON-NLS-1$
1✔
136
                sb.append("\\\""); //$NON-NLS-1$
1✔
137
            } else {
138
                sb.append(token);
1✔
139
            }
140
        }
1✔
141

142
        return sb.toString();
1✔
143
    }
144

145
    public static String escapeStringForKotlin(String s) {
146
        StringTokenizer st = new StringTokenizer(s, "\"$", true); //$NON-NLS-1$
1✔
147
        StringBuilder sb = new StringBuilder();
1✔
148
        while (st.hasMoreTokens()) {
1✔
149
            String token = st.nextToken();
1✔
150
            if ("\"".equals(token)) { //$NON-NLS-1$
1✔
151
                sb.append("\\\""); //$NON-NLS-1$
1✔
152
            } else if ("$".equals(token)) { //$NON-NLS-1$
1✔
153
                sb.append("\\$"); //$NON-NLS-1$
1✔
154
            } else {
155
                sb.append(token);
1✔
156
            }
157
        }
1✔
158

159
        return sb.toString();
1✔
160
    }
161

162
    public static boolean isTrue(@Nullable String s) {
163
        return Boolean.parseBoolean(s);
1✔
164
    }
165

166
    public static boolean stringContainsSQLWildcard(@Nullable String s) {
167
        if (s == null) {
×
168
            return false;
×
169
        }
170

171
        return s.indexOf('%') != -1 || s.indexOf('_') != -1;
×
172
    }
173

174
    /**
175
     * Given an input string, tokenize on the commas and trim all tokens. Returns an empty set if the input string is
176
     * null.
177
     *
178
     * @param in string to tokenize.
179
     *
180
     * @return Set of tokens
181
     */
182
    public static Set<String> tokenize(@Nullable String in) {
183
        return mapStringValueOrElseGet(in, str -> {
1✔
184
            Set<String> answer = new HashSet<>();
×
185
            StringTokenizer st = new StringTokenizer(str, ","); //$NON-NLS-1$
×
186
            while (st.hasMoreTokens()) {
×
187
                String s = st.nextToken().trim();
×
188
                if (!s.isEmpty()) {
×
189
                    answer.add(s);
×
190
                }
191
            }
×
192
            return answer;
×
193
        }, Collections::emptySet);
194
    }
195

196
    public static String convertCamelCaseToSnakeCase(String in) {
197
        if (in.chars().anyMatch(Character::isLowerCase)) {
1✔
198
            return in
1✔
199
                    .replaceAll("([A-Z])(?=[A-Z])", "$1_") //$NON-NLS-1$ //$NON-NLS-2$
1✔
200
                    .replaceAll("([a-z])([A-Z])", "$1_$2") //$NON-NLS-1$ //$NON-NLS-2$
1✔
201
                    .toUpperCase();
1✔
202
        } else {
203
            // if all upper case, then return the string as is
204
            return in;
1✔
205
        }
206
    }
207

208
    public static List<String> removeLastEmptyLine(List<String> lines) {
209
        if (lines.get(lines.size() - 1).isEmpty()) {
1✔
210
            return lines.subList(0, lines.size() - 1);
1✔
211
        } else {
212
            return lines;
1✔
213
        }
214
    }
215
}
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