2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.java.api.generator;
10 import static org.opendaylight.controller.sal.java.api.generator.Constants.*;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.LinkedHashMap;
15 import java.util.List;
18 import org.opendaylight.controller.binding.generator.util.TypeConstants;
19 import org.opendaylight.controller.sal.binding.model.api.*;
20 import org.opendaylight.controller.binding.generator.util.Types;
21 import org.opendaylight.controller.sal.binding.model.api.Enumeration.Pair;
22 import org.opendaylight.controller.sal.binding.model.api.MethodSignature.Parameter;
24 public final class GeneratorUtil {
26 private GeneratorUtil() {
29 public static String createIfcDeclaration(final GeneratedType genType, final String indent,
30 final Map<String, String> availableImports) {
31 return createFileDeclaration(IFC, genType, indent, availableImports, false, false);
34 public static String createClassDeclaration(final GeneratedTransferObject genTransferObject, final String indent,
35 final Map<String, String> availableImports, boolean isIdentity, boolean isInnerClass) {
36 return createFileDeclaration(CLASS, genTransferObject, indent, availableImports, isIdentity, isInnerClass);
39 public static String createPackageDeclaration(final String packageName) {
40 return PKG + GAP + packageName + SC;
43 private static String createFileDeclaration(final String type, final GeneratedType genType, final String indent,
44 final Map<String, String> availableImports, boolean isIdentity, boolean innerClass) {
45 final StringBuilder builder = new StringBuilder();
46 final String currentPkg = genType.getPackageName();
48 createComment(builder, genType.getComment(), indent);
50 if (!genType.getAnnotations().isEmpty()) {
51 final List<AnnotationType> annotations = genType.getAnnotations();
52 appendAnnotations(builder, annotations);
57 builder.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP + type + GAP + genType.getName() + GAP);
58 } else if (isIdentity) {
59 if (!(CLASS.equals(type))) {
60 throw new IllegalArgumentException("'identity' has to be generated as a class");
62 builder.append(indent + PUBLIC + GAP + ABSTRACT + GAP + type + GAP + genType.getName() + GAP);
64 builder.append(indent + PUBLIC + GAP + type + GAP + genType.getName() + GAP);
67 if (genType instanceof GeneratedTransferObject) {
68 GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
70 if (genTO.getExtends() != null) {
71 builder.append(EXTENDS + GAP);
72 String gtoString = getExplicitType(genTO.getExtends(), availableImports, currentPkg);
73 builder.append(gtoString + GAP);
77 final List<Type> genImplements = genType.getImplements();
78 if (!genImplements.isEmpty()) {
79 if (genType instanceof GeneratedTransferObject) {
80 builder.append(IMPLEMENTS + GAP);
82 builder.append(EXTENDS + GAP);
84 builder.append(getExplicitType(genImplements.get(0), availableImports, currentPkg));
86 for (int i = 1; i < genImplements.size(); ++i) {
88 builder.append(getExplicitType(genImplements.get(i), availableImports, currentPkg));
91 builder.append(GAP + LCB);
92 return builder.toString();
95 private static StringBuilder appendAnnotations(final StringBuilder builder, final List<AnnotationType> annotations) {
96 if ((builder != null) && (annotations != null)) {
97 for (final AnnotationType annotation : annotations) {
99 builder.append(annotation.getPackageName());
101 builder.append(annotation.getName());
103 if (annotation.containsParameters()) {
105 final List<AnnotationType.Parameter> parameters = annotation.getParameters();
106 appendAnnotationParams(builder, parameters);
114 private static StringBuilder appendAnnotationParams(final StringBuilder builder,
115 final List<AnnotationType.Parameter> parameters) {
116 if (parameters != null) {
118 for (final AnnotationType.Parameter param : parameters) {
123 builder.append(", ");
125 final String paramName = param.getName();
126 if (param.getValue() != null) {
127 builder.append(paramName);
128 builder.append(" = ");
129 builder.append(param.getValue());
131 builder.append(paramName);
132 builder.append(" = {");
133 final List<String> values = param.getValues();
134 builder.append(values.get(0));
135 for (int j = 1; j < values.size(); ++j) {
136 builder.append(", ");
137 builder.append(values.get(j));
147 public static String createConstant(final Constant constant, final String indent,
148 final Map<String, String> availableImports, final String currentPkg) {
149 final StringBuilder builder = new StringBuilder();
150 if (constant == null)
151 throw new IllegalArgumentException();
152 builder.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP);
153 builder.append(getExplicitType(constant.getType(), availableImports, currentPkg) + GAP + constant.getName());
154 builder.append(GAP + "=" + GAP);
156 if (constant.getName().equals(TypeConstants.PATTERN_CONSTANT_NAME)) {
159 builder.append(constant.getValue());
163 return builder.toString();
166 public static String createField(final GeneratedProperty property, final String indent,
167 final Map<String, String> availableImports, final String currentPkg) {
168 final StringBuilder builder = new StringBuilder();
169 if (!property.getAnnotations().isEmpty()) {
170 final List<AnnotationType> annotations = property.getAnnotations();
171 appendAnnotations(builder, annotations);
174 builder.append(indent + PRIVATE + GAP);
175 builder.append(getExplicitType(property.getReturnType(), availableImports, currentPkg) + GAP
176 + property.getName());
178 return builder.toString();
182 * Create method declaration in interface.
188 public static String createMethodDeclaration(final MethodSignature method, final String indent,
189 Map<String, String> availableImports, final String currentPkg) {
190 final StringBuilder builder = new StringBuilder();
192 if (method == null) {
193 throw new IllegalArgumentException("Method Signature parameter MUST be specified and cannot be NULL!");
196 final String comment = method.getComment();
197 final String name = method.getName();
199 throw new IllegalStateException("Method Name cannot be NULL!");
202 final Type type = method.getReturnType();
204 throw new IllegalStateException("Method Return type cannot be NULL!");
207 final List<Parameter> parameters = method.getParameters();
209 createComment(builder, comment, indent);
212 if (!method.getAnnotations().isEmpty()) {
213 builder.append(indent);
214 final List<AnnotationType> annotations = method.getAnnotations();
215 appendAnnotations(builder, annotations);
219 builder.append(indent + getExplicitType(type, availableImports, currentPkg) + GAP + name);
221 for (int i = 0; i < parameters.size(); i++) {
222 Parameter p = parameters.get(i);
223 String separator = COMMA;
224 if (i + 1 == parameters.size()) {
227 builder.append(getExplicitType(p.getType(), availableImports, currentPkg) + GAP + p.getName() + separator);
232 return builder.toString();
235 public static String createConstructor(final GeneratedTransferObject genTransferObject, final String indent,
236 final Map<String, String> availableImports, final boolean isIdentity, final boolean oneConstructor) {
237 if (genTransferObject == null) {
238 throw new IllegalArgumentException("Generated transfer object can't be null");
240 if (indent == null) {
241 throw new IllegalArgumentException("String with indent can't be null");
243 if (availableImports == null) {
244 throw new IllegalArgumentException("Map of available imports can't be null");
246 GeneratedTransferObject genTOTopParent = getTopParrentTransportObject(genTransferObject);
247 final List<GeneratedProperty> ctorProperties = resolveReadOnlyPropertiesFromTO(genTransferObject
249 final List<GeneratedProperty> ctorPropertiesAllParents = getPropertiesOfAllParents(genTransferObject
252 final String currentPkg = genTransferObject.getPackageName();
253 final String className = genTransferObject.getName();
255 String constructorPart = "";
256 if (oneConstructor) {
257 if (genTOTopParent != genTransferObject && genTOTopParent.isUnionType()) {
258 constructorPart = createConstructorForEveryParentProperty(indent, isIdentity, ctorProperties,
259 ctorPropertiesAllParents, availableImports, currentPkg, className);
262 constructorPart = createOneConstructor(indent, isIdentity, ctorProperties, ctorPropertiesAllParents,
263 availableImports, currentPkg, className);
266 } else { // union won't be extended
267 constructorPart = createConstructorForEveryProperty(indent, isIdentity, ctorProperties,
268 ctorPropertiesAllParents, availableImports, currentPkg, className);
271 return constructorPart;
274 private static String createOneConstructor(final String indent, boolean isIdentity,
275 final List<GeneratedProperty> properties, final List<GeneratedProperty> propertiesAllParents,
276 final Map<String, String> availableImports, final String currentPkg, final String className) {
277 if (indent == null) {
278 throw new IllegalArgumentException("String with indent can't be null");
280 if (properties == null) {
281 throw new IllegalArgumentException("List of generated properties can't be null");
283 if (propertiesAllParents == null) {
284 throw new IllegalArgumentException(
285 "List of generated properties of all parent transport objects can't be null");
287 if (availableImports == null) {
288 throw new IllegalArgumentException("Map of available imports can't be null");
290 if (currentPkg == null) {
291 throw new IllegalArgumentException("String with current package can't be null");
293 if (className == null) {
294 throw new IllegalArgumentException("String with class name can't be null");
297 final StringBuilder builder = new StringBuilder();
299 List<GeneratedProperty> propertiesAll = new ArrayList<GeneratedProperty>(properties);
300 propertiesAll.addAll(propertiesAllParents);
302 builder.append(createConstructorDeclarationToLeftParenthesis(className, indent, isIdentity));
303 builder.append(createMethodPropertiesDeclaration(propertiesAll, availableImports, currentPkg, COMMA + GAP));
304 builder.append(createConstructorDeclarationFromRightParenthesis());
305 builder.append(createConstructorSuper(propertiesAllParents, indent));
306 builder.append(createClassPropertiesInitialization(propertiesAll, indent));
307 builder.append(createConstructorClosingPart(indent));
308 return builder.toString();
311 private static String createConstructorForEveryParentProperty(final String indent, final boolean isIdentity,
312 final List<GeneratedProperty> properties, final List<GeneratedProperty> propertiesAllParents,
313 final Map<String, String> availableImports, final String currentPkg, final String className) {
314 if (indent == null) {
315 throw new IllegalArgumentException("String with indent can't be null");
317 if (properties == null) {
318 throw new IllegalArgumentException("List of generated properties can't be null");
320 if (propertiesAllParents == null) {
321 throw new IllegalArgumentException(
322 "List of generated properties of all parent transport objects can't be null");
324 if (availableImports == null) {
325 throw new IllegalArgumentException("Map of available imports can't be null");
327 if (currentPkg == null) {
328 throw new IllegalArgumentException("String with current package can't be null");
330 if (className == null) {
331 throw new IllegalArgumentException("String with class name can't be null");
333 final StringBuilder builder = new StringBuilder();
334 GeneratedProperty parentProperty;
335 Iterator<GeneratedProperty> parentPropertyIterator = propertiesAllParents.iterator();
338 parentProperty = null;
339 if (parentPropertyIterator.hasNext()) {
340 parentProperty = parentPropertyIterator.next();
343 List<GeneratedProperty> propertiesAndParentProperties = new ArrayList<GeneratedProperty>();
344 if (parentProperty != null) {
345 propertiesAndParentProperties.add(parentProperty);
347 propertiesAndParentProperties.addAll(properties);
349 builder.append(createConstructorDeclarationToLeftParenthesis(className, indent, isIdentity));
350 builder.append(createMethodPropertiesDeclaration(propertiesAndParentProperties, availableImports,
351 currentPkg, COMMA + GAP));
352 builder.append(createConstructorDeclarationFromRightParenthesis());
353 builder.append(createConstructorSuper(parentProperty, indent));
354 builder.append(createClassPropertiesInitialization(properties, indent));
355 builder.append(createConstructorClosingPart(indent));
356 } while (parentPropertyIterator.hasNext());
358 return builder.toString();
361 private static String createConstructorForEveryProperty(final String indent, final boolean isIdentity,
362 final List<GeneratedProperty> properties, final List<GeneratedProperty> propertiesAllParents,
363 final Map<String, String> availableImports, final String currentPkg, final String className) {
364 if (indent == null) {
365 throw new IllegalArgumentException("String with indent can't be null");
367 if (properties == null) {
368 throw new IllegalArgumentException("List of generated properties can't be null");
370 if (propertiesAllParents == null) {
371 throw new IllegalArgumentException(
372 "List of generated properties of all parent transport objects can't be null");
374 if (availableImports == null) {
375 throw new IllegalArgumentException("Map of available imports can't be null");
377 if (currentPkg == null) {
378 throw new IllegalArgumentException("String with current package can't be null");
380 if (className == null) {
381 throw new IllegalArgumentException("String with class name can't be null");
384 final StringBuilder builder = new StringBuilder();
386 GeneratedProperty property;
387 Iterator<GeneratedProperty> propertyIterator = properties.iterator();
391 if (propertyIterator.hasNext()) {
392 property = propertyIterator.next();
395 List<GeneratedProperty> propertyAndTopParentProperties = new ArrayList<GeneratedProperty>();
396 if (property != null) {
397 propertyAndTopParentProperties.add(property);
399 propertyAndTopParentProperties.addAll(propertiesAllParents);
401 builder.append(createConstructorDeclarationToLeftParenthesis(className, indent, isIdentity));
402 builder.append(createMethodPropertiesDeclaration(propertyAndTopParentProperties, availableImports,
403 currentPkg, COMMA + GAP));
404 builder.append(createConstructorDeclarationFromRightParenthesis());
405 builder.append(createConstructorSuper(propertiesAllParents, indent));
406 builder.append(createClassPropertyInitialization(property, indent));
407 builder.append(createConstructorClosingPart(indent));
408 } while (propertyIterator.hasNext());
410 return builder.toString();
414 * The method selects from input list of properties only those which have
415 * read only attribute set to true.
418 * contains list of properties of generated transfer object
419 * @return subset of <code>properties</code> which have read only attribute
422 private static List<GeneratedProperty> resolveReadOnlyPropertiesFromTO(List<GeneratedProperty> properties) {
423 List<GeneratedProperty> readOnlyProperties = new ArrayList<GeneratedProperty>();
424 if (properties != null) {
425 for (final GeneratedProperty property : properties) {
426 if (property.isReadOnly()) {
427 readOnlyProperties.add(property);
431 return readOnlyProperties;
434 private static String createMethodPropertiesDeclaration(final List<GeneratedProperty> parameters,
435 final Map<String, String> availableImports, final String currentPkg, final String parameterSeparator) {
436 StringBuilder builder = new StringBuilder();
437 if (parameters == null) {
438 throw new IllegalArgumentException("List of generated properties can't be null");
440 if (availableImports == null) {
441 throw new IllegalArgumentException("Map of available imports can't be null");
443 if (currentPkg == null) {
444 throw new IllegalArgumentException("String with current package can't be null");
446 if (parameterSeparator == null) {
447 throw new IllegalArgumentException("String with separator of parameters can't be null");
450 for (final GeneratedProperty parameter : parameters) {
451 builder.append(createMethodPropertyDeclaration(parameter, availableImports, currentPkg));
452 builder.append(parameterSeparator);
454 if (!parameters.isEmpty()) {
455 builder = builder.delete(builder.length() - parameterSeparator.length(), builder.length());
457 return builder.toString();
460 private static String createConstructorDeclarationToLeftParenthesis(final String className, final String indent,
461 final boolean isIdentity) {
462 if (className == null) {
463 throw new IllegalArgumentException("String with class name can't be null");
465 if (indent == null) {
466 throw new IllegalArgumentException("String with indent can't be null");
468 final StringBuilder builder = new StringBuilder();
469 builder.append(indent);
470 builder.append(isIdentity ? PROTECTED : PUBLIC);
472 builder.append(className);
474 return builder.toString();
477 private static String createConstructorDeclarationFromRightParenthesis() {
478 final StringBuilder builder = new StringBuilder();
479 builder.append(RB + GAP + LCB + NL);
480 return builder.toString();
483 private static String createConstructorSuper(final List<GeneratedProperty> propertiesAllParents, final String indent) {
484 if (indent == null) {
485 throw new IllegalArgumentException("String with indent can't be null");
487 if (propertiesAllParents == null) {
488 throw new IllegalArgumentException("List of all parent's properties can't be null");
490 StringBuilder builder = new StringBuilder();
491 builder.append(indent + TAB + "super(");
492 String propertySeparator = COMMA + GAP;
493 for (GeneratedProperty superProperty : propertiesAllParents) {
494 builder.append(superProperty.getName());
495 builder.append(propertySeparator);
497 if (!propertiesAllParents.isEmpty()) {
498 builder = builder.delete(builder.length() - propertySeparator.length(), builder.length());
501 builder.append(");" + NL);
502 return builder.toString();
505 private static String createConstructorSuper(final GeneratedProperty parentProperty, final String indent) {
506 if (indent == null) {
507 throw new IllegalArgumentException("String with indent can't be null");
509 if (parentProperty == null) {
510 throw new IllegalArgumentException("Parent property can't be null");
512 StringBuilder builder = new StringBuilder();
513 if (parentProperty != null) {
514 builder.append(indent + TAB + "super(");
515 builder.append(parentProperty.getName());
516 builder.append(");" + NL);
518 return builder.toString();
521 private static String createConstructorClosingPart(final String indent) {
522 if (indent == null) {
523 throw new IllegalArgumentException("String with indent can't be null");
525 final StringBuilder builder = new StringBuilder();
526 builder.append(indent);
528 builder.append(NL + NL);
529 return builder.toString();
532 private static String createClassPropertiesInitialization(final List<GeneratedProperty> properties,
533 final String indent) {
534 if (indent == null) {
535 throw new IllegalArgumentException("String with indent can't be null");
537 if (properties == null) {
538 throw new IllegalArgumentException("List of generated class properties can't be null");
540 final StringBuilder builder = new StringBuilder();
541 for (final GeneratedProperty property : properties) {
542 createClassPropertyInitialization(property, indent);
544 return builder.toString();
547 private static String createClassPropertyInitialization(final GeneratedProperty property, final String indent) {
548 if (indent == null) {
549 throw new IllegalArgumentException("String with indent can't be null");
551 if (property == null) {
552 throw new IllegalArgumentException("List of generated class properties can't be null");
554 final StringBuilder builder = new StringBuilder();
555 builder.append(indent);
557 builder.append("this.");
558 builder.append(property.getName());
559 builder.append(" = ");
560 builder.append(property.getName());
563 return builder.toString();
566 private static String createMethodPropertyDeclaration(final GeneratedProperty property,
567 final Map<String, String> availableImports, final String currentPkg) {
568 if (property == null) {
569 throw new IllegalArgumentException("Generated property can't be null");
571 if (availableImports == null) {
572 throw new IllegalArgumentException("Map of available imports can't be null");
574 if (currentPkg == null) {
575 throw new IllegalArgumentException("String with current package can't be null");
577 final StringBuilder builder = new StringBuilder();
578 builder.append(getExplicitType(property.getReturnType(), availableImports, currentPkg));
580 builder.append(property.getName());
581 return builder.toString();
584 public static String createGetter(final GeneratedProperty property, final String indent,
585 final Map<String, String> availableImports, final String currentPkg) {
586 final StringBuilder builder = new StringBuilder();
588 final Type type = property.getReturnType();
589 final String varName = property.getName();
590 final char first = Character.toUpperCase(varName.charAt(0));
591 final String methodName = "get" + first + varName.substring(1);
593 builder.append(indent + PUBLIC + GAP + getExplicitType(type, availableImports, currentPkg) + GAP + methodName);
594 builder.append(LB + RB + LCB + NL);
596 String currentIndent = indent + TAB;
598 builder.append(currentIndent + "return " + varName + SC + NL);
600 builder.append(indent + RCB);
601 return builder.toString();
604 public static String createSetter(final GeneratedProperty property, final String indent,
605 final Map<String, String> availableImports, final String currentPkg) {
606 final StringBuilder builder = new StringBuilder();
608 final Type type = property.getReturnType();
609 final String varName = property.getName();
610 final char first = Character.toUpperCase(varName.charAt(0));
611 final String methodName = "set" + first + varName.substring(1);
613 builder.append(indent + PUBLIC + GAP + "void" + GAP + methodName);
614 builder.append(LB + getExplicitType(type, availableImports, currentPkg) + GAP + varName + RB + LCB + NL);
615 String currentIndent = indent + TAB;
616 builder.append(currentIndent + "this." + varName + " = " + varName + SC + NL);
617 builder.append(indent + RCB);
618 return builder.toString();
621 public static String createHashCode(final List<GeneratedProperty> properties, final String indent) {
622 StringBuilder builder = new StringBuilder();
623 builder.append(indent + "public int hashCode() {" + NL);
624 builder.append(indent + TAB + "final int prime = 31;" + NL);
625 builder.append(indent + TAB + "int result = 1;" + NL);
627 for (GeneratedProperty property : properties) {
628 String fieldName = property.getName();
629 builder.append(indent + TAB + "result = prime * result + ((" + fieldName + " == null) ? 0 : " + fieldName
630 + ".hashCode());" + NL);
633 builder.append(indent + TAB + "return result;" + NL);
634 builder.append(indent + RCB + NL);
635 return builder.toString();
638 public static String createEquals(final GeneratedTransferObject type, final List<GeneratedProperty> properties,
639 final String indent) {
640 final StringBuilder builder = new StringBuilder();
641 final String indent1 = indent + TAB;
642 final String indent2 = indent1 + TAB;
643 final String indent3 = indent2 + TAB;
645 builder.append(indent + "public boolean equals(Object obj) {" + NL);
646 builder.append(indent1 + "if (this == obj) {" + NL);
647 builder.append(indent2 + "return true;" + NL);
648 builder.append(indent1 + "}" + NL);
649 builder.append(indent1 + "if (obj == null) {" + NL);
650 builder.append(indent2 + "return false;" + NL);
651 builder.append(indent1 + "}" + NL);
652 builder.append(indent1 + "if (getClass() != obj.getClass()) {" + NL);
653 builder.append(indent2 + "return false;" + NL);
654 builder.append(indent1 + "}" + NL);
656 String typeStr = type.getName();
657 builder.append(indent1 + typeStr + " other = (" + typeStr + ") obj;" + NL);
659 for (final GeneratedProperty property : properties) {
660 String fieldName = property.getName();
661 builder.append(indent1 + "if (" + fieldName + " == null) {" + NL);
662 builder.append(indent2 + "if (other." + fieldName + " != null) {" + NL);
663 builder.append(indent3 + "return false;" + NL);
664 builder.append(indent2 + "}" + NL);
665 builder.append(indent1 + "} else if (!" + fieldName + ".equals(other." + fieldName + ")) {" + NL);
666 builder.append(indent2 + "return false;" + NL);
667 builder.append(indent1 + "}" + NL);
670 builder.append(indent1 + "return true;" + NL);
671 builder.append(indent + RCB + NL);
672 return builder.toString();
675 public static String createToString(final GeneratedTransferObject type, final List<GeneratedProperty> properties,
676 final String indent) {
677 StringBuilder builder = new StringBuilder();
678 builder.append(indent);
679 builder.append("public String toString() {");
681 builder.append(indent);
683 builder.append("StringBuilder builder = new StringBuilder();");
685 builder.append(indent);
687 builder.append("builder.append(\"");
688 builder.append(type.getName());
689 builder.append(" [");
691 boolean first = true;
692 for (final GeneratedProperty property : properties) {
694 builder.append(property.getName());
695 builder.append("=\");");
697 builder.append(indent);
699 builder.append("builder.append(");
700 builder.append(property.getName());
701 builder.append(");");
705 builder.append(indent);
707 builder.append("builder.append(\", ");
708 builder.append(property.getName());
709 builder.append("=\");");
711 builder.append(indent);
713 builder.append("builder.append(");
714 builder.append(property.getName());
715 builder.append(");");
719 builder.append(indent);
721 builder.append("builder.append(\"]\");");
723 builder.append(indent);
725 builder.append("return builder.toString();");
728 builder.append(indent);
731 return builder.toString();
734 public static String createEnum(final Enumeration enumeration, final String indent) {
735 if (enumeration == null || indent == null)
736 throw new IllegalArgumentException();
737 final StringBuilder builder = new StringBuilder(indent + PUBLIC + GAP + ENUM + GAP + enumeration.getName()
740 String separator = COMMA + NL;
741 final List<Pair> values = enumeration.getValues();
743 for (int i = 0; i < values.size(); i++) {
744 if (i + 1 == values.size()) {
747 builder.append(indent + TAB + values.get(i).getName() + LB + values.get(i).getValue() + RB + separator);
751 final String ENUMERATION_NAME = "value";
752 final String ENUMERATION_TYPE = "int";
753 builder.append(indent + TAB + ENUMERATION_TYPE + GAP + ENUMERATION_NAME + SC);
755 builder.append(indent + TAB + PRIVATE + GAP + enumeration.getName() + LB + ENUMERATION_TYPE + GAP
756 + ENUMERATION_NAME + RB + GAP + LCB + NL);
757 builder.append(indent + TAB + TAB + "this." + ENUMERATION_NAME + GAP + "=" + GAP + ENUMERATION_NAME + SC + NL);
758 builder.append(indent + TAB + RCB + NL);
760 builder.append(indent + RCB);
762 return builder.toString();
765 private static String getExplicitType(final Type type, final Map<String, String> imports, final String currentPkg) {
767 throw new IllegalArgumentException("Type parameter MUST be specified and cannot be NULL!");
769 if (type.getName() == null) {
770 throw new IllegalArgumentException("Type name cannot be NULL!");
772 if (type.getPackageName() == null) {
773 throw new IllegalArgumentException("Type cannot have Package Name referenced as NULL!");
775 if (imports == null) {
776 throw new IllegalArgumentException("Imports Map cannot be NULL!");
779 final String typePackageName = type.getPackageName();
780 final String typeName = type.getName();
781 final String importedPackageName = imports.get(typeName);
782 if (typePackageName.equals(importedPackageName) || typePackageName.equals(currentPkg)) {
783 final StringBuilder builder = new StringBuilder(type.getName());
784 if (type instanceof ParameterizedType) {
785 final ParameterizedType pType = (ParameterizedType) type;
786 final Type[] pTypes = pType.getActualTypeArguments();
788 builder.append(getParameters(pTypes, imports, currentPkg));
791 if (builder.toString().equals("Void")) {
794 return builder.toString();
796 final StringBuilder builder = new StringBuilder();
797 if (typePackageName.startsWith("java.lang")) {
798 builder.append(type.getName());
800 if (!typePackageName.isEmpty()) {
801 builder.append(typePackageName + "." + type.getName());
803 builder.append(type.getName());
806 if (type.equals(Types.voidType())) {
809 if (type instanceof ParameterizedType) {
810 final ParameterizedType pType = (ParameterizedType) type;
811 final Type[] pTypes = pType.getActualTypeArguments();
813 builder.append(getParameters(pTypes, imports, currentPkg));
816 return builder.toString();
820 private static String getParameters(final Type[] pTypes, Map<String, String> availableImports, String currentPkg) {
821 final StringBuilder builder = new StringBuilder();
822 for (int i = 0; i < pTypes.length; i++) {
823 final Type t = pTypes[i];
825 String separator = COMMA;
826 if (i == (pTypes.length - 1)) {
830 String wildcardParam = "";
831 if (t.equals(Types.voidType())) {
832 builder.append("java.lang.Void" + separator);
836 if (t instanceof WildcardType) {
837 wildcardParam = "? extends ";
840 builder.append(wildcardParam + getExplicitType(t, availableImports, currentPkg) + separator);
843 return builder.toString();
846 private static void createComment(final StringBuilder builder, final String comment, final String indent) {
847 if (comment != null && comment.length() > 0) {
848 builder.append(indent + "/*" + NL);
849 builder.append(indent + comment + NL);
850 builder.append(indent + "*/" + NL);
854 public static Map<String, String> createImports(GeneratedType genType) {
855 if (genType == null) {
856 throw new IllegalArgumentException("Generated Type cannot be NULL!");
858 final Map<String, String> imports = new LinkedHashMap<>();
859 List<GeneratedType> childGeneratedTypes = genType.getEnclosedTypes();
860 if (!childGeneratedTypes.isEmpty()) {
861 for (GeneratedType genTypeChild : childGeneratedTypes) {
862 imports.putAll(createImports(genTypeChild));
866 final List<Constant> constants = genType.getConstantDefinitions();
867 final List<MethodSignature> methods = genType.getMethodDefinitions();
868 final List<Type> impl = genType.getImplements();
872 for (final Type type : impl) {
873 putTypeIntoImports(genType, type, imports);
878 if (constants != null) {
879 for (final Constant constant : constants) {
880 final Type constantType = constant.getType();
881 putTypeIntoImports(genType, constantType, imports);
885 // REGULAR EXPRESSION
886 if (genType instanceof GeneratedTransferObject) {
887 if (isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, (GeneratedTransferObject) genType)) {
888 putTypeIntoImports(genType, Types.typeForClass(java.util.regex.Pattern.class), imports);
889 putTypeIntoImports(genType, Types.typeForClass(java.util.Arrays.class), imports);
890 putTypeIntoImports(genType, Types.typeForClass(java.util.ArrayList.class), imports);
895 if (methods != null) {
896 for (final MethodSignature method : methods) {
897 final Type methodReturnType = method.getReturnType();
898 putTypeIntoImports(genType, methodReturnType, imports);
899 for (final MethodSignature.Parameter methodParam : method.getParameters()) {
900 putTypeIntoImports(genType, methodParam.getType(), imports);
906 if (genType instanceof GeneratedTransferObject) {
907 final GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
908 final List<GeneratedProperty> properties = genTO.getProperties();
909 if (properties != null) {
910 for (GeneratedProperty property : properties) {
911 final Type propertyType = property.getReturnType();
912 putTypeIntoImports(genType, propertyType, imports);
920 public static Map<String, String> createChildImports(GeneratedType genType) {
921 Map<String, String> childImports = new LinkedHashMap<>();
922 List<GeneratedType> childGeneratedTypes = genType.getEnclosedTypes();
923 if (!childGeneratedTypes.isEmpty()) {
924 for (GeneratedType genTypeChild : childGeneratedTypes) {
925 createChildImports(genTypeChild);
926 childImports.put(genTypeChild.getName(), genTypeChild.getPackageName());
932 private static void putTypeIntoImports(final GeneratedType parentGenType, final Type type,
933 final Map<String, String> imports) {
934 if (parentGenType == null) {
935 throw new IllegalArgumentException("Parent Generated Type parameter MUST be specified and cannot be "
938 if (parentGenType.getName() == null) {
939 throw new IllegalArgumentException("Parent Generated Type name cannot be NULL!");
941 if (parentGenType.getPackageName() == null) {
942 throw new IllegalArgumentException("Parent Generated Type cannot have Package Name referenced as NULL!");
945 throw new IllegalArgumentException("Type parameter MUST be specified and cannot be NULL!");
947 if (type.getName() == null) {
948 throw new IllegalArgumentException("Type name cannot be NULL!");
950 if (type.getPackageName() == null) {
951 throw new IllegalArgumentException("Type cannot have Package Name referenced as NULL!");
954 final String typeName = type.getName();
955 final String typePackageName = type.getPackageName();
956 final String parentTypeName = parentGenType.getName();
957 final String parentTypePackageName = parentGenType.getPackageName();
958 if (typeName.equals(parentTypeName) || typePackageName.startsWith("java.lang")
959 || typePackageName.equals(parentTypePackageName) || typePackageName.isEmpty()) {
962 if (!imports.containsKey(typeName)) {
963 imports.put(typeName, typePackageName);
965 if (type instanceof ParameterizedType) {
966 final ParameterizedType paramType = (ParameterizedType) type;
967 final Type[] params = paramType.getActualTypeArguments();
968 for (Type param : params) {
969 putTypeIntoImports(parentGenType, param, imports);
974 public static List<String> createImportLines(final Map<String, String> imports,
975 final Map<String, String> innerTypeImports) {
976 final List<String> importLines = new ArrayList<>();
978 for (Map.Entry<String, String> entry : imports.entrySet()) {
979 final String typeName = entry.getKey();
980 final String packageName = entry.getValue();
981 if (innerTypeImports != null) {
982 String innerTypePackageName = innerTypeImports.get(typeName);
983 if (innerTypePackageName != null) {
984 if (innerTypePackageName.equals(packageName))
988 importLines.add("import " + packageName + "." + typeName + SC);
993 public static boolean isConstantInTO(String constName, GeneratedTransferObject genTO) {
994 if (constName == null || genTO == null)
995 throw new IllegalArgumentException();
996 List<Constant> consts = genTO.getConstantDefinitions();
997 for (Constant cons : consts) {
998 if (cons.getName().equals(constName)) {
1007 * The method returns reference to highest (top parent) Generated Transfer
1010 * @param childTransportObject
1011 * is generated transfer object which can be extended by other
1012 * generated transfer object
1013 * @return in first case that <code>childTransportObject</code> isn't
1014 * extended then <code>childTransportObject</code> is returned. In
1015 * second case the method is recursive called until first case.
1017 private static GeneratedTransferObject getTopParrentTransportObject(GeneratedTransferObject childTransportObject) {
1018 if (childTransportObject == null) {
1019 throw new IllegalArgumentException("Parameter childTransportObject can't be null.");
1021 if (childTransportObject.getExtends() == null) {
1022 return childTransportObject;
1024 return getTopParrentTransportObject(childTransportObject.getExtends());
1029 * The method returns the list of the properties of all extending generated
1030 * transfer object from <code>genTO</code> to highest parent generated
1034 * @return the list of all properties from actual to highest parent
1035 * generated transfer object. In case when extension exists the
1036 * method is recursive called.
1038 private static List<GeneratedProperty> getPropertiesOfAllParents(GeneratedTransferObject genTO) {
1039 List<GeneratedProperty> propertiesOfAllParents = new ArrayList<GeneratedProperty>();
1040 if (genTO != null) {
1041 final List<GeneratedProperty> allPropertiesOfTO = genTO.getProperties();
1042 List<GeneratedProperty> readOnlyPropertiesOfTO = resolveReadOnlyPropertiesFromTO(allPropertiesOfTO);
1043 propertiesOfAllParents.addAll(readOnlyPropertiesOfTO);
1044 if (genTO.getExtends() != null) {
1045 propertiesOfAllParents.addAll(getPropertiesOfAllParents(genTO.getExtends()));
1048 return propertiesOfAllParents;
1051 public static String createStaticInicializationBlock(GeneratedTransferObject genTransferObject, String indent) {
1053 final StringBuilder builder = new StringBuilder();
1055 List<Constant> constants = genTransferObject.getConstantDefinitions();
1056 for (Constant constant : constants) {
1057 if (constant.getName() == null || constant.getType() == null || constant.getValue() == null) {
1060 if (constant.getName().equals(TypeConstants.PATTERN_CONSTANT_NAME)) {
1061 final Object constValue = constant.getValue();
1062 List<String> regularExpressions = new ArrayList<>();
1063 if (constValue instanceof List) {
1064 builder.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP + "List<String>" + GAP
1065 + TypeConstants.PATTERN_CONSTANT_NAME + GAP + "=" + GAP + "Arrays.asList" + LB);
1066 final List<?> constantValues = (List<?>) constValue;
1067 int stringsCount = 0;
1068 for (Object value : constantValues) {
1069 if (value instanceof String) {
1070 if (stringsCount > 0) {
1071 builder.append(COMMA);
1074 regularExpressions.add((String) value);
1075 builder.append(DOUBLE_QUOTE + (String) value + DOUBLE_QUOTE);
1078 builder.append(RB + SC + NL);
1080 builder.append(indent + PRIVATE + GAP + STATIC + GAP + FINAL + GAP + "List<Pattern>" + GAP
1081 + MEMBER_PATTERN_LIST + GAP + ASSIGN + GAP + "new ArrayList<Pattern>()" + GAP + SC + NL + NL);
1083 if (!regularExpressions.isEmpty()) {
1084 builder.append(indent + STATIC + LCB + NL);
1085 builder.append(indent + TAB + "for (String regEx : " + TypeConstants.PATTERN_CONSTANT_NAME + ") {"
1087 builder.append(indent + TAB + TAB + MEMBER_PATTERN_LIST + ".add(Pattern.compile(regEx))" + SC + NL);
1088 builder.append(indent + TAB + RCB + NL);
1089 builder.append(indent + RCB + NL + NL);
1094 return builder.toString();