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

mybatis / generator / 2136

02 Apr 2026 07:17PM UTC coverage: 91.775% (+1.4%) from 90.382%
2136

Pull #1485

github

web-flow
Merge 516685a9a into 18f1f002d
Pull Request #1485: Code Cleanup and Coverage

2425 of 3124 branches covered (77.62%)

191 of 235 new or added lines in 54 files covered. (81.28%)

7 existing lines in 4 files now uncovered.

11884 of 12949 relevant lines covered (91.78%)

0.92 hits per line

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

87.88
/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.PluginAdapter;
25
import org.mybatis.generator.api.dom.java.Field;
26
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
27
import org.mybatis.generator.api.dom.java.JavaVisibility;
28
import org.mybatis.generator.api.dom.java.Method;
29
import org.mybatis.generator.api.dom.java.TopLevelClass;
30

31
public class ToStringPlugin extends PluginAdapter {
1✔
32

33
    private boolean useToStringFromRoot;
34

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

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

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

51
    @Override
52
    public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass,
53
                                                      IntrospectedTable introspectedTable) {
NEW
54
        return generateToString(introspectedTable, topLevelClass);
×
55
    }
56

57
    @Override
58
    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
NEW
59
        return generateToString(introspectedTable, topLevelClass);
×
60
    }
61

62
    private boolean generateToString(IntrospectedTable introspectedTable, TopLevelClass topLevelClass) {
63
        Method method = new Method("toString"); //$NON-NLS-1$
1✔
64
        method.setVisibility(JavaVisibility.PUBLIC);
1✔
65
        method.setReturnType(FullyQualifiedJavaType.getStringInstance());
1✔
66
        method.addAnnotation("@Override"); //$NON-NLS-1$
1✔
67

68
        commentGenerator.addGeneralMethodAnnotation(method, introspectedTable, topLevelClass.getImportedTypes());
1✔
69

70
        method.addBodyLine("StringBuilder sb = new StringBuilder();"); //$NON-NLS-1$
1✔
71
        method.addBodyLine("sb.append(getClass().getSimpleName());"); //$NON-NLS-1$
1✔
72
        method.addBodyLine("sb.append(\" [\");"); //$NON-NLS-1$
1✔
73
        method.addBodyLine("sb.append(\"Hash = \").append(hashCode());"); //$NON-NLS-1$
1✔
74
        StringBuilder sb = new StringBuilder();
1✔
75
        for (Field field : topLevelClass.getFields()) {
1✔
76
            String property = field.getName();
1✔
77
            sb.setLength(0);
1✔
78
            sb.append("sb.append(\"").append(", ").append(property) //$NON-NLS-1$ //$NON-NLS-2$
1✔
79
                    .append("=\")").append(".append(").append(property) //$NON-NLS-1$ //$NON-NLS-2$
1✔
80
                    .append(");"); //$NON-NLS-1$
1✔
81
            method.addBodyLine(sb.toString());
1✔
82
        }
1✔
83

84
        method.addBodyLine("sb.append(\"]\");"); //$NON-NLS-1$
1✔
85
        if (useToStringFromRoot && topLevelClass.getSuperClass().isPresent()) {
1!
86
            method.addBodyLine("sb.append(\", from super class \");"); //$NON-NLS-1$
×
87
            method.addBodyLine("sb.append(super.toString());"); //$NON-NLS-1$
×
88
        }
89
        method.addBodyLine("return sb.toString();"); //$NON-NLS-1$
1✔
90

91
        topLevelClass.addMethod(method);
1✔
92

93
        return true;
1✔
94
    }
95
}
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