Binding generator v2 - uses statement - uses inner type #1
[mdsal.git] / binding2 / mdsal-binding2-generator-util / src / main / java / org / opendaylight / mdsal / binding / javav2 / generator / util / Types.java
index c2cdf7e263f795be7075b8742ee81c6ebdb91365..a25bd4e12022f0e1d3a40231dd9000a4aad7867a 100644 (file)
@@ -22,7 +22,9 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
+import org.opendaylight.mdsal.binding.javav2.generator.context.ModuleContext;
 import org.opendaylight.mdsal.binding.javav2.model.api.BaseTypeWithRestrictions;
 import org.opendaylight.mdsal.binding.javav2.model.api.ConcreteType;
 import org.opendaylight.mdsal.binding.javav2.model.api.ParameterizedType;
@@ -31,11 +33,11 @@ import org.opendaylight.mdsal.binding.javav2.model.api.Type;
 import org.opendaylight.mdsal.binding.javav2.model.api.WildcardType;
 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
-import org.opendaylight.mdsal.binding.javav2.spec.base.RpcCallback;
 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
+import org.w3c.dom.Document;
 
 @Beta
 public final class Types {
@@ -43,7 +45,7 @@ public final class Types {
             new CacheLoader<Class<?>, ConcreteType>() {
 
                 @Override
-                public ConcreteType load(Class<?> key) throws Exception {
+                public ConcreteType load(@Nonnull final Class<?> key) throws Exception {
                     return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
                 }
             };
@@ -52,9 +54,11 @@ public final class Types {
             CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
 
     public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
-    public static final ConcreteType RPC_CALLBACK = typeForClass(RpcCallback.class);
+    public static final ConcreteType CLASS = typeForClass(Class.class);
     public static final ConcreteType STRING = typeForClass(String.class);
     public static final ConcreteType VOID = typeForClass(Void.class);
+    public static final ConcreteType DOCUMENT = typeForClass(Document.class);
+
     public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
     public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
 
@@ -119,6 +123,20 @@ public final class Types {
         }
     }
 
+    public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions,
+            final ModuleContext moduleContext) {
+        if (restrictions != null) {
+            if (restrictions instanceof DefaultRestrictions) {
+                return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
+            } else {
+                return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions,
+                        moduleContext);
+            }
+        } else {
+            return typeForClass(cls);
+        }
+    }
+
     /**
      * Returns an instance of {@link ParameterizedType} describing the typed
      * {@link Map}&lt;K,V&gt;
@@ -189,6 +207,27 @@ public final class Types {
         return new WildcardTypeImpl(packageName, typeName);
     }
 
+    /**
+     * Creates instance of type
+     * {@link WildcardType
+     * WildcardType}
+     *
+     * @param packageName
+     *            string with the package name
+     * @param typeName
+     *            string with the type name
+     * @param isPkNameNormalized
+     *            if the package name has been normalized
+     * @param isTypeNormalized
+     *            if the type name has been normalized
+     * @return <code>WildcardType</code> representation of
+     *         <code>packageName</code> and <code>typeName</code>
+     */
+    public static WildcardType wildcardTypeFor(final String packageName, final String typeName,
+            final boolean isPkNameNormalized, final boolean isTypeNormalized, ModuleContext context) {
+        return new WildcardTypeImpl(packageName, typeName, isPkNameNormalized, isTypeNormalized, context);
+    }
+
     /**
      * Creates instance of
      * {@link ParameterizedType
@@ -228,13 +267,24 @@ public final class Types {
     @Nullable
     public static String getOuterClassName(final Type valueType) {
         final String pkgName = valueType.getPackageName();
-        if(CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName) >= 0) {
+        final int index = CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName);
+        if (index >= 0) {
             // It is inner class.
-            return Iterables.getLast(DOT_SPLITTER.split(pkgName));
+            return Iterables.getFirst(DOT_SPLITTER.split(pkgName.substring(index)), null);
         }
         return null;
     }
 
+    @Nullable
+    public static String getOuterClassPackageName(final Type valueType) {
+        final String pkgName = valueType.getPackageName();
+        final int index = CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName);
+        if (index >= 1) {
+            return pkgName.substring(0, index - 1);
+        }
+        return pkgName;
+    }
+
     /**
      *
      * Represents concrete JAVA type.
@@ -254,13 +304,13 @@ public final class Types {
          *            string with the name of the type
          */
         private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
-            super(pkName, name);
+            super(pkName, name, true,null);
             this.restrictions = restrictions;
         }
 
         @Override
         public Restrictions getRestrictions() {
-            return restrictions;
+            return this.restrictions;
         }
     }
 
@@ -282,13 +332,19 @@ public final class Types {
          *            string with the name of the type
          */
         private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
-            super(pkName, name);
+            super(pkName, name, null);
+            this.restrictions = Preconditions.checkNotNull(restrictions);
+        }
+
+        private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions,
+                final ModuleContext moduleContext) {
+            super(pkName, name, moduleContext);
             this.restrictions = Preconditions.checkNotNull(restrictions);
         }
 
         @Override
         public Restrictions getRestrictions() {
-            return restrictions;
+            return this.restrictions;
         }
     }
 
@@ -318,7 +374,7 @@ public final class Types {
          *            array of actual parameters
          */
         public ParameterizedTypeImpl(final Type rawType, final Type[] actTypes) {
-            super(rawType.getPackageName(), rawType.getName());
+            super(rawType.getPackageName(), rawType.getName(), true, null);
             this.rawType = rawType;
             this.actualTypes = actTypes.clone();
         }
@@ -326,12 +382,12 @@ public final class Types {
         @Override
         public Type[] getActualTypeArguments() {
 
-            return actualTypes;
+            return this.actualTypes;
         }
 
         @Override
         public Type getRawType() {
-            return rawType;
+            return this.rawType;
         }
     }
 
@@ -349,8 +405,26 @@ public final class Types {
          * @param typeName
          *            string with the name of type
          */
+        //FIXME: doesn't seem to be called at all
         public WildcardTypeImpl(final String packageName, final String typeName) {
-            super(packageName, typeName);
+            super(packageName, typeName, null);
+        }
+
+        /**
+         * Creates instance of this class with concrete package and type name.
+         *
+         * @param packageName
+         *            string with the package name
+         * @param typeName
+         *            string with the name of type
+         * @param isPkNameNormalized
+         *            if the package name has been normalized
+         * @param isTypeNormalized
+         *            if the type name has been normalized
+         */
+        public WildcardTypeImpl(final String packageName, final String typeName, final boolean isPkNameNormalized,
+                final boolean isTypeNormalized, ModuleContext context) {
+            super(packageName, typeName, isPkNameNormalized, isTypeNormalized, context);
         }
     }
 
@@ -375,7 +449,7 @@ public final class Types {
 
         @Override
         public List<RangeConstraint> getRangeConstraints() {
-            return rangeConstraints;
+            return this.rangeConstraints;
         }
 
         @Override