• 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

81.58
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/plugins/ToStringPlugin.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.plugins;
17

18
import static org.mybatis.generator.internal.util.StringUtility.isTrue;
19

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

23
import org.mybatis.generator.api.IntrospectedTable;
24
import org.mybatis.generator.api.IntrospectedTable.TargetRuntime;
25
import org.mybatis.generator.api.PluginAdapter;
26
import org.mybatis.generator.api.dom.java.Field;
27
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
28
import org.mybatis.generator.api.dom.java.JavaVisibility;
29
import org.mybatis.generator.api.dom.java.Method;
30
import org.mybatis.generator.api.dom.java.TopLevelClass;
31

32
public class ToStringPlugin extends PluginAdapter {
1✔
33

34
    private boolean useToStringFromRoot;
35

36
    @Override
37
    public void setProperties(Properties properties) {
38
        super.setProperties(properties);
1✔
39
        useToStringFromRoot = isTrue(properties.getProperty("useToStringFromRoot")); //$NON-NLS-1$
1✔
40
    }
1✔
41

42
    @Override
43
    public boolean validate(List<String> warnings) {
44
        return true;
1✔
45
    }
46

47
    @Override
48
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
49
            IntrospectedTable introspectedTable) {
50
        generateToString(introspectedTable, topLevelClass);
1✔
51
        return true;
1✔
52
    }
53

54
    @Override
55
    public boolean modelRecordWithBLOBsClassGenerated(
56
            TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
57
        generateToString(introspectedTable, topLevelClass);
×
58
        return true;
×
59
    }
60

61
    @Override
62
    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
63
            IntrospectedTable introspectedTable) {
64
        generateToString(introspectedTable, topLevelClass);
×
65
        return true;
×
66
    }
67

68
    private void generateToString(IntrospectedTable introspectedTable,
69
            TopLevelClass topLevelClass) {
70
        Method method = new Method("toString"); //$NON-NLS-1$
1✔
71
        method.setVisibility(JavaVisibility.PUBLIC);
1✔
72
        method.setReturnType(FullyQualifiedJavaType.getStringInstance());
1✔
73
        method.addAnnotation("@Override"); //$NON-NLS-1$
1✔
74

75
        if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3_DSQL) {
1!
76
            commentGenerator.addGeneralMethodAnnotation(method, introspectedTable, topLevelClass.getImportedTypes());
×
77
        } else {
78
            commentGenerator.addGeneralMethodComment(method, introspectedTable);
1✔
79
        }
80

81
        method.addBodyLine("StringBuilder sb = new StringBuilder();"); //$NON-NLS-1$
1✔
82
        method.addBodyLine("sb.append(getClass().getSimpleName());"); //$NON-NLS-1$
1✔
83
        method.addBodyLine("sb.append(\" [\");"); //$NON-NLS-1$
1✔
84
        method.addBodyLine("sb.append(\"Hash = \").append(hashCode());"); //$NON-NLS-1$
1✔
85
        StringBuilder sb = new StringBuilder();
1✔
86
        for (Field field : topLevelClass.getFields()) {
1✔
87
            String property = field.getName();
1✔
88
            sb.setLength(0);
1✔
89
            sb.append("sb.append(\"").append(", ").append(property) //$NON-NLS-1$ //$NON-NLS-2$
1✔
90
                    .append("=\")").append(".append(").append(property) //$NON-NLS-1$ //$NON-NLS-2$
1✔
91
                    .append(");"); //$NON-NLS-1$
1✔
92
            method.addBodyLine(sb.toString());
1✔
93
        }
1✔
94

95
        method.addBodyLine("sb.append(\"]\");"); //$NON-NLS-1$
1✔
96
        if (useToStringFromRoot && topLevelClass.getSuperClass().isPresent()) {
1!
97
            method.addBodyLine("sb.append(\", from super class \");"); //$NON-NLS-1$
×
98
            method.addBodyLine("sb.append(super.toString());"); //$NON-NLS-1$
×
99
        }
100
        method.addBodyLine("return sb.toString();"); //$NON-NLS-1$
1✔
101

102
        topLevelClass.addMethod(method);
1✔
103
    }
1✔
104
}
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