Skip to content

Commit 6d9e09a

Browse files
committed
#35: Merge branch 'dev_javaplugin'
2 parents bde4f0e + 521c24b commit 6d9e09a

29 files changed

+1062
-506
lines changed

cobigen/cobigen-javaplugin/pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
<groupId>com.capgemini</groupId>
99
<artifactId>cobigen-javaplugin</artifactId>
1010
<name>CobiGen - Java Plug-in</name>
11-
<version>1.0.0</version>
11+
<version>1.1.0-SNAPSHOT</version>
1212
<packaging>jar</packaging>
13-
13+
1414
<parent>
1515
<groupId>com.capgemini</groupId>
1616
<artifactId>cobigen-parent</artifactId>
@@ -23,7 +23,7 @@
2323
<artifactId>cobigen-core</artifactId>
2424
<version>1.0.0</version>
2525
</dependency>
26-
26+
2727
<!-- QDox for Java parsing-->
2828
<dependency>
2929
<groupId>com.thoughtworks.qdox</groupId>

cobigen/cobigen-javaplugin/src/main/java/com/capgemini/cobigen/javaplugin/inputreader/FreeMarkerUtil.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
/**
99
* Util class for providing additional functionality for the FreeMarker model
10-
*
10+
*
1111
* @author mbrunnli (12.04.2013)
1212
*/
1313
public class FreeMarkerUtil {
@@ -40,7 +40,7 @@ public FreeMarkerUtil(ClassLoader inputClassLoader) {
4040
* @author mbrunnli (12.04.2013)
4141
*/
4242
public boolean isSubtypeOf(String subType, String superType) throws ClassNotFoundException {
43-
return classLoader.loadClass(superType).isAssignableFrom(classLoader.loadClass(subType));
43+
return this.classLoader.loadClass(superType).isAssignableFrom(this.classLoader.loadClass(subType));
4444
}
4545

4646
/**
@@ -50,10 +50,12 @@ public boolean isSubtypeOf(String subType, String superType) throws ClassNotFoun
5050
* @return <code>true</code> if the class is abstract<br>
5151
* <code>false</code> otherwise
5252
* @throws ClassNotFoundException
53+
* if the class with the given className could not be found in the FreeMarkerUtil's
54+
* classLoader
5355
* @author mbrunnli (12.04.2013)
5456
*/
5557
public boolean isAbstract(String className) throws ClassNotFoundException {
56-
return Modifier.isAbstract(classLoader.loadClass(className).getModifiers());
58+
return Modifier.isAbstract(this.classLoader.loadClass(className).getModifiers());
5759
}
5860

5961
}

cobigen/cobigen-javaplugin/src/main/java/com/capgemini/cobigen/javaplugin/inputreader/ModelConstant.java

+22-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/**
77
* String constants of the java object model for generation. Outcome of the {@link JavaInputReader}.
8-
*
8+
*
99
* @author <a href="[email protected]">Malte Brunnlieb</a>
1010
* @version $Revision$
1111
*/
@@ -41,28 +41,41 @@ public class ModelConstant {
4141
*/
4242
public static final String TYPE = "type";
4343

44+
/**
45+
* Type of the supertype of the input class ({@link Map}&lt;{@link String}, {@link Object}&gt;)
46+
*/
47+
public static final String EXTENDED_TYPE = "extendedType";
48+
49+
/**
50+
* A list of all implementedTypes (interfaces) of the input class, whereas one interface will be
51+
* represented by a set of mappings ( {@link List}&lt; {@link Map}&lt;{@link String}, {@link Object}
52+
* &gt;&gt;)
53+
*/
54+
public static final String IMPLEMENTED_TYPES = "implementedTypes";
55+
4456
/**
4557
* Full qualified type of a field of the input class ({@link String})
4658
*/
4759
public static final String CANONICAL_TYPE = "canonicalType";
4860

4961
/**
50-
* A list of all fields, whereas one field will be represented by a set of attribute mappings ({@link List}&lt;
51-
* {@link Map}&lt;{@link String}, {@link Object}&gt;&gt;)
62+
* A list of all fields, whereas one field will be represented by a set of attribute mappings (
63+
* {@link List}&lt; {@link Map}&lt;{@link String}, {@link Object}&gt;&gt;)
5264
*/
5365
public static final String FIELDS = "attributes";
5466

5567
/**
56-
* A list of all methods, whereas one method will be represented by a set of attribute mappings ({@link List}&lt;
57-
* {@link Map}&lt;{@link String}, {@link Object}&gt;&gt;)
68+
* A list of all methods, whereas one method will be represented by a set of attribute mappings (
69+
* {@link List}&lt; {@link Map}&lt;{@link String}, {@link Object}&gt;&gt;)
5870
*/
5971
public static final String METHODS = "methods";
6072

6173
/**
62-
* Annotations of methods or fields, which will be represented by a mapping of the full qualified type of an
63-
* annotation to its value. To gain template compatibility, the key will be stored with '_' instead of '.' in the
64-
* full qualified annotation type. Furthermore the annotation might be recursively defined and thus be accessed
65-
* using the same type of {@link #ANNOTATIONS} (Type: {@link Map}&lt;{@link String}, {@link Object}&gt;)
74+
* Annotations of methods or fields, which will be represented by a mapping of the full qualified type of
75+
* an annotation to its value. To gain template compatibility, the key will be stored with '_' instead of
76+
* '.' in the full qualified annotation type. Furthermore the annotation might be recursively defined and
77+
* thus be accessed using the same type of {@link #ANNOTATIONS} (Type: {@link Map}&lt;{@link String},
78+
* {@link Object}&gt;)
6679
*/
6780
public static final String ANNOTATIONS = "annotations";
6881

cobigen/cobigen-javaplugin/src/main/java/com/capgemini/cobigen/javaplugin/inputreader/ParsedJavaModelBuilder.java

+108-48
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* The {@link ParsedJavaModelBuilder} builds a model using QDox as a Java parser
24-
*
24+
*
2525
* @author mbrunnli (03.06.2014)
2626
*/
2727
public class ParsedJavaModelBuilder {
@@ -38,23 +38,23 @@ public class ParsedJavaModelBuilder {
3838

3939
/**
4040
* Creates the object model for the template instantiation.
41-
*
41+
*
4242
* @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
4444
* @return A {@link Map} of a {@link String} key to {@link Object} mapping keys as described before to the
4545
* corresponding information. Learn more about the FreeMarker data model at http
4646
* ://freemarker.sourceforge.net/docs/dgui_quickstart.html
4747
* @author mbrunnli (06.02.2013)
4848
*/
4949
Map<String, Object> createModel(final JavaClass javaClass) {
5050

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);
5353
}
54-
cachedPojo = javaClass;
54+
this.cachedPojo = javaClass;
5555

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<>();
5858
pojoModel.put(ModelConstant.NAME, javaClass.getName());
5959
if (javaClass.getPackage() != null) {
6060
pojoModel.put(ModelConstant.PACKAGE, javaClass.getPackage().getName());
@@ -72,17 +72,23 @@ Map<String, Object> createModel(final JavaClass javaClass) {
7272
determinePojoIds(javaClass, attributes);
7373
collectAnnotations(javaClass, attributes);
7474

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+
7581
pojoModel.put(ModelConstant.METHODS, extractMethods(javaClass));
76-
cachedModel.put(ModelConstant.ROOT, pojoModel);
82+
this.cachedModel.put(ModelConstant.ROOT, pojoModel);
7783

78-
return new HashMap<String, Object>(cachedModel);
84+
return new HashMap<>(this.cachedModel);
7985
}
8086

8187
/**
8288
* Extracts all methods and the method properties for the model
83-
*
89+
*
8490
* @param javaClass
85-
* input java class
91+
* input java class
8692
* @return a {@link List} of methods mapping each property to its value
8793
* @author mbrunnli (04.06.2014)
8894
*/
@@ -104,21 +110,21 @@ private List<Map<String, Object>> extractMethods(JavaClass javaClass) {
104110

105111
/**
106112
* Extracts the attributes from the given POJO
107-
*
113+
*
108114
* @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
112118
* @author mbrunnli (06.02.2013)
113119
*/
114120
private List<Map<String, Object>> extractAttributes(JavaClass pojo) {
115121

116-
List<Map<String, Object>> attributes = new LinkedList<Map<String, Object>>();
122+
List<Map<String, Object>> attributes = new LinkedList<>();
117123
for (JavaField f : pojo.getFields()) {
118124
if (f.isStatic()) {
119125
continue;
120126
}
121-
Map<String, Object> attrValues = new HashMap<String, Object>();
127+
Map<String, Object> attrValues = new HashMap<>();
122128
attrValues.put(ModelConstant.NAME, f.getName());
123129
attrValues.put(ModelConstant.TYPE, f.getType().getGenericValue());
124130
attrValues.put(ModelConstant.CANONICAL_TYPE, f.getType().getGenericCanonicalName());
@@ -128,38 +134,90 @@ private List<Map<String, Object>> extractAttributes(JavaClass pojo) {
128134
}
129135

130136
/**
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+
*
135193
* @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
137195
* @param attributes
138-
* list of attribute meta data for the generation (object model)
196+
* list of attribute meta data for the generation (object model)
139197
* @author mbrunnli (01.04.2014)
140198
*/
141199
private void collectAnnotations(JavaClass javaClass, List<Map<String, Object>> attributes) {
142200

143201
for (Map<String, Object> attr : attributes) {
144-
Map<String, Object> annotations = new HashMap<String, Object>();
202+
Map<String, Object> annotations = new HashMap<>();
145203
attr.put(ModelConstant.ANNOTATIONS, annotations);
146204

147205
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);
150208
if (getter != null)
151209
extractAnnotationsRecursively(annotations, getter.getAnnotations());
152210

153211
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);
156214
if (getter != null)
157215
extractAnnotationsRecursively(annotations, getter.getAnnotations());
158216

159217
// TODO bugfixing: setter has to have some parameters
160218
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);
163221
if (setter != null)
164222
extractAnnotationsRecursively(annotations, setter.getAnnotations());
165223
}
@@ -168,26 +226,28 @@ private void collectAnnotations(JavaClass javaClass, List<Map<String, Object>> a
168226
/**
169227
* Extracts all information of the given annotations recursively and writes them into the object model
170228
* (annotationsMap)
171-
*
229+
*
172230
* @param annotationsMap
173-
* object model for annotations
231+
* object model for annotations
174232
* @param annotations
175-
* to be analysed
233+
* to be analysed
176234
* @author mbrunnli (01.04.2014)
177235
*/
178-
private void extractAnnotationsRecursively(Map<String, Object> annotationsMap, List<JavaAnnotation> annotations) {
236+
private void extractAnnotationsRecursively(Map<String, Object> annotationsMap,
237+
List<JavaAnnotation> annotations) {
179238

180239
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);
183243

184244
for (String propertyName : annotation.getPropertyMap().keySet()) {
185245
Object value = annotation.getPropertyMap().get(propertyName).getParameterValue();
186246
if (value instanceof JavaAnnotation[]) {
187-
Map<String, Object> annotationParameterParameters = new HashMap<String, Object>();
247+
Map<String, Object> annotationParameterParameters = new HashMap<>();
188248
annotationParameters.put(propertyName, annotationParameterParameters);
189249
extractAnnotationsRecursively(annotationParameterParameters,
190-
Arrays.asList((JavaAnnotation[]) value));
250+
Arrays.asList((JavaAnnotation[]) value));
191251
} else if (value instanceof Enum<?>[]) {
192252
List<String> enumValues = Lists.newLinkedList();
193253
for (Enum<?> e : ((Enum<?>[]) value)) {
@@ -206,13 +266,13 @@ private void extractAnnotationsRecursively(Map<String, Object> annotationsMap, L
206266
}
207267

208268
/**
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+
*
212272
* @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
214274
* @param attributes
215-
* a {@link List} of all attributes and their properties
275+
* a {@link List} of all attributes and their properties
216276
* @author mbrunnli (12.02.2013)
217277
*/
218278
private void determinePojoIds(JavaClass javaClass, List<Map<String, Object>> attributes) {
@@ -221,12 +281,12 @@ private void determinePojoIds(JavaClass javaClass, List<Map<String, Object>> att
221281
JavaMethod getter = null;
222282
try {
223283
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);
226286
} catch (Exception e) {
227287
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);
230290
}
231291
if (getter == null)
232292
return;

0 commit comments

Comments
 (0)