Added support to generate interfaces from Choices and Cases.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / controller / sal / java / api / generator / GeneratorUtil.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.java.api.generator;
9
10 import static org.opendaylight.controller.sal.java.api.generator.Constants.*;
11
12 import java.util.ArrayList;
13 import java.util.LinkedHashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 import org.opendaylight.controller.binding.generator.util.TypeConstants;
18 import org.opendaylight.controller.sal.binding.model.api.*;
19 import org.opendaylight.controller.binding.generator.util.Types;
20 import org.opendaylight.controller.sal.binding.model.api.Enumeration.Pair;
21 import org.opendaylight.controller.sal.binding.model.api.MethodSignature.Parameter;
22
23 public final class GeneratorUtil {
24
25     private GeneratorUtil() {
26     }
27
28     public static String createIfcDeclaration(final GeneratedType genType, final String indent,
29             final Map<String, String> availableImports) {
30         return createFileDeclaration(IFC, genType, indent, availableImports, false);
31     }
32
33     public static String createClassDeclaration(final GeneratedTransferObject genTransferObject, final String indent,
34             final Map<String, String> availableImports, boolean isIdentity) {
35         return createFileDeclaration(CLASS, genTransferObject, indent, availableImports, isIdentity);
36     }
37
38     public static String createPackageDeclaration(final String packageName) {
39         return PKG + GAP + packageName + SC;
40     }
41
42     private static String createFileDeclaration(final String type, final GeneratedType genType, final String indent,
43             final Map<String, String> availableImports, boolean isIdentity) {
44         final StringBuilder builder = new StringBuilder();
45         final String currentPkg = genType.getPackageName();
46
47         createComment(builder, genType.getComment(), indent);
48
49         if (!genType.getAnnotations().isEmpty()) {
50             final List<AnnotationType> annotations = genType.getAnnotations();
51             appendAnnotations(builder, annotations);
52             builder.append(NL);
53         }
54
55         if (isIdentity) {
56             if (!(CLASS.equals(type))) {
57                 throw new IllegalArgumentException("'identity' has to be generated as a class");
58             }
59             builder.append(PUBLIC + GAP + ABSTRACT + GAP + type + GAP + genType.getName() + GAP);
60         } else {
61             builder.append(PUBLIC + GAP + type + GAP + genType.getName() + GAP);
62         }
63
64         if (genType instanceof GeneratedTransferObject) {
65             GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
66
67             if (genTO.getExtends() != null) {
68                 builder.append(EXTENDS + GAP);
69                 String gtoString = getExplicitType(genTO.getExtends(), availableImports, currentPkg);
70                 builder.append(gtoString + GAP);
71             }
72         }
73
74         final List<Type> genImplements = genType.getImplements();
75         if (!genImplements.isEmpty()) {
76             if (genType instanceof GeneratedTransferObject) {
77                 builder.append(IMPLEMENTS + GAP);
78             } else {
79                 builder.append(EXTENDS + GAP);
80             }
81             builder.append(getExplicitType(genImplements.get(0), availableImports, currentPkg));
82
83             for (int i = 1; i < genImplements.size(); ++i) {
84                 builder.append(", ");
85                 builder.append(getExplicitType(genImplements.get(i), availableImports, currentPkg));
86             }
87         }
88         builder.append(GAP + LCB);
89         return builder.toString();
90     }
91
92     private static StringBuilder appendAnnotations(final StringBuilder builder, final List<AnnotationType> annotations) {
93         if ((builder != null) && (annotations != null)) {
94             for (final AnnotationType annotation : annotations) {
95                 builder.append("@");
96                 builder.append(annotation.getPackageName());
97                 builder.append(".");
98                 builder.append(annotation.getName());
99
100                 if (annotation.containsParameters()) {
101                     builder.append("(");
102                     final List<AnnotationType.Parameter> parameters = annotation.getParameters();
103                     appendAnnotationParams(builder, parameters);
104                     builder.append(")");
105                 }
106             }
107         }
108         return builder;
109     }
110
111     private static StringBuilder appendAnnotationParams(final StringBuilder builder,
112             final List<AnnotationType.Parameter> parameters) {
113         if (parameters != null) {
114             int i = 0;
115             for (final AnnotationType.Parameter param : parameters) {
116                 if (param == null) {
117                     continue;
118                 }
119                 if (i > 0) {
120                     builder.append(", ");
121                 }
122                 final String paramName = param.getName();
123                 if (param.getValue() != null) {
124                     builder.append(paramName);
125                     builder.append(" = ");
126                     builder.append(param.getValue());
127                 } else {
128                     builder.append(paramName);
129                     builder.append(" = {");
130                     final List<String> values = param.getValues();
131                     builder.append(values.get(0));
132                     for (int j = 1; j < values.size(); ++j) {
133                         builder.append(", ");
134                         builder.append(values.get(j));
135                     }
136                     builder.append("}");
137                 }
138                 i++;
139             }
140         }
141         return builder;
142     }
143
144     public static String createConstant(final Constant constant, final String indent,
145             final Map<String, String> availableImports, final String currentPkg) {
146         final StringBuilder builder = new StringBuilder();
147         if (constant == null)
148             throw new IllegalArgumentException();
149         builder.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP);
150         builder.append(getExplicitType(constant.getType(), availableImports, currentPkg) + GAP + constant.getName());
151         builder.append(GAP + "=" + GAP);
152         final Object constValue = constant.getValue();
153
154         if (constant.getName().equals(TypeConstants.PATTERN_CONSTANT_NAME)) {
155             if (constant.getName() == null || constant.getType() == null || constant.getValue() == null)
156                 throw new IllegalArgumentException();
157             if (constValue instanceof List) {
158                 builder.append("Arrays.asList" + LB);
159                 final List<?> constantValues = (List<?>) constValue;
160                 int stringsCount = 0;
161                 for (Object value : constantValues) {
162                     if (value instanceof String) {
163                         if (stringsCount > 0) {
164                             builder.append(COMMA);
165                         }
166                         stringsCount++;
167                         builder.append(DOUBLE_QUOTE + (String) value + DOUBLE_QUOTE);
168                     }
169                 }
170                 builder.append(RB);
171             }
172         } else {
173             builder.append(constant.getValue());
174         }
175         builder.append(SC);
176
177         return builder.toString();
178     }
179
180     public static String createField(final GeneratedProperty property, final String indent,
181             final Map<String, String> availableImports, final String currentPkg) {
182         final StringBuilder builder = new StringBuilder();
183         if (!property.getAnnotations().isEmpty()) {
184             final List<AnnotationType> annotations = property.getAnnotations();
185             appendAnnotations(builder, annotations);
186             builder.append(NL);
187         }
188         builder.append(indent + PRIVATE + GAP);
189         builder.append(getExplicitType(property.getReturnType(), availableImports, currentPkg) + GAP
190                 + property.getName());
191         builder.append(SC);
192         return builder.toString();
193     }
194
195     /**
196      * Create method declaration in interface.
197      * 
198      * @param method
199      * @param indent
200      * @return
201      */
202     public static String createMethodDeclaration(final MethodSignature method, final String indent,
203             Map<String, String> availableImports, final String currentPkg) {
204         final StringBuilder builder = new StringBuilder();
205
206         if (method == null) {
207             throw new IllegalArgumentException("Method Signature parameter MUST be specified and cannot be NULL!");
208         }
209
210         final String comment = method.getComment();
211         final String name = method.getName();
212         if (name == null) {
213             throw new IllegalStateException("Method Name cannot be NULL!");
214         }
215
216         final Type type = method.getReturnType();
217         if (type == null) {
218             throw new IllegalStateException("Method Return type cannot be NULL!");
219         }
220
221         final List<Parameter> parameters = method.getParameters();
222
223         createComment(builder, comment, indent);
224         builder.append(NL);
225         builder.append(indent);
226
227         if (!method.getAnnotations().isEmpty()) {
228             final List<AnnotationType> annotations = method.getAnnotations();
229             appendAnnotations(builder, annotations);
230             builder.append(NL);
231         }
232
233         builder.append(indent + getExplicitType(type, availableImports, currentPkg) + GAP + name);
234         builder.append(LB);
235         for (int i = 0; i < parameters.size(); i++) {
236             Parameter p = parameters.get(i);
237             String separator = COMMA;
238             if (i + 1 == parameters.size()) {
239                 separator = "";
240             }
241             builder.append(getExplicitType(p.getType(), availableImports, currentPkg) + GAP + p.getName() + separator);
242         }
243         builder.append(RB);
244         builder.append(SC);
245
246         return builder.toString();
247     }
248
249     public static String createConstructor(GeneratedTransferObject genTransferObject, final String indent,
250             final Map<String, String> availableImports, boolean isIdentity) {
251         final StringBuilder builder = new StringBuilder();
252
253         final String currentPkg = genTransferObject.getPackageName();
254         final List<GeneratedProperty> properties = genTransferObject.getProperties();
255         final List<GeneratedProperty> ctorParams = new ArrayList<GeneratedProperty>();
256         if (properties != null) {
257             for (final GeneratedProperty property : properties) {
258                 if (property.isReadOnly()) {
259                     ctorParams.add(property);
260                 }
261             }
262         }
263
264         builder.append(indent);
265         builder.append(isIdentity ? PROTECTED : PUBLIC);
266         builder.append(GAP);
267         builder.append(genTransferObject.getName());
268         builder.append(LB);
269
270         if (!ctorParams.isEmpty()) {
271             builder.append(getExplicitType(ctorParams.get(0).getReturnType(), availableImports, currentPkg));
272             builder.append(" ");
273             builder.append(ctorParams.get(0).getName());
274             for (int i = 1; i < ctorParams.size(); ++i) {
275                 final GeneratedProperty param = ctorParams.get(i);
276                 builder.append(", ");
277                 builder.append(getExplicitType(param.getReturnType(), availableImports, currentPkg));
278                 builder.append(GAP);
279                 builder.append(param.getName());
280             }
281         }
282         builder.append(RB + GAP + LCB + NL + indent + TAB + "super();" + NL);
283         if (!ctorParams.isEmpty()) {
284             for (final GeneratedProperty property : ctorParams) {
285                 builder.append(indent);
286                 builder.append(TAB);
287                 builder.append("this.");
288                 builder.append(property.getName());
289                 builder.append(" = ");
290                 builder.append(property.getName());
291                 builder.append(SC);
292                 builder.append(NL);
293             }
294         }
295         List<Constant> consts = genTransferObject.getConstantDefinitions();
296         for (Constant con : consts) {
297             if (con.getName() == null || con.getType() == null || con.getValue() == null)
298                 continue;
299             if (con.getName().equals(TypeConstants.PATTERN_CONSTANT_NAME)) {
300                 Object values = con.getValue();
301                 if (values instanceof List) {
302                     for (Object regEx : (List<?>) values) {
303                         if (regEx instanceof String) {
304                             builder.append(indent + TAB + "for (String regEx : " + TypeConstants.PATTERN_CONSTANT_NAME
305                                     + ") {" + NL);
306                             builder.append(indent + TAB + TAB + "this." + MEMBER_PATTERN_LIST
307                                     + ".add(Pattern.compile(regEx))" + SC + NL);
308                             builder.append(indent + TAB + RCB + NL);
309
310                             break;
311                         }
312                     }
313
314                 }
315             }
316
317         }
318
319         builder.append(indent);
320         builder.append(RCB);
321         return builder.toString();
322     }
323
324     public static String createGetter(final GeneratedProperty property, final String indent,
325             final Map<String, String> availableImports, final String currentPkg) {
326         final StringBuilder builder = new StringBuilder();
327
328         final Type type = property.getReturnType();
329         final String varName = property.getName();
330         final char first = Character.toUpperCase(varName.charAt(0));
331         final String methodName = "get" + first + varName.substring(1);
332
333         builder.append(indent + PUBLIC + GAP + getExplicitType(type, availableImports, currentPkg) + GAP + methodName);
334         builder.append(LB + RB + LCB + NL);
335
336         String currentIndent = indent + TAB;
337
338         builder.append(currentIndent + "return " + varName + SC + NL);
339
340         builder.append(indent + RCB);
341         return builder.toString();
342     }
343
344     public static String createSetter(final GeneratedProperty property, final String indent,
345             final Map<String, String> availableImports, final String currentPkg) {
346         final StringBuilder builder = new StringBuilder();
347
348         final Type type = property.getReturnType();
349         final String varName = property.getName();
350         final char first = Character.toUpperCase(varName.charAt(0));
351         final String methodName = "set" + first + varName.substring(1);
352
353         builder.append(indent + PUBLIC + GAP + "void" + GAP + methodName);
354         builder.append(LB + getExplicitType(type, availableImports, currentPkg) + GAP + varName + RB + LCB + NL);
355         String currentIndent = indent + TAB;
356         builder.append(currentIndent + "this." + varName + " = " + varName + SC + NL);
357         builder.append(indent + RCB);
358         return builder.toString();
359     }
360
361     public static String createHashCode(final List<GeneratedProperty> properties, final String indent) {
362         StringBuilder builder = new StringBuilder();
363         builder.append(indent + "public int hashCode() {" + NL);
364         builder.append(indent + TAB + "final int prime = 31;" + NL);
365         builder.append(indent + TAB + "int result = 1;" + NL);
366
367         for (GeneratedProperty property : properties) {
368             String fieldName = property.getName();
369             builder.append(indent + TAB + "result = prime * result + ((" + fieldName + " == null) ? 0 : " + fieldName
370                     + ".hashCode());" + NL);
371         }
372
373         builder.append(indent + TAB + "return result;" + NL);
374         builder.append(indent + RCB + NL);
375         return builder.toString();
376     }
377
378     public static String createEquals(final GeneratedTransferObject type, final List<GeneratedProperty> properties,
379             final String indent) {
380         final StringBuilder builder = new StringBuilder();
381         final String indent1 = indent + TAB;
382         final String indent2 = indent1 + TAB;
383         final String indent3 = indent2 + TAB;
384
385         builder.append(indent + "public boolean equals(Object obj) {" + NL);
386         builder.append(indent1 + "if (this == obj) {" + NL);
387         builder.append(indent2 + "return true;" + NL);
388         builder.append(indent1 + "}" + NL);
389         builder.append(indent1 + "if (obj == null) {" + NL);
390         builder.append(indent2 + "return false;" + NL);
391         builder.append(indent1 + "}" + NL);
392         builder.append(indent1 + "if (getClass() != obj.getClass()) {" + NL);
393         builder.append(indent2 + "return false;" + NL);
394         builder.append(indent1 + "}" + NL);
395
396         String typeStr = type.getName();
397         builder.append(indent1 + typeStr + " other = (" + typeStr + ") obj;" + NL);
398
399         for (final GeneratedProperty property : properties) {
400             String fieldName = property.getName();
401             builder.append(indent1 + "if (" + fieldName + " == null) {" + NL);
402             builder.append(indent2 + "if (other." + fieldName + " != null) {" + NL);
403             builder.append(indent3 + "return false;" + NL);
404             builder.append(indent2 + "}" + NL);
405             builder.append(indent1 + "} else if (!" + fieldName + ".equals(other." + fieldName + ")) {" + NL);
406             builder.append(indent2 + "return false;" + NL);
407             builder.append(indent1 + "}" + NL);
408         }
409
410         builder.append(indent1 + "return true;" + NL);
411         builder.append(indent + RCB + NL);
412         return builder.toString();
413     }
414
415     public static String createToString(final GeneratedTransferObject type, final List<GeneratedProperty> properties,
416             final String indent) {
417         StringBuilder builder = new StringBuilder();
418         builder.append(indent);
419         builder.append("public String toString() {");
420         builder.append(NL);
421         builder.append(indent);
422         builder.append(TAB);
423         builder.append("StringBuilder builder = new StringBuilder();");
424         builder.append(NL);
425         builder.append(indent);
426         builder.append(TAB);
427         builder.append("builder.append(\"");
428         builder.append(type.getName());
429         builder.append(" [");
430
431         boolean first = true;
432         for (final GeneratedProperty property : properties) {
433             if (first) {
434                 builder.append(property.getName());
435                 builder.append("=\");");
436                 builder.append(NL);
437                 builder.append(indent);
438                 builder.append(TAB);
439                 builder.append("builder.append(");
440                 builder.append(property.getName());
441                 builder.append(");");
442                 first = false;
443             } else {
444                 builder.append(NL);
445                 builder.append(indent);
446                 builder.append(TAB);
447                 builder.append("builder.append(\", ");
448                 builder.append(property.getName());
449                 builder.append("=\");");
450                 builder.append(NL);
451                 builder.append(indent);
452                 builder.append(TAB);
453                 builder.append("builder.append(");
454                 builder.append(property.getName());
455                 builder.append(");");
456             }
457         }
458         builder.append(NL);
459         builder.append(indent);
460         builder.append(TAB);
461         builder.append("builder.append(\"]\");");
462         builder.append(NL);
463         builder.append(indent);
464         builder.append(TAB);
465         builder.append("return builder.toString();");
466
467         builder.append(NL);
468         builder.append(indent);
469         builder.append(RCB);
470         builder.append(NL);
471         return builder.toString();
472     }
473
474     public static String createEnum(final Enumeration enumeration, final String indent) {
475         if (enumeration == null || indent == null)
476             throw new IllegalArgumentException();
477         final StringBuilder builder = new StringBuilder(indent + PUBLIC + GAP + ENUM + GAP + enumeration.getName()
478                 + GAP + LCB + NL);
479
480         String separator = COMMA + NL;
481         final List<Pair> values = enumeration.getValues();
482
483         for (int i = 0; i < values.size(); i++) {
484             if (i + 1 == values.size()) {
485                 separator = SC;
486             }
487             builder.append(indent + TAB + values.get(i).getName() + LB + values.get(i).getValue() + RB + separator);
488         }
489         builder.append(NL);
490         builder.append(NL);
491         final String ENUMERATION_NAME = "value";
492         final String ENUMERATION_TYPE = "int";
493         builder.append(indent + TAB + ENUMERATION_TYPE + GAP + ENUMERATION_NAME + SC);
494         builder.append(NL);
495         builder.append(indent + TAB + PRIVATE + GAP + enumeration.getName() + LB + ENUMERATION_TYPE + GAP
496                 + ENUMERATION_NAME + RB + GAP + LCB + NL);
497         builder.append(indent + TAB + TAB + "this." + ENUMERATION_NAME + GAP + "=" + GAP + ENUMERATION_NAME + SC + NL);
498         builder.append(indent + TAB + RCB + NL);
499
500         builder.append(indent + RCB);
501         builder.append(NL);
502         return builder.toString();
503     }
504
505     private static String getExplicitType(final Type type, final Map<String, String> imports, final String currentPkg) {
506         if (type == null) {
507             throw new IllegalArgumentException("Type parameter MUST be specified and cannot be NULL!");
508         }
509         if (type.getName() == null) {
510             throw new IllegalArgumentException("Type name cannot be NULL!");
511         }
512         if (type.getPackageName() == null) {
513             throw new IllegalArgumentException("Type cannot have Package Name referenced as NULL!");
514         }
515         if (imports == null) {
516             throw new IllegalArgumentException("Imports Map cannot be NULL!");
517         }
518
519         final String typePackageName = type.getPackageName();
520         final String typeName = type.getName();
521         final String importedPackageName = imports.get(typeName);
522         if (typePackageName.equals(importedPackageName) || typePackageName.equals(currentPkg)) {
523             final StringBuilder builder = new StringBuilder(type.getName());
524             if (type instanceof ParameterizedType) {
525                 final ParameterizedType pType = (ParameterizedType) type;
526                 final Type[] pTypes = pType.getActualTypeArguments();
527                 builder.append("<");
528                 builder.append(getParameters(pTypes, imports, currentPkg));
529                 builder.append(">");
530             }
531             if (builder.toString().equals("Void")) {
532                 return "void";
533             }
534             return builder.toString();
535         } else {
536             final StringBuilder builder = new StringBuilder();
537             if (typePackageName.startsWith("java.lang")) {
538                 builder.append(type.getName());
539             } else {
540                 if (!typePackageName.isEmpty()) {
541                     builder.append(typePackageName + "." + type.getName());
542                 } else {
543                     builder.append(type.getName());
544                 }
545             }
546             if (type instanceof ParameterizedType) {
547                 final ParameterizedType pType = (ParameterizedType) type;
548                 final Type[] pTypes = pType.getActualTypeArguments();
549                 builder.append("<");
550                 builder.append(getParameters(pTypes, imports, currentPkg));
551                 builder.append(">");
552             }
553             if (builder.toString().equals("Void")) {
554                 return "void";
555             }
556             return builder.toString();
557         }
558     }
559
560     private static String getParameters(final Type[] pTypes, Map<String, String> availableImports, String currentPkg) {
561         final StringBuilder builder = new StringBuilder();
562         for (int i = 0; i < pTypes.length; i++) {
563             final Type t = pTypes[i];
564
565             String separator = COMMA;
566             if (i == (pTypes.length - 1)) {
567                 separator = "";
568             }
569
570             String wildcardParam = "";
571             if (t instanceof WildcardType) {
572                 wildcardParam = "? extends ";
573             }
574             builder.append(wildcardParam + getExplicitType(t, availableImports, currentPkg) + separator);
575         }
576         return builder.toString();
577     }
578
579     private static void createComment(final StringBuilder builder, final String comment, final String indent) {
580         if (comment != null && comment.length() > 0) {
581             builder.append(indent + "/*" + NL);
582             builder.append(indent + comment + NL);
583             builder.append(indent + "*/" + NL);
584         }
585     }
586
587     public static Map<String, String> createImports(final GeneratedType genType) {
588         if (genType == null) {
589             throw new IllegalArgumentException("Generated Type cannot be NULL!");
590         }
591
592         final Map<String, String> imports = new LinkedHashMap<>();
593         final List<Constant> constants = genType.getConstantDefinitions();
594         final List<MethodSignature> methods = genType.getMethodDefinitions();
595         final List<Type> impl = genType.getImplements();
596
597         // IMPLEMENTATIONS
598         if (impl != null) {
599             for (final Type type : impl) {
600                 putTypeIntoImports(genType, type, imports);
601             }
602         }
603
604         // CONSTANTS
605         if (constants != null) {
606             for (final Constant constant : constants) {
607                 final Type constantType = constant.getType();
608                 putTypeIntoImports(genType, constantType, imports);
609             }
610         }
611
612         // REGULAR EXPRESSION
613         if (genType instanceof GeneratedTransferObject) {
614             if (isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, (GeneratedTransferObject) genType)) {
615                 putTypeIntoImports(genType, Types.typeForClass(java.util.regex.Pattern.class), imports);
616                 putTypeIntoImports(genType, Types.typeForClass(java.util.Arrays.class), imports);
617                 putTypeIntoImports(genType, Types.typeForClass(java.util.ArrayList.class), imports);
618             }
619         }
620
621         // METHODS
622         if (methods != null) {
623             for (final MethodSignature method : methods) {
624                 final Type methodReturnType = method.getReturnType();
625                 putTypeIntoImports(genType, methodReturnType, imports);
626                 for (final MethodSignature.Parameter methodParam : method.getParameters()) {
627                     putTypeIntoImports(genType, methodParam.getType(), imports);
628                 }
629             }
630         }
631
632         // PROPERTIES
633         if (genType instanceof GeneratedTransferObject) {
634             final GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
635             final List<GeneratedProperty> properties = genTO.getProperties();
636             if (properties != null) {
637                 for (GeneratedProperty property : properties) {
638                     final Type propertyType = property.getReturnType();
639                     putTypeIntoImports(genType, propertyType, imports);
640                 }
641             }
642         }
643
644         return imports;
645     }
646
647     private static void putTypeIntoImports(final GeneratedType parentGenType, final Type type,
648             final Map<String, String> imports) {
649         if (parentGenType == null) {
650             throw new IllegalArgumentException("Parent Generated Type parameter MUST be specified and cannot be "
651                     + "NULL!");
652         }
653         if (parentGenType.getName() == null) {
654             throw new IllegalArgumentException("Parent Generated Type name cannot be NULL!");
655         }
656         if (parentGenType.getPackageName() == null) {
657             throw new IllegalArgumentException("Parent Generated Type cannot have Package Name referenced as NULL!");
658         }
659         if (type == null) {
660             throw new IllegalArgumentException("Type parameter MUST be specified and cannot be NULL!");
661         }
662         if (type.getName() == null) {
663             throw new IllegalArgumentException("Type name cannot be NULL!");
664         }
665         if (type.getPackageName() == null) {
666             throw new IllegalArgumentException("Type cannot have Package Name referenced as NULL!");
667         }
668
669         final String typeName = type.getName();
670         final String typePackageName = type.getPackageName();
671         final String parentTypeName = parentGenType.getName();
672         final String parentTypePackageName = parentGenType.getPackageName();
673         if (typeName.equals(parentTypeName) || typePackageName.startsWith("java.lang")
674                 || typePackageName.equals(parentTypePackageName) || typePackageName.isEmpty()) {
675             return;
676         }
677         if (!imports.containsKey(typeName)) {
678             imports.put(typeName, typePackageName);
679         }
680         if (type instanceof ParameterizedType) {
681             final ParameterizedType paramType = (ParameterizedType) type;
682             final Type[] params = paramType.getActualTypeArguments();
683             for (Type param : params) {
684                 putTypeIntoImports(parentGenType, param, imports);
685             }
686         }
687     }
688
689     public static List<String> createImportLines(final Map<String, String> imports) {
690         final List<String> importLines = new ArrayList<>();
691
692         for (Map.Entry<String, String> entry : imports.entrySet()) {
693             final String typeName = entry.getKey();
694             final String packageName = entry.getValue();
695             importLines.add("import " + packageName + "." + typeName + SC);
696         }
697         return importLines;
698     }
699
700     public static boolean isConstantInTO(String constName, GeneratedTransferObject genTO) {
701         if (constName == null || genTO == null)
702             throw new IllegalArgumentException();
703         List<Constant> consts = genTO.getConstantDefinitions();
704         for (Constant cons : consts) {
705             if (cons.getName().equals(constName)) {
706                 return true;
707             }
708
709         }
710         return false;
711     }
712 }