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