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.messages.Messages.getString;
19
20 import java.beans.BeanInfo;
21 import java.beans.Introspector;
22 import java.beans.PropertyDescriptor;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.mybatis.generator.api.IntrospectedColumn;
29 import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
30 import org.mybatis.generator.internal.ObjectFactory;
31
32
33
34
35
36
37 public class RootClassInfo {
38
39 private static Map<String, RootClassInfo> rootClassInfoMap;
40
41 static {
42 rootClassInfoMap = Collections
43 .synchronizedMap(new HashMap<String, RootClassInfo>());
44 }
45
46 public static RootClassInfo getInstance(String className,
47 List<String> warnings) {
48 RootClassInfo classInfo = rootClassInfoMap.get(className);
49 if (classInfo == null) {
50 classInfo = new RootClassInfo(className, warnings);
51 rootClassInfoMap.put(className, classInfo);
52 }
53
54 return classInfo;
55 }
56
57 private PropertyDescriptor[] propertyDescriptors;
58 private String className;
59 private List<String> warnings;
60 private boolean genericMode = false;
61
62 private RootClassInfo(String className, List<String> warnings) {
63 super();
64 this.className = className;
65 this.warnings = warnings;
66
67 if (className == null) {
68 return;
69 }
70
71 FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(className);
72 String nameWithoutGenerics = fqjt.getFullyQualifiedNameWithoutTypeParameters();
73 if (!nameWithoutGenerics.equals(className)) {
74 genericMode = true;
75 }
76
77 try {
78 Class<?> clazz = ObjectFactory.externalClassForName(nameWithoutGenerics);
79 BeanInfo bi = Introspector.getBeanInfo(clazz);
80 propertyDescriptors = bi.getPropertyDescriptors();
81 } catch (Exception e) {
82 propertyDescriptors = null;
83 warnings.add(getString("Warning.20", className));
84 }
85 }
86
87 public boolean containsProperty(IntrospectedColumn introspectedColumn) {
88 if (propertyDescriptors == null) {
89 return false;
90 }
91
92 boolean found = false;
93 String propertyName = introspectedColumn.getJavaProperty();
94 String propertyType = introspectedColumn.getFullyQualifiedJavaType()
95 .getFullyQualifiedName();
96
97
98
99 for (int i = 0; i < propertyDescriptors.length; i++) {
100 PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
101
102 if (propertyDescriptor.getName().equals(propertyName)) {
103
104
105
106 String introspectedPropertyType = propertyDescriptor.getPropertyType().getName();
107 if (genericMode && introspectedPropertyType.equals("java.lang.Object")) {
108
109 warnings.add(getString("Warning.28",
110 propertyName, className));
111 } else if (!introspectedPropertyType.equals(propertyType)) {
112 warnings.add(getString("Warning.21",
113 propertyName, className, propertyType));
114 break;
115 }
116
117
118 if (propertyDescriptor.getReadMethod() == null) {
119 warnings.add(getString("Warning.22",
120 propertyName, className));
121 break;
122 }
123
124
125 if (propertyDescriptor.getWriteMethod() == null) {
126 warnings.add(getString("Warning.23",
127 propertyName, className));
128 break;
129 }
130
131 found = true;
132 break;
133 }
134 }
135
136 return found;
137 }
138 }