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

mybatis / generator / 1646

21 Apr 2025 10:17PM UTC coverage: 88.157% (-0.2%) from 88.328%
1646

push

github

hazendaz
[ci] Run auto formatting

2518 of 3412 branches covered (73.8%)

994 of 1117 new or added lines in 164 files covered. (88.99%)

23 existing lines in 12 files now uncovered.

10578 of 11999 relevant lines covered (88.16%)

0.88 hits per line

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

79.49
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/plugins/ToStringPlugin.java
1
/*
2
 *    Copyright 2006-2025 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, IntrospectedTable introspectedTable) {
49
        generateToString(introspectedTable, topLevelClass);
1✔
50
        return true;
1✔
51
    }
52

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

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

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

72
        if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3_DSQL) {
1!
NEW
73
            context.getCommentGenerator().addGeneralMethodAnnotation(method, introspectedTable,
×
NEW
74
                    topLevelClass.getImportedTypes());
×
75
        } else {
76
            context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
1✔
77
        }
78

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

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

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

© 2025 Coveralls, Inc