1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.nuiton.eugene.java;
24
25 import com.google.common.base.Joiner;
26 import com.google.common.collect.Lists;
27 import org.apache.commons.collections4.CollectionUtils;
28 import org.apache.commons.lang3.StringUtils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.codehaus.plexus.component.annotations.Component;
32 import org.nuiton.eugene.GeneratorUtil;
33 import org.nuiton.eugene.Template;
34 import org.nuiton.eugene.java.extension.AnnotationsManagerExtension;
35 import org.nuiton.eugene.java.extension.ImportsManagerExtension;
36 import org.nuiton.eugene.java.extension.ObjectModelAnnotation;
37 import org.nuiton.eugene.java.extension.ObjectModelAnnotationParameter;
38 import org.nuiton.eugene.models.object.ObjectModelAttribute;
39 import org.nuiton.eugene.models.object.ObjectModelClass;
40 import org.nuiton.eugene.models.object.ObjectModelClassifier;
41 import org.nuiton.eugene.models.object.ObjectModelElement;
42 import org.nuiton.eugene.models.object.ObjectModelEnumeration;
43 import org.nuiton.eugene.models.object.ObjectModelGenerator;
44 import org.nuiton.eugene.models.object.ObjectModelInterface;
45 import org.nuiton.eugene.models.object.ObjectModelOperation;
46 import org.nuiton.eugene.models.object.ObjectModelParameter;
47
48 import java.io.File;
49 import java.io.IOException;
50 import java.io.Writer;
51 import java.util.Arrays;
52 import java.util.Collection;
53 import java.util.Iterator;
54 import java.util.List;
55 import java.util.Set;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 @Component(role = Template.class, hint = "org.nuiton.eugene.java.JavaGenerator")
75 public class JavaGenerator extends ObjectModelGenerator {
76
77 private static final Log log = LogFactory.getLog(JavaGenerator.class);
78
79 protected int innerLevel;
80
81 protected String prefix;
82
83 @Override
84 public String getFilenameForClass(ObjectModelClass clazz) {
85 return getFilenameForClassifier(clazz);
86 }
87
88 @Override
89 public String getFilenameForInterface(ObjectModelInterface interfacez) {
90 return getFilenameForClassifier(interfacez);
91 }
92
93 @Override
94 public String getFilenameForEnumeration(ObjectModelEnumeration enumeration) {
95 return getFilenameForClassifier(enumeration);
96 }
97
98 @Override
99 public String getFilenameForClassifier(ObjectModelClassifier clazz) {
100 String s = clazz.getQualifiedName();
101 int index = s.indexOf("<");
102 if (index > -1) {
103 s = s.substring(0, index);
104 }
105 return s.replace('.', File.separatorChar) + ".java";
106 }
107
108
109
110
111
112
113
114
115 @Override
116 public void generateFromClass(Writer output, ObjectModelClass input)
117 throws IOException {
118
119 if (isVerbose()) {
120 log.info("Will generate class " + input.getQualifiedName());
121 }
122 preparePrefix(input);
123
124
125 generateHeader(output, input);
126
127 String abstractStr = input.isAbstract() ? " abstract" : "";
128 String staticStr = input.isStatic() ? " static" : "";
129 String className = input.getName();
130
131 String extend = "";
132 Iterator<ObjectModelClass> j = input.getSuperclasses().iterator();
133 if (j.hasNext()) {
134 ObjectModelClassifier p = j.next();
135 extend += GeneratorUtil.getSimpleName(p.getQualifiedName());
136 }
137
138 String implement = "";
139 for (Iterator<ObjectModelInterface> i =
140 input.getInterfaces().iterator(); i.hasNext(); ) {
141 ObjectModelClassifier parentInterface = i.next();
142 String interfaceName = GeneratorUtil.getSimpleName(
143 parentInterface.getQualifiedName());
144 implement += interfaceName;
145 if (i.hasNext()) {
146 implement += ", ";
147 }
148 }
149 if (log.isDebugEnabled()) {
150 log.debug(className + " : super : " + extend + ", interfaces : "
151 + implement);
152 }
153 generateAnnotations(output, input, input);
154
155
156
157
158
159
160 if (extend.length() > 0) {
161
162 }
163
164 if (implement.length() > 0) {
165
166 }
167
168
169
170 generateInnerClassifiers(output, input.getInnerClassifiers());
171 preparePrefix(input);
172 generateAttributes(output, input, input.getAttributes());
173 generateOperations(output, input, input.getOperations());
174
175
176
177 }
178
179 @Override
180 public void generateFromInterface(Writer output,
181 ObjectModelInterface input)
182 throws IOException {
183 if (isVerbose()) {
184 log.info("Will generate interface " + input.getQualifiedName());
185 }
186 preparePrefix(input);
187
188
189 generateHeader(output, input);
190
191 String interfaceName = input.getName();
192
193 String extend = "";
194 Iterator<ObjectModelInterface> j = input.getInterfaces().iterator();
195 while (j.hasNext()) {
196 ObjectModelClassifier p = j.next();
197 extend += GeneratorUtil.getSimpleName(p.getQualifiedName());
198 if (j.hasNext()) {
199 extend += ", ";
200 }
201 }
202 generateAnnotations(output, input, input);
203
204
205
206
207
208 if (extend.length() > 0) {
209
210 }
211
212
213 generateAttributes(output, input, input.getAttributes());
214 generateOperations(output, input, input.getOperations());
215
216
217 }
218
219 public void generateAnnotations(Writer output,
220 ObjectModelClassifier clazz,
221 ObjectModelElement element)
222 throws IOException {
223 AnnotationsManagerExtension managers = getModel().getExtension(
224 AnnotationsManagerExtension.OBJECTMODEL_EXTENSION,
225 AnnotationsManagerExtension.class);
226 List<ObjectModelAnnotation> annotations =
227 managers.getAnnotations(clazz, element);
228 for (ObjectModelAnnotation annotation : annotations) {
229
230
231
232
233 StringBuilder annotationBuilder = new StringBuilder("@" + annotation.getType());
234 List<ObjectModelAnnotationParameter> annotationParameters = annotation.getParameters();
235 if (CollectionUtils.isNotEmpty(annotationParameters)) {
236 annotationBuilder.append('(');
237 List<String> params = Lists.newArrayList();
238 for (ObjectModelAnnotationParameter annotationParameter : annotationParameters) {
239 String paramStr = annotationParameter.getName() + " = ";
240 Object value = annotationParameter.getValue();
241 if (value instanceof String) {
242 paramStr += "\"" + value + "\"";
243 } else if (value instanceof Enum) {
244 Enum anEnum = (Enum) value;
245
246 paramStr += anEnum.getClass().getSimpleName() + "." + value;
247 } else {
248 paramStr += value.toString();
249 }
250 params.add(paramStr);
251 }
252 Joiner.on(", ").appendTo(annotationBuilder, params);
253
254 annotationBuilder.append(')');
255 }
256 String annotationStr = annotationBuilder.toString();
257
258 if (element instanceof ObjectModelOperation || element instanceof ObjectModelAttribute) {
259
260 annotationStr = " " + annotationStr;
261 }
262
263 if (element instanceof ObjectModelClassifier || element instanceof ObjectModelOperation || element instanceof ObjectModelAttribute) {
264
265
266 }
267 }
268 }
269
270 @Override
271 public void generateFromEnumeration(Writer output,
272 ObjectModelEnumeration input) throws IOException {
273
274 generateFromEnum(output, input);
275 }
276
277 @Override
278 public void generateFromEnum(Writer output, ObjectModelEnumeration input)
279 throws IOException {
280 if (isVerbose()) {
281 log.info("Will generate enumeration " + input.getQualifiedName());
282 }
283 preparePrefix(input);
284 generateHeader(output, input);
285
286 String enumzName = input.getName();
287
288 String extend = "";
289 Iterator<ObjectModelInterface> j = input.getInterfaces().iterator();
290 if (j.hasNext()) {
291 ObjectModelClassifier p = j.next();
292 extend += GeneratorUtil.getSimpleName(p.getQualifiedName());
293 }
294 generateAnnotations(output, input, input);
295
296
297
298 if (extend.length() > 0) {
299
300
301 } else {
302
303
304 }
305
306 if (input.getLiterals().isEmpty()) {
307
308 } else {
309 Iterator<String> i = input.getLiterals().iterator();
310 while (i.hasNext()) {
311 String literal = i.next();
312
313
314 }
315 }
316 generateAttributes(output, input, input.getAttributes());
317 generateOperations(output, input, input.getOperations());
318
319
320 }
321
322 public void generateInnerClassifiers(Writer output,
323 Collection<ObjectModelClassifier>
324 innerClassifiers)
325 throws IOException {
326 if (innerClassifiers == null || innerClassifiers.isEmpty()) {
327 return;
328 }
329 for (ObjectModelClassifier innerClassifier : innerClassifiers) {
330 if (innerClassifier.isClass()) {
331 generateFromClass(output, (ObjectModelClass) innerClassifier);
332 innerLevel--;
333 continue;
334 }
335 if (innerClassifier.isInterface()) {
336 generateFromInterface(output, (ObjectModelInterface)
337 innerClassifier);
338 innerLevel--;
339 continue;
340 }
341 if (innerClassifier.isEnum()) {
342 generateFromEnum(output, (ObjectModelEnumeration)
343 innerClassifier);
344 innerLevel--;
345 }
346 }
347 }
348
349 protected void preparePrefix(ObjectModelClassifier clazz) {
350 if (!clazz.isInner()) {
351 innerLevel = 0;
352 prefix = "";
353 } else {
354 innerLevel++;
355 char[] tmp = new char[innerLevel * 4];
356 Arrays.fill(tmp, ' ');
357 prefix = new String(tmp);
358 }
359 if (log.isDebugEnabled()) {
360 log.debug("prefix to use for classifier " +
361 clazz.getName() + " : [" + prefix + "]");
362 }
363 }
364
365
366
367
368
369
370
371
372
373 protected void generateHeader(Writer output,
374 ObjectModelClassifier classifier)
375 throws IOException {
376 if (classifier.isInner()) {
377 return;
378 }
379 String packageName = classifier.getPackageName();
380
381
382
383
384 ImportsManagerExtension managers = getModel().getExtension(
385 ImportsManagerExtension.OBJECTMODEL_EXTENSION,
386 ImportsManagerExtension.class);
387
388 List<String> imports = managers.getImports(classifier);
389 for (String singleImport : imports) {
390
391
392 }
393 if (CollectionUtils.isNotEmpty(imports)) {
394
395
396 }
397 }
398
399
400
401
402
403
404
405
406
407 protected void generateAttributes(Writer output, ObjectModelClassifier clazz,
408 Collection<ObjectModelAttribute>
409 attributes)
410 throws IOException {
411
412 for (ObjectModelAttribute attr : attributes) {
413
414
415
416 String documentation = attr.getDocumentation();
417 if (StringUtils.isNotEmpty(documentation)) {
418
419
420 String[] lines = documentation.split("\n");
421 for (String line : lines) {
422
423
424 }
425
426
427 }
428
429 generateAnnotations(output, clazz, attr);
430 String attrName = attr.getName();
431 String attrVisibility = attr.getVisibility();
432 String attrType = GeneratorUtil.getSimpleName(attr.getType());
433 String attrStatic = attr.isStatic() ? "static " : "";
434 String attrFinal = attr.isFinal() ? "final " : "";
435 String attrTransient = attr.isTransient() ? "transient " : "";
436 if (clazz instanceof ObjectModelInterface) {
437
438
439 attrStatic = "";
440 attrFinal = "";
441 attrTransient = "";
442 attrVisibility = "";
443 }
444
445 if (StringUtils.isNotEmpty(attrVisibility)) {
446 attrVisibility += " ";
447 }
448
449 String attrValue = StringUtils.isNotEmpty(attr.getDefaultValue()) ?
450
451
452
453 " = " + attr.getDefaultValue() : "";
454
455
456
457
458 }
459 }
460
461
462
463
464
465
466
467
468
469 protected void generateOperations(Writer output, ObjectModelClassifier clazz,
470 Collection<ObjectModelOperation>
471 operations) throws IOException {
472 if (!operations.isEmpty()) {
473
474
475 }
476
477
478
479
480 boolean interfacez =
481 ObjectModelInterface.class.isAssignableFrom(clazz.getClass());
482
483 for (ObjectModelOperation op : operations) {
484 String opName = op.getName();
485 if (opName == null) {
486 generateBlock(output, clazz, op);
487 continue;
488 }
489 generateOperationDocumentation(output, op);
490
491 generateAnnotations(output, clazz, op);
492
493 String opVisibility = !interfacez ? op.getVisibility() : "";
494 String opStatic = op.isStatic() ? "static " : "";
495 String opAbstract = op.isAbstract() ? "abstract " : "";
496
497 ObjectModelParameter returnParam = op.getReturnParameter();
498 String opReturn = "";
499 if (returnParam != null) {
500 opReturn = GeneratorUtil.getSimpleName(
501 returnParam.getType()) + " ";
502 }
503 if (StringUtils.isNotEmpty(opVisibility)) {
504 opVisibility += " ";
505 }
506
507
508 String comma = "";
509 Collection<ObjectModelParameter> params = op.getParameters();
510 for (ObjectModelParameter param : params) {
511 String paramName = param.getName();
512 String paramType = GeneratorUtil.getSimpleName(param.getType());
513
514 comma = ", ";
515 }
516
517
518 comma = " throws ";
519 Set<String> exceptions = op.getExceptions();
520 for (String exception : exceptions) {
521 String exceptionName = GeneratorUtil.getSimpleName(exception);
522
523 comma = ", ";
524 }
525
526
527 if (!(clazz instanceof ObjectModelInterface) && !op.isAbstract()) {
528 String body = op.getBodyCode() == null ? "" : op.getBodyCode();
529
530
531
532 } else {
533
534
535
536 }
537 }
538 }
539
540 protected void generateOperationDocumentation(Writer output,
541 ObjectModelOperation op) throws IOException {
542 String documentation = op.getDocumentation();
543 if (StringUtils.isEmpty(documentation)) {
544
545
546 return;
547 }
548
549
550
551 String[] documentationLines = documentation.split("\n");
552 for (String documentationLine : documentationLines) {
553
554
555 }
556 Collection<ObjectModelParameter> params = op.getParameters();
557 for (ObjectModelParameter param : params) {
558 String paramName = param.getName();
559 String paramDocumentation = param.getDocumentation();
560 if (paramDocumentation == null) {
561 paramDocumentation = "";
562 }
563
564
565 }
566
567 ObjectModelParameter returnParam = op.getReturnParameter();
568 String opReturn = "";
569 if (returnParam != null) {
570 opReturn = GeneratorUtil.getSimpleName(
571 returnParam.getType()) + " ";
572 if (!opReturn.contains("void")) {
573 String paramDocumentation = returnParam.getDocumentation();
574 if (paramDocumentation == null) {
575 paramDocumentation = "";
576 }
577
578
579 }
580 }
581 Set<String> exceptions = op.getExceptions();
582 for (String exception : exceptions) {
583 String exceptionName = GeneratorUtil.getSimpleName(exception);
584
585
586 }
587
588
589
590 }
591
592 protected void generateBlock(Writer output,
593 ObjectModelClassifier clazz,
594 ObjectModelOperation op) throws IOException {
595 String opStatic = op.isStatic() ? "static " : " ";
596
597
598
599
600
601
602 }
603
604 }