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

mybatis / generator / 2063

24 Feb 2026 06:21PM UTC coverage: 90.056% (-0.008%) from 90.064%
2063

Pull #1457

github

web-flow
Merge 80c9c482d into 601c7f314
Pull Request #1457: Refactoring - Code Generation Standardization

2360 of 3127 branches covered (75.47%)

928 of 945 new or added lines in 85 files covered. (98.2%)

11674 of 12963 relevant lines covered (90.06%)

0.9 hits per line

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

99.13
/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 java.util.Locale;
19

20
import org.mybatis.generator.api.CommentGenerator;
21
import org.mybatis.generator.api.IntrospectedColumn;
22
import org.mybatis.generator.api.IntrospectedTable;
23
import org.mybatis.generator.api.dom.java.CompilationUnit;
24
import org.mybatis.generator.api.dom.java.Field;
25
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
26
import org.mybatis.generator.api.dom.java.JavaVisibility;
27
import org.mybatis.generator.api.dom.java.Method;
28
import org.mybatis.generator.api.dom.java.Parameter;
29
import org.mybatis.generator.config.PropertyRegistry;
30

31
public class JavaBeansUtil {
32

33
    private JavaBeansUtil() {
34
        super();
35
    }
36

37
    /**
38
     * Calculates a getter method name. Java record "getters" have a different naming convention.
39
     *
40
     * @param introspectedColumn the column
41
     * @return the calculated getter method name (no parenthesis)
42
     */
43
    public static String getCallingGetterMethodName(IntrospectedColumn introspectedColumn) {
44
        if (introspectedColumn.getIntrospectedTable().isRecordBased()) {
1✔
45
            return introspectedColumn.getJavaProperty();
1✔
46
        } else {
47
            return getGetterMethodName(introspectedColumn.getJavaProperty(),
1✔
48
                    introspectedColumn.getFullyQualifiedJavaType());
1✔
49
        }
50
    }
51

52
    /**
53
     * Computes a getter method name.  Warning - does not check to see that the property is a valid
54
     * property.  Call getValidPropertyName first.
55
     *
56
     * @param property
57
     *            the property
58
     * @param fullyQualifiedJavaType
59
     *            the fully qualified java type
60
     * @return the getter method name
61
     */
62
    public static String getGetterMethodName(String property, FullyQualifiedJavaType fullyQualifiedJavaType) {
63
        StringBuilder sb = new StringBuilder();
1✔
64

65
        sb.append(property);
1✔
66
        if (Character.isLowerCase(sb.charAt(0))
1✔
67
                && (sb.length() == 1 || !Character.isUpperCase(sb.charAt(1)))) {
1✔
68
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
1✔
69
        }
70

71
        if (fullyQualifiedJavaType.equals(FullyQualifiedJavaType
1✔
72
                .getBooleanPrimitiveInstance())) {
1✔
73
            sb.insert(0, "is"); //$NON-NLS-1$
1✔
74
        } else {
75
            sb.insert(0, "get"); //$NON-NLS-1$
1✔
76
        }
77

78
        return sb.toString();
1✔
79
    }
80

81
    /**
82
     * Computes a setter method name.  Warning - does not check to see that the property is a valid
83
     * property.  Call getValidPropertyName first.
84
     *
85
     * @param property
86
     *            the property
87
     *
88
     * @return the setter method name
89
     */
90
    public static String getSetterMethodName(String property) {
91
        StringBuilder sb = new StringBuilder();
1✔
92

93
        sb.append(property);
1✔
94
        if (Character.isLowerCase(sb.charAt(0))
1✔
95
                && (sb.length() == 1 || !Character.isUpperCase(sb.charAt(1)))) {
1✔
96
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
1✔
97
        }
98

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

101
        return sb.toString();
1✔
102
    }
103

104
    public static String getFirstCharacterUppercase(String inputString) {
105
        StringBuilder sb = new StringBuilder(inputString);
1✔
106
        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
1✔
107
        return sb.toString();
1✔
108
    }
109

110
    public static String getCamelCaseString(String inputString, boolean firstCharacterUppercase) {
111
        StringBuilder sb = new StringBuilder();
1✔
112

113
        boolean nextUpperCase = false;
1✔
114
        for (int i = 0; i < inputString.length(); i++) {
1✔
115
            char c = inputString.charAt(i);
1✔
116

117
            switch (c) {
1✔
118
            case '_', '-', '@', '$', '#', ' ', '/', '&':
119
                if (!sb.isEmpty()) {
1✔
120
                    nextUpperCase = true;
1✔
121
                }
122
                break;
123

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

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

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

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

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

185
        return answer;
1✔
186
    }
187

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

195
    public static Method getJavaBeansGetterWithGeneratedAnnotation(IntrospectedColumn introspectedColumn,
196
                                                                   CommentGenerator commentGenerator,
197
                                                                   IntrospectedTable introspectedTable,
198
                                                                   CompilationUnit compilationUnit) {
199
        Method method = getBasicJavaBeansGetter(introspectedColumn);
1✔
200
        commentGenerator.addGeneralMethodAnnotation(method, introspectedTable, introspectedColumn,
1✔
201
                compilationUnit.getImportedTypes());
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
    public static Field getJavaBeansField(IntrospectedColumn introspectedColumn, CommentGenerator commentGenerator,
221
                                          IntrospectedTable introspectedTable) {
222
        Field field = getBasicJavaBeansField(introspectedColumn);
1✔
223
        commentGenerator.addFieldComment(field, introspectedTable, introspectedColumn);
1✔
224
        return field;
1✔
225
    }
226

227
    public static Field getJavaBeansFieldWithGeneratedAnnotation(IntrospectedColumn introspectedColumn,
228
                                                                 CommentGenerator commentGenerator,
229
                                                                 IntrospectedTable introspectedTable,
230
                                                                 CompilationUnit compilationUnit) {
231
        Field field = getBasicJavaBeansField(introspectedColumn);
1✔
232
        commentGenerator.addFieldAnnotation(field, introspectedTable, introspectedColumn,
1✔
233
                compilationUnit.getImportedTypes());
1✔
234
        return field;
1✔
235
    }
236

237
    private static Field getBasicJavaBeansField(IntrospectedColumn introspectedColumn) {
238
        FullyQualifiedJavaType fqjt = introspectedColumn
1✔
239
                .getFullyQualifiedJavaType();
1✔
240
        String property = introspectedColumn.getJavaProperty();
1✔
241

242
        Field field = new Field(property, fqjt);
1✔
243
        field.setVisibility(JavaVisibility.PRIVATE);
1✔
244

245
        return field;
1✔
246
    }
247

248
    public static Method getJavaBeansSetter(IntrospectedColumn introspectedColumn,
249
                                            CommentGenerator commentGenerator, IntrospectedTable introspectedTable) {
250
        Method method = getBasicJavaBeansSetter(introspectedColumn);
1✔
251
        commentGenerator.addSetterComment(method, introspectedTable, introspectedColumn);
1✔
252
        return method;
1✔
253
    }
254

255
    public static Method getJavaBeansSetterWithGeneratedAnnotation(IntrospectedColumn introspectedColumn,
256
                                                                   CommentGenerator commentGenerator,
257
                                                                   IntrospectedTable introspectedTable,
258
                                                                   CompilationUnit compilationUnit) {
259
        Method method = getBasicJavaBeansSetter(introspectedColumn);
1✔
260
        commentGenerator.addGeneralMethodAnnotation(method, introspectedTable, introspectedColumn,
1✔
261
                compilationUnit.getImportedTypes());
1✔
262
        return method;
1✔
263
    }
264

265
    public static String generateFieldSetterForConstructor(IntrospectedColumn introspectedColumn) {
266
        return "this." //$NON-NLS-1$
1✔
267
                + introspectedColumn.getJavaProperty()
1✔
268
                + " = " //$NON-NLS-1$
269
                + introspectedColumn.getJavaProperty()
1✔
270
                + ';';
271
    }
272

273
    private static Method getBasicJavaBeansSetter(IntrospectedColumn introspectedColumn) {
274
        FullyQualifiedJavaType fqjt = introspectedColumn
1✔
275
                .getFullyQualifiedJavaType();
1✔
276
        String property = introspectedColumn.getJavaProperty();
1✔
277

278
        Method method = new Method(getSetterMethodName(property));
1✔
279
        method.setVisibility(JavaVisibility.PUBLIC);
1✔
280
        method.addParameter(new Parameter(fqjt, property));
1✔
281

282
        StringBuilder sb = new StringBuilder();
1✔
283
        if (introspectedColumn.isStringColumn() && isTrimStringsEnabled(introspectedColumn)) {
1✔
284
            sb.append("this."); //$NON-NLS-1$
1✔
285
            sb.append(property);
1✔
286
            sb.append(" = "); //$NON-NLS-1$
1✔
287
            sb.append(property);
1✔
288
            sb.append(" == null ? null : "); //$NON-NLS-1$
1✔
289
            sb.append(property);
1✔
290
            sb.append(".trim();"); //$NON-NLS-1$
1✔
291
            method.addBodyLine(sb.toString());
1✔
292
        } else {
293
            sb.append("this."); //$NON-NLS-1$
1✔
294
            sb.append(property);
1✔
295
            sb.append(" = "); //$NON-NLS-1$
1✔
296
            sb.append(property);
1✔
297
            sb.append(';');
1✔
298
            method.addBodyLine(sb.toString());
1✔
299
        }
300

301
        return method;
1✔
302
    }
303

304
    private static boolean isTrimStringsEnabled(IntrospectedTable table) {
305
        return table.findTableOrModelGeneratorProperty(PropertyRegistry.MODEL_GENERATOR_TRIM_STRINGS)
1✔
306
                .map(Boolean::parseBoolean)
1✔
307
                .orElse(false);
1✔
308
    }
309

310
    private static boolean isTrimStringsEnabled(IntrospectedColumn column) {
311
        String trimSpaces = column.getProperties().getProperty(PropertyRegistry.MODEL_GENERATOR_TRIM_STRINGS);
1✔
312
        if (trimSpaces != null) {
1!
NEW
313
            return Boolean.parseBoolean(trimSpaces);
×
314
        }
315
        return isTrimStringsEnabled(column.getIntrospectedTable());
1✔
316
    }
317
}
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