1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 public abstract class AbstractJavaGenerator extends AbstractGenerator {
31 public abstract List<CompilationUnit> getCompilationUnits();
32
33 private final String project;
34
35 protected AbstractJavaGenerator(String project) {
36 this.project = project;
37 }
38
39 public String getProject() {
40 return project;
41 }
42
43 public static Method getGetter(Field field) {
44 Method method = new Method(getGetterMethodName(field.getName(), field.getType()));
45 method.setReturnType(field.getType());
46 method.setVisibility(JavaVisibility.PUBLIC);
47 String s = "return " + field.getName() + ';';
48
49 method.addBodyLine(s);
50 return method;
51 }
52
53 public String getRootClass() {
54 String rootClass = introspectedTable.getTableConfigurationProperty(PropertyRegistry.ANY_ROOT_CLASS);
55 if (rootClass == null) {
56 Properties properties = context.getJavaModelGeneratorConfiguration().getProperties();
57 rootClass = properties.getProperty(PropertyRegistry.ANY_ROOT_CLASS);
58 }
59
60 return rootClass;
61 }
62
63 protected void addDefaultConstructor(TopLevelClass topLevelClass) {
64 topLevelClass.addMethod(getDefaultConstructor(topLevelClass));
65 }
66
67 protected void addDefaultConstructorWithGeneratedAnnotatoin(TopLevelClass topLevelClass) {
68 topLevelClass.addMethod(getDefaultConstructorWithGeneratedAnnotation(topLevelClass));
69 }
70
71 private Method getDefaultConstructor(TopLevelClass topLevelClass) {
72 Method method = getBasicConstructor(topLevelClass);
73 addGeneratedJavaDoc(method);
74 return method;
75 }
76
77 private Method getDefaultConstructorWithGeneratedAnnotation(TopLevelClass topLevelClass) {
78 Method method = getBasicConstructor(topLevelClass);
79 addGeneratedAnnotation(method, topLevelClass);
80 return method;
81 }
82
83 private Method getBasicConstructor(TopLevelClass topLevelClass) {
84 Method method = new Method(topLevelClass.getType().getShortName());
85 method.setVisibility(JavaVisibility.PUBLIC);
86 method.setConstructor(true);
87 method.addBodyLine("super();");
88 return method;
89 }
90
91 private void addGeneratedJavaDoc(Method method) {
92 context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
93 }
94
95 private void addGeneratedAnnotation(Method method, TopLevelClass topLevelClass) {
96 context.getCommentGenerator().addGeneralMethodAnnotation(method, introspectedTable,
97 topLevelClass.getImportedTypes());
98 }
99 }