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