View Javadoc
1   /**
2    *    Copyright 2006-2016 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    *       http://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 {
32  
33      private boolean useToStringFromRoot;
34  
35      @Override
36      public void setProperties(Properties properties) {
37          super.setProperties(properties);
38          useToStringFromRoot = isTrue(properties.getProperty("useToStringFromRoot"));
39      }
40  
41      public boolean validate(List<String> warnings) {
42          return true;
43      }
44  
45      @Override
46      public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
47              IntrospectedTable introspectedTable) {
48          generateToString(introspectedTable, topLevelClass);
49          return true;
50      }
51  
52      @Override
53      public boolean modelRecordWithBLOBsClassGenerated(
54              TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
55          generateToString(introspectedTable, topLevelClass);
56          return true;
57      }
58      
59      @Override
60      public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
61              IntrospectedTable introspectedTable) {
62          generateToString(introspectedTable, topLevelClass);
63          return true;
64      }
65  
66      private void generateToString(IntrospectedTable introspectedTable,
67              TopLevelClass topLevelClass) {
68          Method method = new Method();
69          method.setVisibility(JavaVisibility.PUBLIC);
70          method.setReturnType(FullyQualifiedJavaType.getStringInstance());
71          method.setName("toString"); //$NON-NLS-1$
72          if (introspectedTable.isJava5Targeted()) {
73              method.addAnnotation("@Override"); //$NON-NLS-1$
74          }
75  
76          context.getCommentGenerator().addGeneralMethodComment(method,
77                  introspectedTable);
78  
79          method.addBodyLine("StringBuilder sb = new StringBuilder();"); //$NON-NLS-1$
80          method.addBodyLine("sb.append(getClass().getSimpleName());"); //$NON-NLS-1$
81          method.addBodyLine("sb.append(\" [\");"); //$NON-NLS-1$
82          method.addBodyLine("sb.append(\"Hash = \").append(hashCode());"); //$NON-NLS-1$
83          StringBuilder sb = new StringBuilder();
84          for (Field field : topLevelClass.getFields()) {
85              String property = field.getName();
86              sb.setLength(0);
87              sb.append("sb.append(\"").append(", ").append(property) //$NON-NLS-1$ //$NON-NLS-2$
88                      .append("=\")").append(".append(").append(property) //$NON-NLS-1$ //$NON-NLS-2$
89                      .append(");"); //$NON-NLS-1$
90              method.addBodyLine(sb.toString());
91          }
92  
93          method.addBodyLine("sb.append(\"]\");"); //$NON-NLS-1$
94          if (useToStringFromRoot && topLevelClass.getSuperClass() != null) {
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$
99  
100         topLevelClass.addMethod(method);
101     }
102 }