1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.devtools.generators.mo; |
17 | |
|
18 | |
import com.sun.codemodel.ClassType; |
19 | |
import com.sun.codemodel.JAnnotationArrayMember; |
20 | |
import com.sun.codemodel.JAnnotationUse; |
21 | |
import com.sun.codemodel.JBlock; |
22 | |
import com.sun.codemodel.JClass; |
23 | |
import com.sun.codemodel.JCodeModel; |
24 | |
import com.sun.codemodel.JConditional; |
25 | |
import com.sun.codemodel.JDefinedClass; |
26 | |
import com.sun.codemodel.JDocComment; |
27 | |
import com.sun.codemodel.JExpr; |
28 | |
import com.sun.codemodel.JFieldRef; |
29 | |
import com.sun.codemodel.JFieldVar; |
30 | |
import com.sun.codemodel.JInvocation; |
31 | |
import com.sun.codemodel.JMethod; |
32 | |
import com.sun.codemodel.JMod; |
33 | |
import com.sun.codemodel.JType; |
34 | |
import com.sun.codemodel.JVar; |
35 | |
import com.sun.codemodel.writer.SingleStreamCodeWriter; |
36 | |
import org.apache.commons.lang.builder.EqualsBuilder; |
37 | |
import org.apache.commons.lang.builder.HashCodeBuilder; |
38 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
39 | |
import org.kuali.rice.core.api.CoreConstants; |
40 | |
import org.kuali.rice.core.api.mo.AbstractDataTransferObject; |
41 | |
import org.kuali.rice.core.api.mo.ModelBuilder; |
42 | |
|
43 | |
import javax.xml.bind.annotation.XmlAccessType; |
44 | |
import javax.xml.bind.annotation.XmlAccessorType; |
45 | |
import javax.xml.bind.annotation.XmlAnyElement; |
46 | |
import javax.xml.bind.annotation.XmlElement; |
47 | |
import javax.xml.bind.annotation.XmlRootElement; |
48 | |
import javax.xml.bind.annotation.XmlType; |
49 | |
import java.io.ByteArrayOutputStream; |
50 | |
import java.io.Serializable; |
51 | |
import java.lang.reflect.Method; |
52 | |
import java.util.ArrayList; |
53 | |
import java.util.List; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | 0 | public class ImmutableJaxbGenerator { |
63 | |
|
64 | |
public static void main(String[] args) throws Exception { |
65 | |
|
66 | 0 | if (args.length > 2 || args.length < 1) { |
67 | 0 | System.err.println("There should be two arguments defined as follows:\n" + |
68 | |
" 1. Fully qualified class name of a 'contract' interface\n" + |
69 | |
" 2. [Optional] Fully qualified class name of the class to generate. If not specified, will use the name of the contract interface class and remove \"Contract\" from the end of it.\n"); |
70 | 0 | System.exit(1); |
71 | |
} |
72 | |
|
73 | |
|
74 | 0 | String contractInterfaceName = args[0]; |
75 | |
|
76 | 0 | String className = null; |
77 | |
|
78 | 0 | if (args.length == 2) { |
79 | 0 | className = args[1]; |
80 | |
} else { |
81 | 0 | if (!contractInterfaceName.endsWith("Contract")) { |
82 | 0 | throw new IllegalArgumentException("If not explicitly specifying target classname, then contract class name must end with 'Contract'"); |
83 | |
} |
84 | 0 | className = contractInterfaceName.substring(0, contractInterfaceName.lastIndexOf("Contract")); |
85 | |
} |
86 | |
|
87 | 0 | Generator generator = new Generator(contractInterfaceName, className); |
88 | 0 | generator.generate(); |
89 | 0 | } |
90 | |
|
91 | |
public static class Generator { |
92 | |
|
93 | |
private final String contractInterfaceName; |
94 | |
private final String className; |
95 | |
private final JCodeModel codeModel; |
96 | |
|
97 | 0 | public Generator(String contractInterfaceName, String className) { |
98 | 0 | this.contractInterfaceName = contractInterfaceName; |
99 | 0 | this.className = className; |
100 | 0 | this.codeModel = new JCodeModel(); |
101 | 0 | } |
102 | |
|
103 | |
public void generate() throws Exception { |
104 | 0 | byte[] javaCode = generateJava(); |
105 | 0 | System.out.println(new String(javaCode)); |
106 | 0 | } |
107 | |
|
108 | |
private byte[] generateJava() throws Exception { |
109 | |
|
110 | 0 | JDefinedClass classModel = codeModel._class(JMod.PUBLIC | JMod.FINAL, className, ClassType.CLASS); |
111 | 0 | Class<?> contractInterface = Class.forName(contractInterfaceName); |
112 | 0 | classModel._implements(contractInterface); |
113 | 0 | classModel._extends(AbstractDataTransferObject.class); |
114 | |
|
115 | 0 | List<FieldModel> fields = determineFields(contractInterface); |
116 | |
|
117 | 0 | renderConstantsClass(classModel); |
118 | 0 | renderElementsClass(classModel, fields); |
119 | 0 | renderClassLevelAnnotations(classModel, fields); |
120 | 0 | renderFields(classModel, fields); |
121 | 0 | renderFutureElementsField(classModel); |
122 | 0 | renderPrivateJaxbConstructor(classModel, fields); |
123 | 0 | renderBuilderConstructor(classModel, fields); |
124 | 0 | renderGetters(classModel, fields); |
125 | 0 | renderBuilderClass(classModel, fields, contractInterface); |
126 | |
|
127 | 0 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
128 | 0 | codeModel.build(new SingleStreamCodeWriter(outputStream)); |
129 | 0 | return outputStream.toByteArray(); |
130 | |
|
131 | |
} |
132 | |
|
133 | |
private List<FieldModel> determineFields(Class<?> contractInterface) throws Exception { |
134 | 0 | List<FieldModel> fieldModels = new ArrayList<FieldModel>(); |
135 | |
|
136 | 0 | Method[] methods = contractInterface.getMethods(); |
137 | 0 | for (Method method : methods) { |
138 | 0 | String methodName = method.getName(); |
139 | 0 | String fieldName = null; |
140 | 0 | if (method.getReturnType() != Void.class && method.getParameterTypes().length == 0) { |
141 | 0 | if (methodName.startsWith("get")) { |
142 | 0 | fieldName = Util.toLowerCaseFirstLetter(methodName.substring(3)); |
143 | 0 | } else if (methodName.startsWith("is")) { |
144 | 0 | fieldName = Util.toLowerCaseFirstLetter(methodName.substring(2)); |
145 | |
} else { |
146 | |
continue; |
147 | |
} |
148 | 0 | fieldModels.add(new FieldModel(fieldName, method.getReturnType())); |
149 | |
} |
150 | |
} |
151 | |
|
152 | 0 | return fieldModels; |
153 | |
} |
154 | |
|
155 | |
private void renderConstantsClass(JDefinedClass classModel) throws Exception { |
156 | |
|
157 | |
|
158 | 0 | JDefinedClass constantsClass = classModel._class(JMod.STATIC, Util.CONSTANTS_CLASS_NAME); |
159 | |
|
160 | |
|
161 | 0 | JDocComment javadoc = constantsClass.javadoc(); |
162 | 0 | javadoc.append(Util.CONSTANTS_CLASS_JAVADOC); |
163 | |
|
164 | |
|
165 | 0 | JFieldVar rootElementField = constantsClass.field(JMod.FINAL | JMod.STATIC, String.class, Util.ROOT_ELEMENT_NAME_FIELD); |
166 | 0 | rootElementField.init(JExpr.lit(Util.toLowerCaseFirstLetter(classModel.name()))); |
167 | |
|
168 | |
|
169 | 0 | JFieldVar typeNameField = constantsClass.field(JMod.FINAL | JMod.STATIC, String.class, Util.TYPE_NAME_FIELD); |
170 | 0 | typeNameField.init(JExpr.lit(classModel.name() + Util.TYPE_NAME_SUFFIX)); |
171 | |
|
172 | 0 | } |
173 | |
|
174 | |
private void renderElementsClass(JDefinedClass classModel, List<FieldModel> fields) throws Exception { |
175 | |
|
176 | |
|
177 | 0 | JDefinedClass elementsClass = classModel._class(JMod.STATIC, Util.ELEMENTS_CLASS_NAME); |
178 | |
|
179 | |
|
180 | 0 | JDocComment javadoc = elementsClass.javadoc(); |
181 | 0 | javadoc.append(Util.ELEMENTS_CLASS_JAVADOC); |
182 | |
|
183 | |
|
184 | 0 | for (FieldModel fieldModel : fields) { |
185 | 0 | if (Util.isCommonElement(fieldModel.fieldName)) { |
186 | 0 | continue; |
187 | |
} |
188 | 0 | JFieldVar elementFieldVar = elementsClass.field(JMod.FINAL | JMod.STATIC, String.class, Util.toConstantsVariable(fieldModel.fieldName)); |
189 | 0 | elementFieldVar.init(JExpr.lit(fieldModel.fieldName)); |
190 | 0 | } |
191 | 0 | } |
192 | |
|
193 | |
private void renderClassLevelAnnotations(JDefinedClass classModel, List<FieldModel> fields) throws Exception { |
194 | 0 | JFieldRef constantsClass = classModel.staticRef(Util.CONSTANTS_CLASS_NAME); |
195 | 0 | JFieldRef elementsClass = classModel.staticRef(Util.ELEMENTS_CLASS_NAME); |
196 | 0 | JClass coreConstants = codeModel.ref(CoreConstants.class); |
197 | 0 | JFieldRef commonElementsRef = coreConstants.staticRef("CommonElements"); |
198 | |
|
199 | |
|
200 | 0 | JAnnotationUse rootElementAnnotation = classModel.annotate(XmlRootElement.class); |
201 | 0 | rootElementAnnotation.param("name", constantsClass.ref(Util.ROOT_ELEMENT_NAME_FIELD)); |
202 | |
|
203 | |
|
204 | 0 | JAnnotationUse xmlAccessorTypeAnnotation = classModel.annotate(XmlAccessorType.class); |
205 | 0 | xmlAccessorTypeAnnotation.param("value", XmlAccessType.NONE); |
206 | |
|
207 | |
|
208 | 0 | JAnnotationUse xmlTypeAnnotation = classModel.annotate(XmlType.class); |
209 | 0 | xmlTypeAnnotation.param("name", constantsClass.ref(Util.TYPE_NAME_FIELD)); |
210 | 0 | JAnnotationArrayMember propOrderMember = xmlTypeAnnotation.paramArray("propOrder"); |
211 | 0 | for (FieldModel field : fields) { |
212 | 0 | if (Util.isCommonElement(field.fieldName)) { |
213 | 0 | propOrderMember.param(commonElementsRef.ref(Util.toConstantsVariable(field.fieldName))); |
214 | |
} else { |
215 | 0 | propOrderMember.param(elementsClass.ref(Util.toConstantsVariable(field.fieldName))); |
216 | |
} |
217 | |
} |
218 | 0 | propOrderMember.param(commonElementsRef.ref("FUTURE_ELEMENTS")); |
219 | 0 | } |
220 | |
|
221 | |
private void renderFields(JDefinedClass classModel, List<FieldModel> fields) { |
222 | 0 | for (FieldModel fieldModel : fields) { |
223 | 0 | renderField(classModel, fieldModel); |
224 | |
} |
225 | 0 | } |
226 | |
|
227 | |
private void renderField(JDefinedClass classModel, FieldModel fieldModel) { |
228 | 0 | JFieldVar field = classModel.field(JMod.PRIVATE | JMod.FINAL, fieldModel.fieldType, fieldModel.fieldName); |
229 | 0 | JAnnotationUse annotation = field.annotate(XmlElement.class); |
230 | 0 | if (Util.isCommonElement(fieldModel.fieldName)) { |
231 | 0 | JClass coreConstants = codeModel.ref(CoreConstants.class); |
232 | 0 | JFieldRef commonElementsRef = coreConstants.staticRef("CommonElements"); |
233 | 0 | annotation.param("name", commonElementsRef.ref(Util.toConstantsVariable(fieldModel.fieldName))); |
234 | 0 | } else { |
235 | 0 | JClass elementsClass = codeModel.ref(Util.ELEMENTS_CLASS_NAME); |
236 | 0 | JFieldRef fieldXmlNameRef = elementsClass.staticRef(Util.toConstantsVariable(fieldModel.fieldName)); |
237 | 0 | annotation.param("name", fieldXmlNameRef); |
238 | |
} |
239 | 0 | annotation.param("required", false); |
240 | 0 | } |
241 | |
|
242 | |
private void renderFutureElementsField(JDefinedClass classModel) throws Exception { |
243 | 0 | JType collectionType = codeModel.parseType("java.util.Collection<org.w3c.dom.Element>"); |
244 | 0 | JFieldVar field = classModel.field(JMod.PRIVATE | JMod.FINAL, collectionType, "_futureElements"); |
245 | 0 | field.init(JExpr._null()); |
246 | 0 | JAnnotationUse annotation = field.annotate(SuppressWarnings.class); |
247 | 0 | annotation.param("value", "unused"); |
248 | 0 | field.annotate(XmlAnyElement.class); |
249 | 0 | } |
250 | |
|
251 | |
private void renderPrivateJaxbConstructor(JDefinedClass classModel, List<FieldModel> fields) { |
252 | 0 | JMethod method = classModel.constructor(JMod.PRIVATE); |
253 | 0 | JBlock body = method.body(); |
254 | 0 | for (FieldModel fieldModel : fields) { |
255 | 0 | body.directStatement("this." + fieldModel.fieldName + " = " + getInitString(fieldModel.fieldType) + ";"); |
256 | |
} |
257 | 0 | method.javadoc().add("Private constructor used only by JAXB."); |
258 | 0 | } |
259 | |
|
260 | |
private String getInitString(Class<?> clazz) { |
261 | 0 | if (clazz == Boolean.TYPE) { |
262 | 0 | return "false"; |
263 | 0 | } else if (clazz == Character.TYPE) { |
264 | 0 | return "'\\\\u0000'"; |
265 | 0 | } else if (clazz == Long.TYPE) { |
266 | 0 | return "0L"; |
267 | 0 | } else if (clazz == Float.TYPE) { |
268 | 0 | return "0.0F"; |
269 | 0 | } else if (clazz == Double.TYPE) { |
270 | 0 | return "0.0D"; |
271 | 0 | } else if (clazz == Byte.TYPE || clazz == Short.TYPE || clazz == Integer.TYPE) { |
272 | 0 | return "0"; |
273 | |
} else { |
274 | 0 | return "null"; |
275 | |
} |
276 | |
} |
277 | |
|
278 | |
private void renderBuilderConstructor(JDefinedClass classModel, List<FieldModel> fields) { |
279 | 0 | JMethod method = classModel.constructor(JMod.PRIVATE); |
280 | 0 | method.param(codeModel.ref("Builder"), "builder"); |
281 | 0 | JBlock body = method.body(); |
282 | 0 | for (FieldModel fieldModel : fields) { |
283 | 0 | body.directStatement("this." + fieldModel.fieldName + " = builder." + Util.generateGetter(fieldModel.fieldName, isBoolean(fieldModel.fieldType)) + ";"); |
284 | |
} |
285 | 0 | } |
286 | |
|
287 | |
private void renderGetters(JDefinedClass classModel, List<FieldModel> fields) { |
288 | 0 | for (FieldModel fieldModel : fields) { |
289 | 0 | JMethod getterMethod = classModel.method(JMod.PUBLIC, fieldModel.fieldType, Util.generateGetterName(fieldModel.fieldName, isBoolean(fieldModel.fieldType))); |
290 | 0 | JBlock methodBody = getterMethod.body(); |
291 | 0 | methodBody.directStatement("return this." + fieldModel.fieldName + ";"); |
292 | 0 | getterMethod.annotate(Override.class); |
293 | 0 | } |
294 | 0 | } |
295 | |
|
296 | |
private boolean isBoolean(Class<?> clazz) { |
297 | 0 | return clazz == Boolean.TYPE || clazz == Boolean.class; |
298 | |
} |
299 | |
|
300 | |
private void renderBuilderClass(JDefinedClass classModel, List<FieldModel> fields, Class<?> contractInterface) throws Exception { |
301 | |
|
302 | |
|
303 | 0 | JDefinedClass builderClass = classModel._class(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, Util.BUILDER_CLASS_NAME); |
304 | |
|
305 | |
|
306 | 0 | JClass literalBuilderClass = codeModel.ref("Builder"); |
307 | |
|
308 | |
|
309 | 0 | JDocComment javadoc = builderClass.javadoc(); |
310 | 0 | javadoc.append(Util.generateBuilderJavadoc(classModel.name(), contractInterface.getSimpleName())); |
311 | |
|
312 | 0 | builderClass._implements(contractInterface); |
313 | 0 | builderClass._implements(ModelBuilder.class); |
314 | 0 | builderClass._implements(Serializable.class); |
315 | |
|
316 | |
|
317 | 0 | for (FieldModel fieldModel : fields) { |
318 | 0 | builderClass.field(JMod.PRIVATE, fieldModel.fieldType, fieldModel.fieldName); |
319 | |
} |
320 | |
|
321 | |
|
322 | 0 | JMethod constructor = builderClass.constructor(JMod.PRIVATE); |
323 | 0 | constructor.body().directStatement("// TODO modify this constructor as needed to pass any required values and invoke the appropriate 'setter' methods"); |
324 | |
|
325 | 0 | renderBuilderDefaultCreate(builderClass, literalBuilderClass); |
326 | 0 | renderBuilderCreateContract(builderClass, literalBuilderClass, fields, contractInterface); |
327 | 0 | renderBuild(builderClass); |
328 | 0 | renderGetters(builderClass, fields); |
329 | 0 | renderSetters(builderClass, fields); |
330 | 0 | } |
331 | |
|
332 | |
private void renderBuilderDefaultCreate(JDefinedClass builderClass, JClass literalBuilderClass) { |
333 | 0 | JMethod createMethod = builderClass.method(JMod.PUBLIC | JMod.STATIC, literalBuilderClass, "create"); |
334 | 0 | JBlock createMethodBody = createMethod.body(); |
335 | 0 | createMethodBody.directStatement("// TODO modify as needed to pass any required values and add them to the signature of the 'create' method"); |
336 | 0 | createMethodBody.directStatement("return new Builder();"); |
337 | 0 | } |
338 | |
|
339 | |
private void renderBuilderCreateContract(JDefinedClass builderClass, JClass literalBuilderClass, List<FieldModel> fields, Class<?> contractInterface) { |
340 | 0 | JMethod createContractMethod = builderClass.method(JMod.PUBLIC | JMod.STATIC, literalBuilderClass, "create"); |
341 | 0 | JVar contractParam = createContractMethod.param(contractInterface, "contract"); |
342 | 0 | JBlock body = createContractMethod.body(); |
343 | 0 | JConditional nullContractCheck = body._if(contractParam.eq(JExpr._null())); |
344 | 0 | nullContractCheck._then().directStatement("throw new IllegalArgumentException(\"contract was null\");"); |
345 | 0 | body.directStatement("// TODO if create() is modified to accept required parameters, this will need to be modified"); |
346 | 0 | body.directStatement("Builder builder = create();"); |
347 | 0 | for (FieldModel fieldModel : fields) { |
348 | 0 | String fieldName = fieldModel.fieldName; |
349 | 0 | body.directStatement("builder." + Util.generateSetter(fieldName, "contract." + Util.generateGetter(fieldName, isBoolean(fieldModel.fieldType))) + ";"); |
350 | 0 | } |
351 | 0 | body.directStatement("return builder;"); |
352 | 0 | } |
353 | |
|
354 | |
private void renderBuild(JDefinedClass builderClass) { |
355 | 0 | JMethod buildMethod = builderClass.method(JMod.PUBLIC, builderClass.outer(), "build"); |
356 | 0 | buildMethod.body().directStatement("return new " + builderClass.outer().name() + "(this);"); |
357 | 0 | } |
358 | |
|
359 | |
private void renderSetters(JDefinedClass builderClass, List<FieldModel> fields) { |
360 | 0 | for (FieldModel fieldModel : fields) { |
361 | 0 | String fieldName = fieldModel.fieldName; |
362 | 0 | JMethod setterMethod = builderClass.method(JMod.PUBLIC, codeModel.VOID, Util.generateSetterName(fieldName)); |
363 | 0 | setterMethod.param(fieldModel.fieldType, fieldName); |
364 | 0 | setterMethod.body().directStatement("// TODO add validation of input value if required and throw IllegalArgumentException if needed"); |
365 | 0 | setterMethod.body().directStatement("this." + fieldName + " = " + fieldName + ";"); |
366 | 0 | } |
367 | 0 | } |
368 | |
|
369 | |
} |
370 | |
|
371 | 0 | private static class FieldModel { |
372 | |
|
373 | |
private final String fieldName; |
374 | |
private final Class<?> fieldType; |
375 | |
|
376 | 0 | private FieldModel(String fieldName, Class<?> fieldType) { |
377 | 0 | this.fieldName = fieldName; |
378 | 0 | this.fieldType = fieldType; |
379 | 0 | } |
380 | |
|
381 | |
} |
382 | |
|
383 | |
} |