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.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   * @author Jeff Butler
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)); //$NON-NLS-1$
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          // get method names from class and check against this column definition.
98          // better yet, have a map of method Names. check against it.
99          for (int i = 0; i < propertyDescriptors.length; i++) {
100             PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
101 
102             if (propertyDescriptor.getName().equals(propertyName)) {
103                 // property name is in the rootClass...
104 
105                 // Is it the proper type?
106                 String introspectedPropertyType = propertyDescriptor.getPropertyType().getName();
107                 if (genericMode && introspectedPropertyType.equals("java.lang.Object")) { //$NON-NLS-1$
108                     // OK - but add a warning
109                     warnings.add(getString("Warning.28", //$NON-NLS-1$
110                             propertyName, className));
111                 } else if (!introspectedPropertyType.equals(propertyType)) {
112                     warnings.add(getString("Warning.21", //$NON-NLS-1$
113                             propertyName, className, propertyType));
114                     break;
115                 }
116 
117                 // Does it have a getter?
118                 if (propertyDescriptor.getReadMethod() == null) {
119                     warnings.add(getString("Warning.22", //$NON-NLS-1$
120                             propertyName, className));
121                     break;
122                 }
123 
124                 // Does it have a setter?
125                 if (propertyDescriptor.getWriteMethod() == null) {
126                     warnings.add(getString("Warning.23", //$NON-NLS-1$
127                             propertyName, className));
128                     break;
129                 }
130 
131                 found = true;
132                 break;
133             }
134         }
135 
136         return found;
137     }
138 }