View Javadoc
1   /**
2    *    Copyright 2006-2015 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.codegen;
17  
18  import static org.mybatis.generator.internal.util.JavaBeansUtil.getGetterMethodName;
19  
20  import java.util.List;
21  import java.util.Properties;
22  
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.JavaVisibility;
26  import org.mybatis.generator.api.dom.java.Method;
27  import org.mybatis.generator.api.dom.java.TopLevelClass;
28  import org.mybatis.generator.config.PropertyRegistry;
29  
30  /**
31   * 
32   * @author Jeff Butler
33   * 
34   */
35  public abstract class AbstractJavaGenerator extends AbstractGenerator {
36      public abstract List<CompilationUnit> getCompilationUnits();
37  
38      public static Method getGetter(Field field) {
39          Method method = new Method();
40          method.setName(getGetterMethodName(field.getName(), field
41                  .getType()));
42          method.setReturnType(field.getType());
43          method.setVisibility(JavaVisibility.PUBLIC);
44          StringBuilder sb = new StringBuilder();
45          sb.append("return "); //$NON-NLS-1$
46          sb.append(field.getName());
47          sb.append(';');
48          method.addBodyLine(sb.toString());
49          return method;
50      }
51  
52      public String getRootClass() {
53          String rootClass = introspectedTable
54                  .getTableConfigurationProperty(PropertyRegistry.ANY_ROOT_CLASS);
55          if (rootClass == null) {
56              Properties properties = context
57                      .getJavaModelGeneratorConfiguration().getProperties();
58              rootClass = properties.getProperty(PropertyRegistry.ANY_ROOT_CLASS);
59          }
60  
61          return rootClass;
62      }
63  
64      protected void addDefaultConstructor(TopLevelClass topLevelClass) {
65          Method method = new Method();
66          method.setVisibility(JavaVisibility.PUBLIC);
67          method.setConstructor(true);
68          method.setName(topLevelClass.getType().getShortName());
69          method.addBodyLine("super();"); //$NON-NLS-1$
70          context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
71          topLevelClass.addMethod(method);
72      }
73  }