Merge "Added export of augmentation schemas to Binding Context"
[yangtools.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / GeneratorUtil.java
index 678ae343ee90d7c7bd34731052d491918b122491..da285e37bec39d80c12000d68d4821e367198ee2 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.yangtools.sal.java.api.generator;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static org.opendaylight.yangtools.sal.java.api.generator.Constants.COMMA;
 
 import java.util.ArrayList;
@@ -16,6 +17,7 @@ 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.AnnotationType;
 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;
@@ -24,7 +26,6 @@ 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 static com.google.common.base.Preconditions.*;
 
 public final class GeneratorUtil {
 
@@ -39,7 +40,7 @@ public final class GeneratorUtil {
      * 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
@@ -59,18 +60,6 @@ public final class GeneratorUtil {
             }
         }
 
-        final List<Constant> constants = genType.getConstantDefinitions();
-        final List<MethodSignature> methods = genType.getMethodDefinitions();
-        final List<Type> impl = genType.getImplements();
-
-        // 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)) {
@@ -79,6 +68,7 @@ public final class GeneratorUtil {
             putTypeIntoImports(genType, Types.typeForClass(java.util.ArrayList.class), imports);
         }
 
+        final List<MethodSignature> methods = genType.getMethodDefinitions();
         // METHODS
         if (methods != null) {
             for (final MethodSignature method : methods) {
@@ -87,6 +77,9 @@ public final class GeneratorUtil {
                 for (final MethodSignature.Parameter methodParam : method.getParameters()) {
                     putTypeIntoImports(genType, methodParam.getType(), imports);
                 }
+                for (final AnnotationType at : method.getAnnotations()) {
+                    putTypeIntoImports(genType, at, imports);
+                }
             }
         }
 
@@ -109,7 +102,7 @@ public final class GeneratorUtil {
      * 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
@@ -159,8 +152,10 @@ public final class GeneratorUtil {
         if (type instanceof ParameterizedType) {
             final ParameterizedType paramType = (ParameterizedType) type;
             final Type[] params = paramType.getActualTypeArguments();
-            for (Type param : params) {
-                putTypeIntoImports(parentGenType, param, imports);
+            if (params != null) {
+                for (Type param : params) {
+                    putTypeIntoImports(parentGenType, param, imports);
+                }
             }
         }
     }
@@ -168,7 +163,7 @@ public final class GeneratorUtil {
     /**
      * 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
@@ -203,7 +198,7 @@ public final class GeneratorUtil {
      * 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
@@ -225,7 +220,7 @@ public final class GeneratorUtil {
      * 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
@@ -287,7 +282,7 @@ public final class GeneratorUtil {
      * 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
@@ -316,7 +311,7 @@ public final class GeneratorUtil {
     /**
      * 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
@@ -327,6 +322,9 @@ public final class GeneratorUtil {
      */
     private static String getParameters(final GeneratedType parentGenType, final Type[] pTypes,
             Map<String, String> availableImports) {
+        if (pTypes == null || pTypes.length == 0) {
+            return "?";
+        }
         final StringBuilder builder = new StringBuilder();
         for (int i = 0; i < pTypes.length; i++) {
             final Type t = pTypes[i];
@@ -354,7 +352,7 @@ public final class GeneratorUtil {
 
     /**
      * 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
@@ -378,7 +376,7 @@ public final class GeneratorUtil {
     /**
      * 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
@@ -400,7 +398,7 @@ public final class GeneratorUtil {
      * 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