21
21
22
22
/**
23
23
* The {@link ParsedJavaModelBuilder} builds a model using QDox as a Java parser
24
- *
24
+ *
25
25
* @author mbrunnli (03.06.2014)
26
26
*/
27
27
public class ParsedJavaModelBuilder {
@@ -38,23 +38,23 @@ public class ParsedJavaModelBuilder {
38
38
39
39
/**
40
40
* Creates the object model for the template instantiation.
41
- *
41
+ *
42
42
* @param javaClass
43
- * {@link Class} object of the pojo all information should be retrieved from
43
+ * {@link Class} object of the pojo all information should be retrieved from
44
44
* @return A {@link Map} of a {@link String} key to {@link Object} mapping keys as described before to the
45
45
* corresponding information. Learn more about the FreeMarker data model at http
46
46
* ://freemarker.sourceforge.net/docs/dgui_quickstart.html
47
47
* @author mbrunnli (06.02.2013)
48
48
*/
49
49
Map <String , Object > createModel (final JavaClass javaClass ) {
50
50
51
- if (cachedPojo != null && cachedPojo .equals (javaClass )) {
52
- return new HashMap <String , Object >( cachedModel );
51
+ if (this . cachedPojo != null && this . cachedPojo .equals (javaClass )) {
52
+ return new HashMap <>( this . cachedModel );
53
53
}
54
- cachedPojo = javaClass ;
54
+ this . cachedPojo = javaClass ;
55
55
56
- cachedModel = new HashMap <String , Object >();
57
- Map <String , Object > pojoModel = new HashMap <String , Object >();
56
+ this . cachedModel = new HashMap <>();
57
+ Map <String , Object > pojoModel = new HashMap <>();
58
58
pojoModel .put (ModelConstant .NAME , javaClass .getName ());
59
59
if (javaClass .getPackage () != null ) {
60
60
pojoModel .put (ModelConstant .PACKAGE , javaClass .getPackage ().getName ());
@@ -72,17 +72,23 @@ Map<String, Object> createModel(final JavaClass javaClass) {
72
72
determinePojoIds (javaClass , attributes );
73
73
collectAnnotations (javaClass , attributes );
74
74
75
+ Map <String , Object > superclass = extractSuperclass (javaClass );
76
+ pojoModel .put (ModelConstant .EXTENDED_TYPE , superclass );
77
+
78
+ List <Map <String , Object >> interfaces = extractInterfaces (javaClass );
79
+ pojoModel .put (ModelConstant .IMPLEMENTED_TYPES , interfaces );
80
+
75
81
pojoModel .put (ModelConstant .METHODS , extractMethods (javaClass ));
76
- cachedModel .put (ModelConstant .ROOT , pojoModel );
82
+ this . cachedModel .put (ModelConstant .ROOT , pojoModel );
77
83
78
- return new HashMap <String , Object >( cachedModel );
84
+ return new HashMap <>( this . cachedModel );
79
85
}
80
86
81
87
/**
82
88
* Extracts all methods and the method properties for the model
83
- *
89
+ *
84
90
* @param javaClass
85
- * input java class
91
+ * input java class
86
92
* @return a {@link List} of methods mapping each property to its value
87
93
* @author mbrunnli (04.06.2014)
88
94
*/
@@ -104,21 +110,21 @@ private List<Map<String, Object>> extractMethods(JavaClass javaClass) {
104
110
105
111
/**
106
112
* Extracts the attributes from the given POJO
107
- *
113
+ *
108
114
* @param pojo
109
- * {@link Class} object of the POJO the data should be retrieved from
110
- * @return a {@link Set} of attributes, where each attribute is represented by a {@link Map} of a {@link String} key
111
- * to the corresponding {@link String} value of meta information
115
+ * {@link Class} object of the POJO the data should be retrieved from
116
+ * @return a {@link Set} of attributes, where each attribute is represented by a {@link Map} of a
117
+ * {@link String} key to the corresponding {@link String} value of meta information
112
118
* @author mbrunnli (06.02.2013)
113
119
*/
114
120
private List <Map <String , Object >> extractAttributes (JavaClass pojo ) {
115
121
116
- List <Map <String , Object >> attributes = new LinkedList <Map < String , Object > >();
122
+ List <Map <String , Object >> attributes = new LinkedList <>();
117
123
for (JavaField f : pojo .getFields ()) {
118
124
if (f .isStatic ()) {
119
125
continue ;
120
126
}
121
- Map <String , Object > attrValues = new HashMap <String , Object >();
127
+ Map <String , Object > attrValues = new HashMap <>();
122
128
attrValues .put (ModelConstant .NAME , f .getName ());
123
129
attrValues .put (ModelConstant .TYPE , f .getType ().getGenericValue ());
124
130
attrValues .put (ModelConstant .CANONICAL_TYPE , f .getType ().getGenericCanonicalName ());
@@ -128,38 +134,90 @@ private List<Map<String, Object>> extractAttributes(JavaClass pojo) {
128
134
}
129
135
130
136
/**
131
- * Collect all annotations for the given pojo from setter and getter methods by searching using the attribute names.
132
- * Annotation information retrieved from the setter and getter methods will be added the the corresponding attribute
133
- * meta data
134
- *
137
+ * Extracts the superclass from the given POJO
138
+ *
139
+ * @param pojo
140
+ * {@link Class} object of the POJO the supertype should be retrieved from
141
+ * @return the supertype, represented by a {@link Map} of a {@link String} key to the corresponding
142
+ * {@link String} value of meta information
143
+ * @author fkreis (24.09.2014)
144
+ */
145
+ private Map <String , Object > extractSuperclass (JavaClass pojo ) {
146
+
147
+ Map <String , Object > superclassModel = new HashMap <>();
148
+
149
+ JavaClass superclass = pojo .getSuperJavaClass ();
150
+ superclassModel .put (ModelConstant .NAME , superclass .getName ());
151
+ superclassModel .put (ModelConstant .CANONICAL_NAME , superclass .getCanonicalName ());
152
+ if (superclass .getPackage () != null ) {
153
+ superclassModel .put (ModelConstant .PACKAGE , superclass .getPackage ().getName ());
154
+ } else {
155
+ superclassModel .put (ModelConstant .PACKAGE , "" );
156
+ }
157
+ return superclassModel ;
158
+ }
159
+
160
+ /**
161
+ * Extracts the implementedTypes (interfaces) from the given POJO
162
+ *
163
+ * @param pojo
164
+ * {@link Class} object of the POJO the interfaces should be retrieved from
165
+ * @return a {@link Set} of implementedTypes (interfaces), where each is represented by a {@link Map} of a
166
+ * {@link String} key to the corresponding {@link String} value of meta information
167
+ * @author fkreis (24.09.2014)
168
+ */
169
+ private List <Map <String , Object >> extractInterfaces (JavaClass pojo ) {
170
+
171
+ List <Map <String , Object >> interfaceList = new LinkedList <>();
172
+ for (JavaClass c : pojo .getImplementedInterfaces ()) {
173
+ Map <String , Object > interfaceModel = new HashMap <>();
174
+ interfaceModel .put (ModelConstant .NAME , c .getName ());
175
+ interfaceModel .put (ModelConstant .CANONICAL_NAME , c .getCanonicalName ());
176
+ if (c .getPackage () != null ) {
177
+ interfaceModel .put (ModelConstant .PACKAGE , c .getPackage ().getName ());
178
+ } else {
179
+ interfaceModel .put (ModelConstant .PACKAGE , "" );
180
+ }
181
+
182
+ interfaceList .add (interfaceModel );
183
+ }
184
+
185
+ return interfaceList ;
186
+ }
187
+
188
+ /**
189
+ * Collect all annotations for the given pojo from setter and getter methods by searching using the
190
+ * attribute names. Annotation information retrieved from the setter and getter methods will be added the
191
+ * the corresponding attribute meta data
192
+ *
135
193
* @param javaClass
136
- * class for which the setter and getter should be evaluated according to their annotations
194
+ * class for which the setter and getter should be evaluated according to their annotations
137
195
* @param attributes
138
- * list of attribute meta data for the generation (object model)
196
+ * list of attribute meta data for the generation (object model)
139
197
* @author mbrunnli (01.04.2014)
140
198
*/
141
199
private void collectAnnotations (JavaClass javaClass , List <Map <String , Object >> attributes ) {
142
200
143
201
for (Map <String , Object > attr : attributes ) {
144
- Map <String , Object > annotations = new HashMap <String , Object >();
202
+ Map <String , Object > annotations = new HashMap <>();
145
203
attr .put (ModelConstant .ANNOTATIONS , annotations );
146
204
147
205
JavaMethod getter =
148
- javaClass .getMethod ("get" + StringUtils .capitalize ((String ) attr .get (ModelConstant .NAME )), null ,
149
- false );
206
+ javaClass .getMethod ("get" + StringUtils .capitalize ((String ) attr .get (ModelConstant .NAME )),
207
+ null , false );
150
208
if (getter != null )
151
209
extractAnnotationsRecursively (annotations , getter .getAnnotations ());
152
210
153
211
getter =
154
- javaClass .getMethod ("is" + StringUtils .capitalize ((String ) attr .get (ModelConstant .NAME )), null ,
155
- false );
212
+ javaClass .getMethod ("is" + StringUtils .capitalize ((String ) attr .get (ModelConstant .NAME )),
213
+ null , false );
156
214
if (getter != null )
157
215
extractAnnotationsRecursively (annotations , getter .getAnnotations ());
158
216
159
217
// TODO bugfixing: setter has to have some parameters
160
218
JavaMethod setter =
161
- javaClass .getMethod ("set" + StringUtils .capitalize ((String ) attr .get (ModelConstant .NAME )), null ,
162
- false );
219
+ javaClass .getMethod ("set" + StringUtils .capitalize ((String ) attr .get (ModelConstant .NAME )),
220
+ null , false );
163
221
if (setter != null )
164
222
extractAnnotationsRecursively (annotations , setter .getAnnotations ());
165
223
}
@@ -168,26 +226,28 @@ private void collectAnnotations(JavaClass javaClass, List<Map<String, Object>> a
168
226
/**
169
227
* Extracts all information of the given annotations recursively and writes them into the object model
170
228
* (annotationsMap)
171
- *
229
+ *
172
230
* @param annotationsMap
173
- * object model for annotations
231
+ * object model for annotations
174
232
* @param annotations
175
- * to be analysed
233
+ * to be analysed
176
234
* @author mbrunnli (01.04.2014)
177
235
*/
178
- private void extractAnnotationsRecursively (Map <String , Object > annotationsMap , List <JavaAnnotation > annotations ) {
236
+ private void extractAnnotationsRecursively (Map <String , Object > annotationsMap ,
237
+ List <JavaAnnotation > annotations ) {
179
238
180
239
for (JavaAnnotation annotation : annotations ) {
181
- Map <String , Object > annotationParameters = new HashMap <String , Object >();
182
- annotationsMap .put (annotation .getType ().getCanonicalName ().replaceAll ("\\ ." , "_" ), annotationParameters );
240
+ Map <String , Object > annotationParameters = new HashMap <>();
241
+ annotationsMap .put (annotation .getType ().getCanonicalName ().replaceAll ("\\ ." , "_" ),
242
+ annotationParameters );
183
243
184
244
for (String propertyName : annotation .getPropertyMap ().keySet ()) {
185
245
Object value = annotation .getPropertyMap ().get (propertyName ).getParameterValue ();
186
246
if (value instanceof JavaAnnotation []) {
187
- Map <String , Object > annotationParameterParameters = new HashMap <String , Object >();
247
+ Map <String , Object > annotationParameterParameters = new HashMap <>();
188
248
annotationParameters .put (propertyName , annotationParameterParameters );
189
249
extractAnnotationsRecursively (annotationParameterParameters ,
190
- Arrays .asList ((JavaAnnotation []) value ));
250
+ Arrays .asList ((JavaAnnotation []) value ));
191
251
} else if (value instanceof Enum <?>[]) {
192
252
List <String > enumValues = Lists .newLinkedList ();
193
253
for (Enum <?> e : ((Enum <?>[]) value )) {
@@ -206,13 +266,13 @@ private void extractAnnotationsRecursively(Map<String, Object> annotationsMap, L
206
266
}
207
267
208
268
/**
209
- * Determines whether the given attributes behaving as IDs on the persistence layer. The information will be
210
- * integrated into the default model as stated in {@link #createModel(JavaClass)}
211
- *
269
+ * Determines whether the given attributes behaving as IDs on the persistence layer. The information will
270
+ * be integrated into the default model as stated in {@link #createModel(JavaClass)}
271
+ *
212
272
* @param javaClass
213
- * {@link Class} object of the POJO the data should be retrieved from
273
+ * {@link Class} object of the POJO the data should be retrieved from
214
274
* @param attributes
215
- * a {@link List} of all attributes and their properties
275
+ * a {@link List} of all attributes and their properties
216
276
* @author mbrunnli (12.02.2013)
217
277
*/
218
278
private void determinePojoIds (JavaClass javaClass , List <Map <String , Object >> attributes ) {
@@ -221,12 +281,12 @@ private void determinePojoIds(JavaClass javaClass, List<Map<String, Object>> att
221
281
JavaMethod getter = null ;
222
282
try {
223
283
getter =
224
- javaClass .getMethod ("get" + StringUtil .capFirst ((String ) attr .get (ModelConstant .NAME )), null ,
225
- false );
284
+ javaClass .getMethod ("get" + StringUtil .capFirst ((String ) attr .get (ModelConstant .NAME )),
285
+ null , false );
226
286
} catch (Exception e ) {
227
287
getter =
228
- javaClass .getMethod ("is" + StringUtil .capFirst ((String ) attr .get (ModelConstant .NAME )), null ,
229
- false );
288
+ javaClass .getMethod ("is" + StringUtil .capFirst ((String ) attr .get (ModelConstant .NAME )),
289
+ null , false );
230
290
}
231
291
if (getter == null )
232
292
return ;
0 commit comments