+ public static Map<String, String> createImports(final GeneratedType genType) {
+ if (genType == null) {
+ throw new IllegalArgumentException("Generated Type cannot be NULL!");
+ }
+
+ final Map<String, String> imports = new LinkedHashMap<>();
+ final List<Constant> constants = genType.getConstantDefinitions();
+ final List<MethodSignature> methods = genType.getMethodDefinitions();
+ final List<Type> impl = genType.getImplements();
+
+ // IMPLEMENTATIONS
+ if (impl != null) {
+ for (final Type type : impl) {
+ putTypeIntoImports(genType, type, imports);
+ }
+ }
+
+ // CONSTANTS
+ if (constants != null) {
+ for (final Constant constant : constants) {
+ final Type constantType = constant.getType();
+ putTypeIntoImports(genType, constantType, imports);
+ }
+ }
+
+ // REGULAR EXPRESSION
+ if (genType instanceof GeneratedTransferObject) {
+ if (isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, (GeneratedTransferObject) genType)) {
+ putTypeIntoImports(genType, Types.typeForClass(java.util.regex.Pattern.class), imports);
+ putTypeIntoImports(genType, Types.typeForClass(java.util.Arrays.class), imports);
+ putTypeIntoImports(genType, Types.typeForClass(java.util.ArrayList.class), imports);
+ }
+ }
+
+ // METHODS
+ if (methods != null) {
+ for (final MethodSignature method : methods) {
+ final Type methodReturnType = method.getReturnType();
+ putTypeIntoImports(genType, methodReturnType, imports);
+ for (final MethodSignature.Parameter methodParam : method.getParameters()) {
+ putTypeIntoImports(genType, methodParam.getType(), imports);
+ }
+ }
+ }
+
+ // PROPERTIES
+ if (genType instanceof GeneratedTransferObject) {
+ final GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
+ final List<GeneratedProperty> properties = genTO.getProperties();
+ if (properties != null) {
+ for (GeneratedProperty property : properties) {
+ final Type propertyType = property.getReturnType();
+ putTypeIntoImports(genType, propertyType, imports);
+ }
+ }
+ }
+
+ return imports;
+ }
+
+ private static void putTypeIntoImports(final GeneratedType parentGenType, final Type type,
+ final Map<String, String> imports) {
+ if (parentGenType == null) {
+ throw new IllegalArgumentException("Parent Generated Type parameter MUST be specified and cannot be "
+ + "NULL!");
+ }
+ if (parentGenType.getName() == null) {
+ throw new IllegalArgumentException("Parent Generated Type name cannot be NULL!");
+ }
+ if (parentGenType.getPackageName() == null) {
+ throw new IllegalArgumentException("Parent Generated Type cannot have Package Name referenced as NULL!");
+ }
+ if (type == null) {
+ throw new IllegalArgumentException("Type parameter MUST be specified and cannot be NULL!");
+ }
+ if (type.getName() == null) {
+ throw new IllegalArgumentException("Type name cannot be NULL!");
+ }
+ if (type.getPackageName() == null) {
+ throw new IllegalArgumentException("Type cannot have Package Name referenced as NULL!");
+ }
+
+ final String typeName = type.getName();
+ final String typePackageName = type.getPackageName();
+ final String parentTypeName = parentGenType.getName();
+ final String parentTypePackageName = parentGenType.getPackageName();
+ if (typeName.equals(parentTypeName) || typePackageName.startsWith("java.lang")
+ || typePackageName.equals(parentTypePackageName) || typePackageName.isEmpty()) {
+ return;
+ }
+ if (!imports.containsKey(typeName)) {
+ imports.put(typeName, typePackageName);
+ }
+ if (type instanceof ParameterizedType) {
+ final ParameterizedType paramType = (ParameterizedType) type;
+ final Type[] params = paramType.getActualTypeArguments();
+ for (Type param : params) {
+ putTypeIntoImports(parentGenType, param, imports);
+ }
+ }
+ }
+
+ public static List<String> createImportLines(final Map<String, String> imports) {
+ final List<String> importLines = new ArrayList<>();
+
+ for (Map.Entry<String, String> entry : imports.entrySet()) {
+ final String typeName = entry.getKey();
+ final String packageName = entry.getValue();
+ importLines.add("import " + packageName + "." + typeName + SC);
+ }
+ return importLines;
+ }
+
+ public static boolean isConstantInTO(String constName, GeneratedTransferObject genTO) {
+ if (constName == null || genTO == null)
+ throw new IllegalArgumentException();
+ List<Constant> consts = genTO.getConstantDefinitions();
+ for (Constant cons : consts) {
+ if (cons.getName().equals(constName)) {
+ return true;
+ }
+
+ }
+ return false;
+ }