+ public static Map<String, String> createImports(GeneratedType genType) {
+ if (genType == null) {
+ throw new IllegalArgumentException("Generated Type cannot be NULL!");
+ }
+ final Map<String, String> imports = new LinkedHashMap<>();
+ List<GeneratedType> childGeneratedTypes = genType.getEnclosedTypes();
+ if (!childGeneratedTypes.isEmpty()) {
+ for (GeneratedType genTypeChild : childGeneratedTypes) {
+ imports.putAll(createImports(genTypeChild));
+ }
+ }
+
+ 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;
+ }
+
+ public static Map<String, String> createChildImports(GeneratedType genType) {
+ Map<String, String> childImports = new LinkedHashMap<>();
+ List<GeneratedType> childGeneratedTypes = genType.getEnclosedTypes();
+ if (!childGeneratedTypes.isEmpty()) {
+ for (GeneratedType genTypeChild : childGeneratedTypes) {
+ createChildImports(genTypeChild);
+ childImports.put(genTypeChild.getName(), genTypeChild.getPackageName());
+ }
+ }
+ return childImports;
+ }
+
+ 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 Map<String, String> innerTypeImports) {
+ final List<String> importLines = new ArrayList<>();
+
+ for (Map.Entry<String, String> entry : imports.entrySet()) {
+ final String typeName = entry.getKey();
+ final String packageName = entry.getValue();
+ if (innerTypeImports != null) {
+ String innerTypePackageName = innerTypeImports.get(typeName);
+ if (innerTypePackageName != null) {
+ if (innerTypePackageName.equals(packageName))
+ continue;
+ }
+ }
+ 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;
+ }
+
+ /**
+ * The method returns reference to highest (top parent) Generated Transfer
+ * Object.
+ *
+ * @param childTransportObject
+ * is generated transfer object which can be extended by other
+ * generated transfer object
+ * @return in first case that <code>childTransportObject</code> isn't
+ * extended then <code>childTransportObject</code> is returned. In
+ * second case the method is recursive called until first case.
+ */
+ private static GeneratedTransferObject getTopParrentTransportObject(GeneratedTransferObject childTransportObject) {
+ if (childTransportObject == null) {
+ throw new IllegalArgumentException("Parameter childTransportObject can't be null.");
+ }
+ if (childTransportObject.getExtends() == null) {
+ return childTransportObject;
+ } else {
+ return getTopParrentTransportObject(childTransportObject.getExtends());
+ }
+ }
+
+ /**
+ * The method returns the list of the properties of all extending generated
+ * transfer object from <code>genTO</code> to highest parent generated
+ * transfer object
+ *
+ * @param genTO
+ * @return the list of all properties from actual to highest parent
+ * generated transfer object. In case when extension exists the
+ * method is recursive called.
+ */
+ private static List<GeneratedProperty> getPropertiesOfAllParents(GeneratedTransferObject genTO) {
+ List<GeneratedProperty> propertiesOfAllParents = new ArrayList<GeneratedProperty>();
+ if (genTO != null) {
+ final List<GeneratedProperty> allPropertiesOfTO = genTO.getProperties();
+ List<GeneratedProperty> readOnlyPropertiesOfTO = resolveReadOnlyPropertiesFromTO(allPropertiesOfTO);
+ propertiesOfAllParents.addAll(readOnlyPropertiesOfTO);
+ if (genTO.getExtends() != null) {
+ propertiesOfAllParents.addAll(getPropertiesOfAllParents(genTO.getExtends()));
+ }
+ }
+ return propertiesOfAllParents;
+ }
+
+ public static String createStaticInicializationBlock(GeneratedTransferObject genTransferObject, String indent) {
+
+ final StringBuilder builder = new StringBuilder();
+
+ List<Constant> constants = genTransferObject.getConstantDefinitions();
+ for (Constant constant : constants) {
+ if (constant.getName() == null || constant.getType() == null || constant.getValue() == null) {
+ continue;
+ }
+ if (constant.getName().equals(TypeConstants.PATTERN_CONSTANT_NAME)) {
+ final Object constValue = constant.getValue();
+ List<String> regularExpressions = new ArrayList<>();
+ if (constValue instanceof List) {
+ builder.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP + "List<String>" + GAP
+ + TypeConstants.PATTERN_CONSTANT_NAME + GAP + "=" + GAP + "Arrays.asList" + LB);
+ final List<?> constantValues = (List<?>) constValue;
+ int stringsCount = 0;
+ for (Object value : constantValues) {
+ if (value instanceof String) {
+ if (stringsCount > 0) {
+ builder.append(COMMA);
+ }
+ stringsCount++;
+ regularExpressions.add((String) value);
+ builder.append(DOUBLE_QUOTE + (String) value + DOUBLE_QUOTE);
+ }
+ }
+ builder.append(RB + SC + NL);
+ }
+ builder.append(indent + PRIVATE + GAP + STATIC + GAP + FINAL + GAP + "List<Pattern>" + GAP
+ + MEMBER_PATTERN_LIST + GAP + ASSIGN + GAP + "new ArrayList<Pattern>()" + GAP + SC + NL + NL);
+
+ if (!regularExpressions.isEmpty()) {
+ builder.append(indent + STATIC + LCB + NL);
+ builder.append(indent + TAB + "for (String regEx : " + TypeConstants.PATTERN_CONSTANT_NAME + ") {"
+ + NL);
+ builder.append(indent + TAB + TAB + MEMBER_PATTERN_LIST + ".add(Pattern.compile(regEx))" + SC + NL);
+ builder.append(indent + TAB + RCB + NL);
+ builder.append(indent + RCB + NL + NL);
+ }
+
+ }
+ }
+ return builder.toString();
+ }