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

mybatis / generator / 1947

14 Jan 2026 02:31PM UTC coverage: 88.838% (+0.04%) from 88.799%
1947

push

github

web-flow
Merge pull request #1411 from mybatis/renovate/github-codeql-action-digest

Update github/codeql-action digest to cdefb33

2347 of 3184 branches covered (73.71%)

11517 of 12964 relevant lines covered (88.84%)

0.89 hits per line

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

97.66
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/internal/util/JavaBeansUtil.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 static org.mybatis.generator.internal.util.StringUtility.isTrue;
19

20
import java.util.Locale;
21
import java.util.Properties;
22

23
import org.jspecify.annotations.Nullable;
24
import org.mybatis.generator.api.CommentGenerator;
25
import org.mybatis.generator.api.IntrospectedColumn;
26
import org.mybatis.generator.api.IntrospectedTable;
27
import org.mybatis.generator.api.dom.java.CompilationUnit;
28
import org.mybatis.generator.api.dom.java.Field;
29
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
30
import org.mybatis.generator.api.dom.java.JavaVisibility;
31
import org.mybatis.generator.api.dom.java.Method;
32
import org.mybatis.generator.api.dom.java.Parameter;
33
import org.mybatis.generator.config.Context;
34
import org.mybatis.generator.config.PropertyRegistry;
35
import org.mybatis.generator.config.TableConfiguration;
36

37
public class JavaBeansUtil {
38

39
    private JavaBeansUtil() {
40
        super();
41
    }
42

43
    /**
44
     * Computes a getter method name.  Warning - does not check to see that the property is a valid
45
     * property.  Call getValidPropertyName first.
46
     *
47
     * @param property
48
     *            the property
49
     * @param fullyQualifiedJavaType
50
     *            the fully qualified java type
51
     * @return the getter method name
52
     */
53
    public static String getGetterMethodName(String property,
54
            FullyQualifiedJavaType fullyQualifiedJavaType) {
55
        StringBuilder sb = new StringBuilder();
1✔
56

57
        sb.append(property);
1✔
58
        if (Character.isLowerCase(sb.charAt(0))
1✔
59
                && (sb.length() == 1 || !Character.isUpperCase(sb.charAt(1)))) {
1✔
60
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
1✔
61
        }
62

63
        if (fullyQualifiedJavaType.equals(FullyQualifiedJavaType
1✔
64
                .getBooleanPrimitiveInstance())) {
1✔
65
            sb.insert(0, "is"); //$NON-NLS-1$
1✔
66
        } else {
67
            sb.insert(0, "get"); //$NON-NLS-1$
1✔
68
        }
69

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

73
    /**
74
     * Computes a setter method name.  Warning - does not check to see that the property is a valid
75
     * property.  Call getValidPropertyName first.
76
     *
77
     * @param property
78
     *            the property
79
     *
80
     * @return the setter method name
81
     */
82
    public static String getSetterMethodName(String property) {
83
        StringBuilder sb = new StringBuilder();
1✔
84

85
        sb.append(property);
1✔
86
        if (Character.isLowerCase(sb.charAt(0))
1✔
87
                && (sb.length() == 1 || !Character.isUpperCase(sb.charAt(1)))) {
1✔
88
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
1✔
89
        }
90

91
        sb.insert(0, "set"); //$NON-NLS-1$
1✔
92

93
        return sb.toString();
1✔
94
    }
95

96
    public static String getFirstCharacterUppercase(String inputString) {
97
        StringBuilder sb = new StringBuilder(inputString);
1✔
98
        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
1✔
99
        return sb.toString();
1✔
100
    }
101

102
    public static String getCamelCaseString(String inputString, boolean firstCharacterUppercase) {
103
        StringBuilder sb = new StringBuilder();
1✔
104

105
        boolean nextUpperCase = false;
1✔
106
        for (int i = 0; i < inputString.length(); i++) {
1✔
107
            char c = inputString.charAt(i);
1✔
108

109
            switch (c) {
1✔
110
            case '_':
111
            case '-':
112
            case '@':
113
            case '$':
114
            case '#':
115
            case ' ':
116
            case '/':
117
            case '&':
118
                if (!sb.isEmpty()) {
1✔
119
                    nextUpperCase = true;
1✔
120
                }
121
                break;
122

123
            default:
124
                if (nextUpperCase) {
1✔
125
                    sb.append(Character.toUpperCase(c));
1✔
126
                    nextUpperCase = false;
1✔
127
                } else {
128
                    sb.append(Character.toLowerCase(c));
1✔
129
                }
130
                break;
131
            }
132
        }
133

134
        if (firstCharacterUppercase) {
1✔
135
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
1✔
136
        }
137

138
        return sb.toString();
1✔
139
    }
140

141
    /**
142
     * This method ensures that the specified input string is a valid Java property name.
143
     *
144
     * <p>The rules are as follows:
145
     *
146
     * <ol>
147
     *   <li>If the first character is lower case, then OK</li>
148
     *   <li>If the first two characters are upper case, then OK</li>
149
     *   <li>If the first character is upper case, and the second character is lower case, then the first character
150
     *       should be made lower case</li>
151
     * </ol>
152
     *
153
     * <p>For example:
154
     *
155
     * <ul>
156
     *   <li>eMail &gt; eMail</li>
157
     *   <li>firstName &gt; firstName</li>
158
     *   <li>URL &gt; URL</li>
159
     *   <li>XAxis &gt; XAxis</li>
160
     *   <li>a &gt; a</li>
161
     *   <li>B &gt; b</li>
162
     *   <li>Yaxis &gt; yaxis</li>
163
     * </ul>
164
     *
165
     * @param inputString
166
     *            the input string
167
     * @return the valid property name
168
     */
169
    public static @Nullable String getValidPropertyName(@Nullable String inputString) {
170
        String answer;
171

172
        if (inputString == null) {
1!
173
            answer = null;
×
174
        } else if (inputString.length() < 2) {
1✔
175
            answer = inputString.toLowerCase(Locale.US);
1✔
176
        } else {
177
            if (Character.isUpperCase(inputString.charAt(0))
1✔
178
                    && !Character.isUpperCase(inputString.charAt(1))) {
1✔
179
                answer = inputString.substring(0, 1).toLowerCase(Locale.US)
1✔
180
                        + inputString.substring(1);
1✔
181
            } else {
182
                answer = inputString;
1✔
183
            }
184
        }
185

186
        return answer;
1✔
187
    }
188

189
    public static Method getJavaBeansGetter(IntrospectedColumn introspectedColumn, CommentGenerator commentGenerator,
190
                                            IntrospectedTable introspectedTable) {
191
        Method method = getBasicJavaBeansGetter(introspectedColumn);
1✔
192
        addGeneratedGetterJavaDoc(method, introspectedColumn, commentGenerator, introspectedTable);
1✔
193
        return method;
1✔
194
    }
195

196
    public static Method getJavaBeansGetterWithGeneratedAnnotation(IntrospectedColumn introspectedColumn,
197
                                                                   CommentGenerator commentGenerator,
198
                                                                   IntrospectedTable introspectedTable,
199
                                                                   CompilationUnit compilationUnit) {
200
        Method method = getBasicJavaBeansGetter(introspectedColumn);
1✔
201
        addGeneratedGetterAnnotation(method, introspectedColumn, commentGenerator, introspectedTable, compilationUnit);
1✔
202
        return method;
1✔
203
    }
204

205
    private static Method getBasicJavaBeansGetter(IntrospectedColumn introspectedColumn) {
206
        FullyQualifiedJavaType fqjt = introspectedColumn
1✔
207
                .getFullyQualifiedJavaType();
1✔
208
        String property = introspectedColumn.getJavaProperty();
1✔
209

210
        Method method = new Method(getGetterMethodName(property, fqjt));
1✔
211
        method.setVisibility(JavaVisibility.PUBLIC);
1✔
212
        method.setReturnType(fqjt);
1✔
213

214
        String s = "return " + property + ';'; //$NON-NLS-1$
1✔
215
        method.addBodyLine(s);
1✔
216

217
        return method;
1✔
218
    }
219

220
    private static void addGeneratedGetterJavaDoc(Method method, IntrospectedColumn introspectedColumn,
221
                                                  CommentGenerator commentGenerator,
222
                                                  IntrospectedTable introspectedTable) {
223
        commentGenerator.addGetterComment(method, introspectedTable, introspectedColumn);
1✔
224
    }
1✔
225

226
    private static void addGeneratedGetterAnnotation(Method method, IntrospectedColumn introspectedColumn,
227
                                                     CommentGenerator commentGenerator,
228
                                                     IntrospectedTable introspectedTable,
229
                                                     CompilationUnit compilationUnit) {
230
        commentGenerator.addGeneralMethodAnnotation(method, introspectedTable, introspectedColumn,
1✔
231
                compilationUnit.getImportedTypes());
1✔
232
    }
1✔
233

234
    public static Field getJavaBeansField(IntrospectedColumn introspectedColumn, CommentGenerator commentGenerator,
235
                                          IntrospectedTable introspectedTable) {
236
        Field field = getBasicJavaBeansField(introspectedColumn);
1✔
237
        addGeneratedJavaDoc(field, commentGenerator, introspectedColumn, introspectedTable);
1✔
238
        return field;
1✔
239
    }
240

241
    public static Field getJavaBeansFieldWithGeneratedAnnotation(IntrospectedColumn introspectedColumn,
242
                                                                 CommentGenerator commentGenerator,
243
                                                                 IntrospectedTable introspectedTable,
244
                                                                 CompilationUnit compilationUnit) {
245
        Field field = getBasicJavaBeansField(introspectedColumn);
1✔
246
        addGeneratedAnnotation(field, commentGenerator, introspectedColumn, introspectedTable, compilationUnit);
1✔
247
        return field;
1✔
248
    }
249

250
    private static Field getBasicJavaBeansField(IntrospectedColumn introspectedColumn) {
251
        FullyQualifiedJavaType fqjt = introspectedColumn
1✔
252
                .getFullyQualifiedJavaType();
1✔
253
        String property = introspectedColumn.getJavaProperty();
1✔
254

255
        Field field = new Field(property, fqjt);
1✔
256
        field.setVisibility(JavaVisibility.PRIVATE);
1✔
257

258
        return field;
1✔
259
    }
260

261
    private static void addGeneratedJavaDoc(Field field, CommentGenerator commentGenerator,
262
                                            IntrospectedColumn introspectedColumn,
263
                                            IntrospectedTable introspectedTable) {
264
        commentGenerator.addFieldComment(field, introspectedTable, introspectedColumn);
1✔
265
    }
1✔
266

267
    private static void addGeneratedAnnotation(Field field, CommentGenerator commentGenerator,
268
                                               IntrospectedColumn introspectedColumn,
269
                                               IntrospectedTable introspectedTable,
270
                                               CompilationUnit compilationUnit) {
271
        commentGenerator.addFieldAnnotation(field, introspectedTable, introspectedColumn,
1✔
272
                compilationUnit.getImportedTypes());
1✔
273
    }
1✔
274

275
    public static Method getJavaBeansSetter(IntrospectedColumn introspectedColumn,
276
                                            CommentGenerator commentGenerator, IntrospectedTable introspectedTable) {
277
        Method method = getBasicJavaBeansSetter(introspectedColumn);
1✔
278
        addGeneratedSetterJavaDoc(method, introspectedColumn, commentGenerator, introspectedTable);
1✔
279
        return method;
1✔
280
    }
281

282
    public static Method getJavaBeansSetterWithGeneratedAnnotation(IntrospectedColumn introspectedColumn,
283
                                                                   CommentGenerator commentGenerator,
284
                                                                   IntrospectedTable introspectedTable,
285
                                                                   CompilationUnit compilationUnit) {
286
        Method method = getBasicJavaBeansSetter(introspectedColumn);
1✔
287
        addGeneratedSetterAnnotation(method, introspectedColumn, commentGenerator, introspectedTable, compilationUnit);
1✔
288
        return method;
1✔
289
    }
290

291
    private static Method getBasicJavaBeansSetter(IntrospectedColumn introspectedColumn) {
292
        FullyQualifiedJavaType fqjt = introspectedColumn
1✔
293
                .getFullyQualifiedJavaType();
1✔
294
        String property = introspectedColumn.getJavaProperty();
1✔
295

296
        Method method = new Method(getSetterMethodName(property));
1✔
297
        method.setVisibility(JavaVisibility.PUBLIC);
1✔
298
        method.addParameter(new Parameter(fqjt, property));
1✔
299

300
        StringBuilder sb = new StringBuilder();
1✔
301
        if (introspectedColumn.isStringColumn() && isTrimStringsEnabled(introspectedColumn)) {
1✔
302
            sb.append("this."); //$NON-NLS-1$
1✔
303
            sb.append(property);
1✔
304
            sb.append(" = "); //$NON-NLS-1$
1✔
305
            sb.append(property);
1✔
306
            sb.append(" == null ? null : "); //$NON-NLS-1$
1✔
307
            sb.append(property);
1✔
308
            sb.append(".trim();"); //$NON-NLS-1$
1✔
309
            method.addBodyLine(sb.toString());
1✔
310
        } else {
311
            sb.append("this."); //$NON-NLS-1$
1✔
312
            sb.append(property);
1✔
313
            sb.append(" = "); //$NON-NLS-1$
1✔
314
            sb.append(property);
1✔
315
            sb.append(';');
1✔
316
            method.addBodyLine(sb.toString());
1✔
317
        }
318

319
        return method;
1✔
320
    }
321

322
    private static void addGeneratedSetterJavaDoc(Method method, IntrospectedColumn introspectedColumn,
323
                                                  CommentGenerator commentGenerator,
324
                                                  IntrospectedTable introspectedTable) {
325
        commentGenerator.addSetterComment(method, introspectedTable, introspectedColumn);
1✔
326
    }
1✔
327

328
    private static void addGeneratedSetterAnnotation(Method method, IntrospectedColumn introspectedColumn,
329
                                                     CommentGenerator commentGenerator,
330
                                                     IntrospectedTable introspectedTable,
331
                                                     CompilationUnit compilationUnit) {
332
        commentGenerator.addGeneralMethodAnnotation(method, introspectedTable, introspectedColumn,
1✔
333
                compilationUnit.getImportedTypes());
1✔
334
    }
1✔
335

336
    private static boolean isTrimStringsEnabled(Context context) {
337
        Properties properties = context
1✔
338
                .getJavaModelGeneratorConfiguration().getProperties();
1✔
339
        return isTrue(properties
1✔
340
                .getProperty(PropertyRegistry.MODEL_GENERATOR_TRIM_STRINGS));
1✔
341
    }
342

343
    private static boolean isTrimStringsEnabled(IntrospectedTable table) {
344
        TableConfiguration tableConfiguration = table.getTableConfiguration();
1✔
345
        String trimSpaces = tableConfiguration.getProperties().getProperty(
1✔
346
                PropertyRegistry.MODEL_GENERATOR_TRIM_STRINGS);
347
        if (trimSpaces != null) {
1!
348
            return isTrue(trimSpaces);
×
349
        }
350
        return isTrimStringsEnabled(table.getContext());
1✔
351
    }
352

353
    private static boolean isTrimStringsEnabled(IntrospectedColumn column) {
354
        String trimSpaces = column.getProperties().getProperty(PropertyRegistry.MODEL_GENERATOR_TRIM_STRINGS);
1✔
355
        if (trimSpaces != null) {
1!
356
            return isTrue(trimSpaces);
×
357
        }
358
        return isTrimStringsEnabled(column.getIntrospectedTable());
1✔
359
    }
360
}
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