-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-package org.opendaylight.yangtools.sal.java.api.generator;\r
-\r
-import static org.opendaylight.yangtools.sal.java.api.generator.Constants.COMMA;\r
-\r
-import java.util.ArrayList;\r
-import java.util.LinkedHashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-import org.opendaylight.yangtools.binding.generator.util.TypeConstants;\r
-import org.opendaylight.yangtools.binding.generator.util.Types;\r
-import org.opendaylight.yangtools.sal.binding.model.api.Constant;\r
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;\r
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;\r
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;\r
-import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;\r
-import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;\r
-import org.opendaylight.yangtools.sal.binding.model.api.Type;\r
-import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;\r
-\r
-public final class GeneratorUtil {\r
-\r
- /**\r
- * It doesn't have the sense to create the instances of this class.\r
- */\r
- private GeneratorUtil() {\r
- }\r
-\r
- /**\r
- * Returns the map of imports. The map maps the type name to the package\r
- * name. To the map are added packages for <code>genType</code> and for all\r
- * enclosed types, constants, methods (parameter types, return values),\r
- * implemented types.\r
- * \r
- * @param genType\r
- * generated type for which the map of the imports is created\r
- * @return map of the necessary imports\r
- * @throws IllegalArgumentException\r
- * if <code>genType</code> equals <code>null</code>\r
- */\r
- public static Map<String, String> createImports(GeneratedType genType) {\r
- if (genType == null) {\r
- throw new IllegalArgumentException("Generated Type cannot be NULL!");\r
- }\r
- final Map<String, String> imports = new LinkedHashMap<>();\r
-\r
- List<GeneratedType> childGeneratedTypes = genType.getEnclosedTypes();\r
- if (!childGeneratedTypes.isEmpty()) {\r
- for (GeneratedType genTypeChild : childGeneratedTypes) {\r
- imports.putAll(createImports(genTypeChild));\r
- }\r
- }\r
-\r
- final List<Constant> constants = genType.getConstantDefinitions();\r
- final List<MethodSignature> methods = genType.getMethodDefinitions();\r
- final List<Type> impl = genType.getImplements();\r
-\r
- // IMPLEMENTATIONS\r
- if (impl != null) {\r
- for (final Type type : impl) {\r
- putTypeIntoImports(genType, type, imports);\r
- }\r
- }\r
-\r
- // CONSTANTS\r
- if (constants != null) {\r
- for (final Constant constant : constants) {\r
- final Type constantType = constant.getType();\r
- putTypeIntoImports(genType, constantType, imports);\r
- }\r
- }\r
-\r
- // REGULAR EXPRESSION\r
- if (genType instanceof GeneratedTransferObject) {\r
- if (isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, (GeneratedTransferObject) genType)) {\r
- putTypeIntoImports(genType, Types.typeForClass(java.util.regex.Pattern.class), imports);\r
- putTypeIntoImports(genType, Types.typeForClass(java.util.Arrays.class), imports);\r
- putTypeIntoImports(genType, Types.typeForClass(java.util.ArrayList.class), imports);\r
- }\r
- }\r
-\r
- // METHODS\r
- if (methods != null) {\r
- for (final MethodSignature method : methods) {\r
- final Type methodReturnType = method.getReturnType();\r
- putTypeIntoImports(genType, methodReturnType, imports);\r
- for (final MethodSignature.Parameter methodParam : method.getParameters()) {\r
- putTypeIntoImports(genType, methodParam.getType(), imports);\r
- }\r
- }\r
- }\r
-\r
- // PROPERTIES\r
- if (genType instanceof GeneratedTransferObject) {\r
- final GeneratedTransferObject genTO = (GeneratedTransferObject) genType;\r
- final List<GeneratedProperty> properties = genTO.getProperties();\r
- if (properties != null) {\r
- for (GeneratedProperty property : properties) {\r
- final Type propertyType = property.getReturnType();\r
- putTypeIntoImports(genType, propertyType, imports);\r
- }\r
- }\r
- }\r
-\r
- return imports;\r
- }\r
-\r
- /**\r
- * Evaluates if it is necessary to add the package name for\r
- * <code>type</code> to the map of imports for <code>parentGenType</code>.\r
- * If it is so the package name is saved to the map <code>imports</code>.\r
- * \r
- * @param parentGenType\r
- * generated type for which is the map of the necessary imports\r
- * built\r
- * @param type\r
- * JAVA <code>Type</code> for which is the necessary of the\r
- * package import evaluated\r
- * @param imports\r
- * map of the imports for <code>parentGenType</code>\r
- * @throws IllegalArgumentException\r
- * <ul>\r
- * <li>if the <code>parentGenType</code> equals\r
- * <code>null</code></li>\r
- * <li>if the name of <code>parentGenType</code> equals\r
- * <code>null</code></li>\r
- * <li>if the name of the package of <code>parentGenType</code>\r
- * equals <code>null</code></li>\r
- * <li>if the <code>type</code> equals <code>null</code></li>\r
- * <li>if the name of <code>type</code> equals <code>null</code>\r
- * </li>\r
- * <li>if the name of the package of <code>type</code> equals\r
- * <code>null</code></li>\r
- * </ul>\r
- */\r
- public static void putTypeIntoImports(final GeneratedType parentGenType, final Type type,\r
- final Map<String, String> imports) {\r
- if (parentGenType == null) {\r
- throw new IllegalArgumentException("Parent Generated Type parameter MUST be specified and cannot be "\r
- + "NULL!");\r
- }\r
- if (parentGenType.getName() == null) {\r
- throw new IllegalArgumentException("Parent Generated Type name cannot be NULL!");\r
- }\r
- if (parentGenType.getPackageName() == null) {\r
- throw new IllegalArgumentException("Parent Generated Type cannot have Package Name referenced as NULL!");\r
- }\r
- if (type == null) {\r
- throw new IllegalArgumentException("Type parameter MUST be specified and cannot be NULL!");\r
- }\r
- if (type.getName() == null) {\r
- throw new IllegalArgumentException("Type name cannot be NULL!");\r
- }\r
- if (type.getPackageName() == null) {\r
- throw new IllegalArgumentException("Type cannot have Package Name referenced as NULL!");\r
- }\r
-\r
- final String typeName = type.getName();\r
- final String typePackageName = type.getPackageName();\r
- final String parentTypeName = parentGenType.getName();\r
- final String parentTypePackageName = parentGenType.getPackageName();\r
- if (typeName.equals(parentTypeName) || typePackageName.startsWith("java.lang")\r
- || typePackageName.equals(parentTypePackageName) || typePackageName.isEmpty()) {\r
- return;\r
- }\r
- if (!imports.containsKey(typeName)) {\r
- imports.put(typeName, typePackageName);\r
- }\r
- if (type instanceof ParameterizedType) {\r
- final ParameterizedType paramType = (ParameterizedType) type;\r
- final Type[] params = paramType.getActualTypeArguments();\r
- for (Type param : params) {\r
- putTypeIntoImports(parentGenType, param, imports);\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Checks if the constant with the name <code>constName</code> is in the\r
- * list of the constant definition for <code>genTO</code>.\r
- * \r
- * @param constName\r
- * string with the name of constant which is sought\r
- * @param genTO\r
- * generated transfer object in which is <code>constName</code>\r
- * sought\r
- * @return boolean value\r
- * <ul>\r
- * <li>true - if <code>constName</code> is in the list of the\r
- * constant definition for <code>genTO</code></li>\r
- * <li>false - in other cases</li>\r
- * </ul>\r
- * @throws IllegalArgumentException\r
- * <ul>\r
- * <li>if <code>constName</code> equals <code>null</code></li>\r
- * <li>if <code>genTO</code> equals <code>null</code></li>\r
- * </ul>\r
- */\r
- public static boolean isConstantInTO(String constName, GeneratedTransferObject genTO) {\r
- if (constName == null || genTO == null)\r
- throw new IllegalArgumentException();\r
- List<Constant> consts = genTO.getConstantDefinitions();\r
- for (Constant cons : consts) {\r
- if (cons.getName().equals(constName)) {\r
- return true;\r
- }\r
- }\r
- return false;\r
- }\r
-\r
- /**\r
- * Creates the map which maps the type name to package name and contains\r
- * only package names for enclosed types of <code>genType</code> and\r
- * recursivelly their enclosed types.\r
- * \r
- * @param genType\r
- * JAVA <code>Type</code> for which is the map created\r
- * @return map of the package names for all the enclosed types and\r
- * recursivelly their enclosed types\r
- */\r
- public static Map<String, String> createChildImports(GeneratedType genType) {\r
- Map<String, String> childImports = new LinkedHashMap<>();\r
- List<GeneratedType> childGeneratedTypes = genType.getEnclosedTypes();\r
- if (!childGeneratedTypes.isEmpty()) {\r
- for (GeneratedType genTypeChild : childGeneratedTypes) {\r
- createChildImports(genTypeChild);\r
- childImports.put(genTypeChild.getName(), genTypeChild.getPackageName());\r
- }\r
- }\r
- return childImports;\r
- }\r
-\r
- /**\r
- * Builds the string which contains either the full path to the type\r
- * (package name with type) or only type name if the package is among\r
- * <code>imports</code>.\r
- * \r
- * @param parentGenType\r
- * generated type which contains <code>type</code>\r
- * @param type\r
- * JAVA <code>Type</code> for which is the string with type info\r
- * generated\r
- * @param imports\r
- * map of necessary imports for <code>parentGenType</code>\r
- * @return string with type name for <code>type</code> in the full format or\r
- * in the short format\r
- * @throws IllegalArgumentException\r
- * <ul>\r
- * <li>if the <code>type</code> equals <code>null</code></li>\r
- * <li>if the name of the <code>type</code> equals\r
- * <code>null</code></li>\r
- * <li>if the name of the package of the <code>type</code>\r
- * equals <code>null</code></li>\r
- * <li>if the <code>imports</code> equals <code>null</code></li>\r
- * </ul>\r
- */\r
- public static String getExplicitType(final GeneratedType parentGenType, final Type type,\r
- final Map<String, String> imports) {\r
- if (type == null) {\r
- throw new IllegalArgumentException("Type parameter MUST be specified and cannot be NULL!");\r
- }\r
- if (type.getName() == null) {\r
- throw new IllegalArgumentException("Type name cannot be NULL!");\r
- }\r
- if (type.getPackageName() == null) {\r
- throw new IllegalArgumentException("Type cannot have Package Name referenced as NULL!");\r
- }\r
- if (imports == null) {\r
- throw new IllegalArgumentException("Imports Map cannot be NULL!");\r
- }\r
-\r
- final String typePackageName = type.getPackageName();\r
- final String typeName = type.getName();\r
- final String importedPackageName = imports.get(typeName);\r
- if (typePackageName.equals(importedPackageName) || typePackageName.equals(parentGenType.getPackageName())) {\r
- final StringBuilder builder = new StringBuilder(type.getName());\r
- if (type instanceof ParameterizedType) {\r
- final ParameterizedType pType = (ParameterizedType) type;\r
- final Type[] pTypes = pType.getActualTypeArguments();\r
- builder.append("<");\r
- builder.append(getParameters(parentGenType, pTypes, imports));\r
- builder.append(">");\r
- }\r
- if (builder.toString().equals("Void")) {\r
- return "void";\r
- }\r
- return builder.toString();\r
- } else {\r
- final StringBuilder builder = new StringBuilder();\r
- if (typePackageName.startsWith("java.lang")) {\r
- builder.append(type.getName());\r
- } else {\r
- if (!typePackageName.isEmpty()) {\r
- builder.append(typePackageName + Constants.DOT + type.getName());\r
- } else {\r
- builder.append(type.getName());\r
- }\r
- }\r
- if (type.equals(Types.voidType())) {\r
- return "void";\r
- }\r
- if (type instanceof ParameterizedType) {\r
- final ParameterizedType pType = (ParameterizedType) type;\r
- final Type[] pTypes = pType.getActualTypeArguments();\r
- builder.append("<");\r
- builder.append(getParameters(parentGenType, pTypes, imports));\r
- builder.append(">");\r
- }\r
- return builder.toString();\r
- }\r
- }\r
-\r
- /**\r
- * Generates the string with all actual type parameters from\r
- * <code>pTypes</code>\r
- * \r
- * @param parentGenType\r
- * generated type for which is the JAVA code generated\r
- * @param pTypes\r
- * array of <code>Type</code> instances = actual type parameters\r
- * @param availableImports\r
- * map of imports for <code>parentGenType</code>\r
- * @return string with all actual type parameters from <code>pTypes</code>\r
- */\r
- private static String getParameters(final GeneratedType parentGenType, final Type[] pTypes,\r
- Map<String, String> availableImports) {\r
- final StringBuilder builder = new StringBuilder();\r
- for (int i = 0; i < pTypes.length; i++) {\r
- final Type t = pTypes[i];\r
-\r
- String separator = COMMA;\r
- if (i == (pTypes.length - 1)) {\r
- separator = "";\r
- }\r
-\r
- String wildcardParam = "";\r
- if (t.equals(Types.voidType())) {\r
- builder.append("java.lang.Void" + separator);\r
- continue;\r
- } else {\r
-\r
- if (t instanceof WildcardType) {\r
- wildcardParam = "? extends ";\r
- }\r
-\r
- builder.append(wildcardParam + getExplicitType(parentGenType, t, availableImports) + separator);\r
- }\r
- }\r
- return builder.toString();\r
- }\r
-\r
- /**\r
- * Returns the reference to highest (top parent) Generated Transfer Object.\r
- * \r
- * @param childTransportObject\r
- * is generated transfer object which can be extended by other\r
- * generated transfer object\r
- * @return in first case that <code>childTransportObject</code> isn't\r
- * extended then <code>childTransportObject</code> is returned. In\r
- * second case the method is recursive called until first case.\r
- * @throws IllegalArgumentException\r
- * if <code>childTransportObject</code> equals <code>null</code>\r
- */\r
- public static GeneratedTransferObject getTopParrentTransportObject(GeneratedTransferObject childTransportObject) {\r
- if (childTransportObject == null) {\r
- throw new IllegalArgumentException("Parameter childTransportObject can't be null.");\r
- }\r
- if (childTransportObject.getSuperType() == null) {\r
- return childTransportObject;\r
- } else {\r
- return getTopParrentTransportObject(childTransportObject.getSuperType());\r
- }\r
- }\r
-\r
- /**\r
- * Selects from input list of properties only those which have read only\r
- * attribute set to true.\r
- * \r
- * @param properties\r
- * list of properties of generated transfer object\r
- * @return subset of <code>properties</code> which have read only attribute\r
- * set to true\r
- */\r
- public static List<GeneratedProperty> resolveReadOnlyPropertiesFromTO(List<GeneratedProperty> properties) {\r
- List<GeneratedProperty> readOnlyProperties = new ArrayList<GeneratedProperty>();\r
- if (properties != null) {\r
- for (final GeneratedProperty property : properties) {\r
- if (property.isReadOnly()) {\r
- readOnlyProperties.add(property);\r
- }\r
- }\r
- }\r
- return readOnlyProperties;\r
- }\r
-\r
- /**\r
- * Returns the list of the read only properties of all extending generated\r
- * transfer object from <code>genTO</code> to highest parent generated\r
- * transfer object\r
- * \r
- * @param genTO\r
- * generated transfer object for which is the list of read only\r
- * properties generated\r
- * @return list of all read only properties from actual to highest parent\r
- * generated transfer object. In case when extension exists the\r
- * method is recursive called.\r
- */\r
- public static List<GeneratedProperty> getPropertiesOfAllParents(GeneratedTransferObject genTO) {\r
- List<GeneratedProperty> propertiesOfAllParents = new ArrayList<GeneratedProperty>();\r
- if (genTO.getSuperType() != null) {\r
- final List<GeneratedProperty> allPropertiesOfTO = genTO.getSuperType().getProperties();\r
- List<GeneratedProperty> readOnlyPropertiesOfTO = resolveReadOnlyPropertiesFromTO(allPropertiesOfTO);\r
- propertiesOfAllParents.addAll(readOnlyPropertiesOfTO);\r
- propertiesOfAllParents.addAll(getPropertiesOfAllParents(genTO.getSuperType()));\r
- }\r
- return propertiesOfAllParents;\r
- }\r
-\r
-}\r
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.sal.java.api.generator;
+
+import static org.opendaylight.yangtools.sal.java.api.generator.Constants.COMMA;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.opendaylight.yangtools.binding.generator.util.TypeConstants;
+import org.opendaylight.yangtools.binding.generator.util.Types;
+import org.opendaylight.yangtools.sal.binding.model.api.Constant;
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
+import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
+import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
+import org.opendaylight.yangtools.sal.binding.model.api.Type;
+import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;
+import com.google.common.base.Preconditions;
+
+public final class GeneratorUtil {
+
+ /**
+ * It doesn't have the sense to create the instances of this class.
+ */
+ private GeneratorUtil() {
+ }
+
+ /**
+ * Returns the map of imports. The map maps the type name to the package
+ * name. To the map are added packages for <code>genType</code> and for all
+ * enclosed types, constants, methods (parameter types, return values),
+ * implemented types.
+ *
+ * @param genType
+ * generated type for which the map of the imports is created
+ * @return map of the necessary imports
+ * @throws IllegalArgumentException
+ * if <code>genType</code> equals <code>null</code>
+ */
+ 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
+ && 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;
+ }
+
+ /**
+ * Evaluates if it is necessary to add the package name for
+ * <code>type</code> to the map of imports for <code>parentGenType</code>.
+ * If it is so the package name is saved to the map <code>imports</code>.
+ *
+ * @param parentGenType
+ * generated type for which is the map of the necessary imports
+ * built
+ * @param type
+ * JAVA <code>Type</code> for which is the necessary of the
+ * package import evaluated
+ * @param imports
+ * map of the imports for <code>parentGenType</code>
+ * @throws IllegalArgumentException
+ * <ul>
+ * <li>if the <code>parentGenType</code> equals
+ * <code>null</code></li>
+ * <li>if the name of <code>parentGenType</code> equals
+ * <code>null</code></li>
+ * <li>if the name of the package of <code>parentGenType</code>
+ * equals <code>null</code></li>
+ * <li>if the <code>type</code> equals <code>null</code></li>
+ * <li>if the name of <code>type</code> equals <code>null</code>
+ * </li>
+ * <li>if the name of the package of <code>type</code> equals
+ * <code>null</code></li>
+ * </ul>
+ */
+ public static void putTypeIntoImports(final GeneratedType parentGenType, final Type type,
+ final Map<String, String> imports) {
+ Preconditions.checkArgument(parentGenType != null,
+ "Parent Generated Type parameter MUST be specified and cannot be " + "NULL!");
+ Preconditions.checkArgument(parentGenType.getName() != null, "Parent Generated Type name cannot be NULL!");
+ Preconditions.checkArgument(parentGenType.getPackageName() != null,
+ "Parent Generated Type cannot have Package Name referenced as NULL!");
+ Preconditions.checkArgument(type != null, "Type parameter MUST be specified and cannot be NULL!");
+
+ Preconditions.checkArgument(type.getName() != null, "Type name cannot be NULL!");
+ Preconditions.checkArgument(type.getPackageName() != null, "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);
+ }
+ }
+ }
+
+ /**
+ * Checks if the constant with the name <code>constName</code> is in the
+ * list of the constant definition for <code>genTO</code>.
+ *
+ * @param constName
+ * string with the name of constant which is sought
+ * @param genTO
+ * generated transfer object in which is <code>constName</code>
+ * sought
+ * @return boolean value
+ * <ul>
+ * <li>true - if <code>constName</code> is in the list of the
+ * constant definition for <code>genTO</code></li>
+ * <li>false - in other cases</li>
+ * </ul>
+ * @throws IllegalArgumentException
+ * <ul>
+ * <li>if <code>constName</code> equals <code>null</code></li>
+ * <li>if <code>genTO</code> equals <code>null</code></li>
+ * </ul>
+ */
+ 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;
+ }
+
+ /**
+ * Creates the map which maps the type name to package name and contains
+ * only package names for enclosed types of <code>genType</code> and
+ * recursivelly their enclosed types.
+ *
+ * @param genType
+ * JAVA <code>Type</code> for which is the map created
+ * @return map of the package names for all the enclosed types and
+ * recursivelly their enclosed types
+ */
+ 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;
+ }
+
+ /**
+ * Builds the string which contains either the full path to the type
+ * (package name with type) or only type name if the package is among
+ * <code>imports</code>.
+ *
+ * @param parentGenType
+ * generated type which contains <code>type</code>
+ * @param type
+ * JAVA <code>Type</code> for which is the string with type info
+ * generated
+ * @param imports
+ * map of necessary imports for <code>parentGenType</code>
+ * @return string with type name for <code>type</code> in the full format or
+ * in the short format
+ * @throws IllegalArgumentException
+ * <ul>
+ * <li>if the <code>type</code> equals <code>null</code></li>
+ * <li>if the name of the <code>type</code> equals
+ * <code>null</code></li>
+ * <li>if the name of the package of the <code>type</code>
+ * equals <code>null</code></li>
+ * <li>if the <code>imports</code> equals <code>null</code></li>
+ * </ul>
+ */
+ public static String getExplicitType(final GeneratedType parentGenType, final Type type,
+ final Map<String, String> imports) {
+
+ Preconditions.checkArgument(type != null, "Type parameter MUST be specified and cannot be NULL!");
+ Preconditions.checkArgument(type.getName() != null, "Type name cannot be NULL!");
+ Preconditions.checkArgument(type.getPackageName() != null, "Type cannot have Package Name referenced as NULL!");
+ Preconditions.checkArgument(imports != null, "Imports Map cannot be NULL!");
+
+ final String typePackageName = type.getPackageName();
+ final String typeName = type.getName();
+ final String importedPackageName = imports.get(typeName);
+ final StringBuilder builder;
+ if (typePackageName.equals(importedPackageName) || typePackageName.equals(parentGenType.getPackageName())) {
+ builder = new StringBuilder(type.getName());
+ addActualTypeParameters(builder, type, parentGenType, imports);
+ if (builder.toString().equals("Void")) {
+ return "void";
+ }
+ } else {
+ builder = new StringBuilder();
+ if (typePackageName.startsWith("java.lang")) {
+ builder.append(type.getName());
+ } else {
+ if (!typePackageName.isEmpty()) {
+ builder.append(typePackageName + Constants.DOT + type.getName());
+ } else {
+ builder.append(type.getName());
+ }
+ }
+ if (type.equals(Types.voidType())) {
+ return "void";
+ }
+ addActualTypeParameters(builder, type, parentGenType, imports);
+ }
+ return builder.toString();
+
+ }
+
+ /**
+ * Adds actual type parameters from <code>type</code> to
+ * <code>builder</code> if <code>type</code> is
+ * <code>ParametrizedType</code>.
+ *
+ * @param builder
+ * string builder which contains type name
+ * @param type
+ * JAVA <code>Type</code> for which is the string with type info
+ * generated
+ * @param parentGenType
+ * generated type which contains <code>type</code>
+ * @param imports
+ * map of necessary imports for <code>parentGenType</code>
+ * @return if <code>type</code> is of the type <code>ParametrizedType</code> <br />
+ * <li>then <code>builder</code> + actual <code>type</code>
+ * parameters</li> <li>else only <code>builder</code></li>
+ */
+ private static StringBuilder addActualTypeParameters(StringBuilder builder, final Type type,
+ final GeneratedType parentGenType, final Map<String, String> imports) {
+ if (type instanceof ParameterizedType) {
+ final ParameterizedType pType = (ParameterizedType) type;
+ final Type[] pTypes = pType.getActualTypeArguments();
+ builder.append("<");
+ builder.append(getParameters(parentGenType, pTypes, imports));
+ builder.append(">");
+ }
+ return builder;
+ }
+
+ /**
+ * Generates the string with all actual type parameters from
+ * <code>pTypes</code>
+ *
+ * @param parentGenType
+ * generated type for which is the JAVA code generated
+ * @param pTypes
+ * array of <code>Type</code> instances = actual type parameters
+ * @param availableImports
+ * map of imports for <code>parentGenType</code>
+ * @return string with all actual type parameters from <code>pTypes</code>
+ */
+ private static String getParameters(final GeneratedType parentGenType, final Type[] pTypes,
+ Map<String, String> availableImports) {
+ final StringBuilder builder = new StringBuilder();
+ for (int i = 0; i < pTypes.length; i++) {
+ final Type t = pTypes[i];
+
+ String separator = COMMA;
+ if (i == (pTypes.length - 1)) {
+ separator = "";
+ }
+
+ String wildcardParam = "";
+ if (t.equals(Types.voidType())) {
+ builder.append("java.lang.Void" + separator);
+ continue;
+ } else {
+
+ if (t instanceof WildcardType) {
+ wildcardParam = "? extends ";
+ }
+
+ builder.append(wildcardParam + getExplicitType(parentGenType, t, availableImports) + separator);
+ }
+ }
+ return builder.toString();
+ }
+
+ /**
+ * Returns the 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.
+ * @throws IllegalArgumentException
+ * if <code>childTransportObject</code> equals <code>null</code>
+ */
+ public static GeneratedTransferObject getTopParrentTransportObject(GeneratedTransferObject childTransportObject) {
+ if (childTransportObject == null) {
+ throw new IllegalArgumentException("Parameter childTransportObject can't be null.");
+ }
+ if (childTransportObject.getSuperType() == null) {
+ return childTransportObject;
+ } else {
+ return getTopParrentTransportObject(childTransportObject.getSuperType());
+ }
+ }
+
+ /**
+ * Selects from input list of properties only those which have read only
+ * attribute set to true.
+ *
+ * @param properties
+ * list of properties of generated transfer object
+ * @return subset of <code>properties</code> which have read only attribute
+ * set to true
+ */
+ public static List<GeneratedProperty> resolveReadOnlyPropertiesFromTO(List<GeneratedProperty> properties) {
+ List<GeneratedProperty> readOnlyProperties = new ArrayList<GeneratedProperty>();
+ if (properties != null) {
+ for (final GeneratedProperty property : properties) {
+ if (property.isReadOnly()) {
+ readOnlyProperties.add(property);
+ }
+ }
+ }
+ return readOnlyProperties;
+ }
+
+ /**
+ * Returns the list of the read only properties of all extending generated
+ * transfer object from <code>genTO</code> to highest parent generated
+ * transfer object
+ *
+ * @param genTO
+ * generated transfer object for which is the list of read only
+ * properties generated
+ * @return list of all read only properties from actual to highest parent
+ * generated transfer object. In case when extension exists the
+ * method is recursive called.
+ */
+ public static List<GeneratedProperty> getPropertiesOfAllParents(GeneratedTransferObject genTO) {
+ List<GeneratedProperty> propertiesOfAllParents = new ArrayList<GeneratedProperty>();
+ if (genTO.getSuperType() != null) {
+ final List<GeneratedProperty> allPropertiesOfTO = genTO.getSuperType().getProperties();
+ List<GeneratedProperty> readOnlyPropertiesOfTO = resolveReadOnlyPropertiesFromTO(allPropertiesOfTO);
+ propertiesOfAllParents.addAll(readOnlyPropertiesOfTO);
+ propertiesOfAllParents.addAll(getPropertiesOfAllParents(genTO.getSuperType()));
+ }
+ return propertiesOfAllParents;
+ }
+
+}