BUG-1902: reuse ConcreteTypeImpl instances 25/11225/4
authorRobert Varga <rovarga@cisco.com>
Tue, 16 Sep 2014 09:53:44 +0000 (11:53 +0200)
committerRobert Varga <rovarga@cisco.com>
Tue, 16 Sep 2014 20:01:32 +0000 (22:01 +0200)
Creates shared instances for byte[], char[], Serializable and QName
and reuses them as much as possible. Unfortuntely this Helium-specific
patch does not have an explicit cache due to maven interactions, which
prevent us from useing Guava cache classes.

Change-Id: I6d9b57418e4a7dbbe4878f9f9fd1ed57ddb4362d
Signed-off-by: Robert Varga <rovarga@cisco.com>
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java
code-generator/binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/Types.java
code-generator/binding-type-provider/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/BaseYangTypes.java
code-generator/binding-type-provider/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java

index c1fd71df23a3065cebb6206481f52b184f08a986..86725af618c588800b6506b93b12447b01d77499 100644 (file)
@@ -26,7 +26,6 @@ import static org.opendaylight.yangtools.binding.generator.util.Types.typeForCla
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findNodeInSchemaContext;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
-
 import com.google.common.base.Splitter;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Sets;
@@ -1927,7 +1926,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             }
 
             final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
-            genPropBuilder.setReturnType(Types.primitiveType("char[]", null));
+            genPropBuilder.setReturnType(Types.CHAR_ARRAY);
             resultTOBuilder.addEqualsIdentity(genPropBuilder);
             resultTOBuilder.addHashIdentity(genPropBuilder);
             resultTOBuilder.addToStringProperty(genPropBuilder);
index 91d56a3562bbcfcdff2f62e263c46e7e05fc3085..7c261bb7ca8b2507b4ff7d61f00e4153dfca81c9 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.yangtools.binding.generator.util;
 
+import java.io.Serializable;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
@@ -19,16 +20,21 @@ import org.opendaylight.yangtools.sal.binding.model.api.Type;
 import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;
 import org.opendaylight.yangtools.yang.binding.Augmentable;
 import org.opendaylight.yangtools.yang.binding.Augmentation;
+import org.opendaylight.yangtools.yang.common.QName;
 
 public final class Types {
     private static final Type SET_TYPE = typeForClass(Set.class);
     private static final Type LIST_TYPE = typeForClass(List.class);
     private static final Type MAP_TYPE = typeForClass(Map.class);
+    private static final ConcreteType QNAME_TYPE = internalTypeForClass(QName.class);
+    private static final ConcreteType SERIALIZABLE_TYPE = internalTypeForClass(Serializable.class);
 
     public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
     public static final ConcreteType FUTURE = typeForClass(Future.class);
     public static final ConcreteType STRING = typeForClass(String.class);
     public static final ConcreteType VOID = typeForClass(Void.class);
+    public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
+    public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
 
     /**
      * It is not desirable to create instance of this class
@@ -55,15 +61,19 @@ public final class Types {
      * doesn't exist.
      *
      * @param primitiveType
-     *            string containing programaticall construction based on
+     *            string containing programmatic construction based on
      *            primitive type (e.g byte[])
-     * @return <code>ConcreteType</code> instance which represents programatic
+     * @return <code>ConcreteType</code> instance which represents programmatic
      *         construction with primitive JAVA type
      */
-    public static Type primitiveType(final String primitiveType, final Restrictions restrictions) {
+    public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
         return new ConcreteTypeImpl("", primitiveType, restrictions);
     }
 
+    private static ConcreteType internalTypeForClass(final Class<?> cls) {
+        return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), null);
+    }
+
     /**
      * Returns an instance of {@link ConcreteType} describing the class
      *
@@ -71,12 +81,23 @@ public final class Types {
      *            Class to describe
      * @return Description of class
      */
-    public static ConcreteType typeForClass(Class<?> cls) {
-        return typeForClass(cls, null);
+    public static ConcreteType typeForClass(final Class<?> cls) {
+        if (cls.equals(Serializable.class)) {
+            return SERIALIZABLE_TYPE;
+        }
+        if (cls.equals(QName.class)) {
+            return QNAME_TYPE;
+        }
+
+        return internalTypeForClass(cls);
     }
 
-    public static ConcreteType typeForClass(Class<?> cls, Restrictions restrictions) {
-        return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
+    public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
+        if (restrictions != null) {
+            return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
+        } else {
+            return typeForClass(cls);
+        }
     }
 
     /**
@@ -89,7 +110,7 @@ public final class Types {
      *            Value Type
      * @return Description of generic type instance
      */
-    public static ParameterizedType mapTypeFor(Type keyType, Type valueType) {
+    public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
         return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
     }
 
@@ -101,7 +122,7 @@ public final class Types {
      *            Value Type
      * @return Description of generic type instance of Set
      */
-    public static ParameterizedType setTypeFor(Type valueType) {
+    public static ParameterizedType setTypeFor(final Type valueType) {
         return parameterizedTypeFor(SET_TYPE, valueType);
     }
 
@@ -113,7 +134,7 @@ public final class Types {
      *            Value Type
      * @return Description of type instance of List
      */
-    public static ParameterizedType listTypeFor(Type valueType) {
+    public static ParameterizedType listTypeFor(final Type valueType) {
         return parameterizedTypeFor(LIST_TYPE, valueType);
     }
 
@@ -129,7 +150,7 @@ public final class Types {
      * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
      *         and its parameters <code>parameters</code>
      */
-    public static ParameterizedType parameterizedTypeFor(Type type, Type... parameters) {
+    public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
         return new ParametrizedTypeImpl(type, parameters);
     }
 
@@ -142,10 +163,10 @@ public final class Types {
      *            string with the package name
      * @param typeName
      *            string with the type name
-     * @return <code>WildcardType</code> reprezentation of
+     * @return <code>WildcardType</code> representation of
      *         <code>packageName</code> and <code>typeName</code>
      */
-    public static WildcardType wildcardTypeFor(String packageName, String typeName) {
+    public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
         return new WildcardTypeImpl(packageName, typeName);
     }
 
@@ -158,11 +179,11 @@ public final class Types {
      *
      * @param valueType
      *            JAVA <code>Type</code> with actual parameter
-     * @return <code>ParametrizedType</code> reprezentation of raw type
+     * @return <code>ParametrizedType</code> representation of raw type
      *         <code>Augmentable</code> with actual parameter
      *         <code>valueType</code>
      */
-    public static ParameterizedType augmentableTypeFor(Type valueType) {
+    public static ParameterizedType augmentableTypeFor(final Type valueType) {
         final Type augmentable = typeForClass(Augmentable.class);
         return parameterizedTypeFor(augmentable, valueType);
     }
@@ -180,7 +201,7 @@ public final class Types {
      *         <code>Augmentation</code> with actual parameter
      *         <code>valueType</code>
      */
-    public static ParameterizedType augmentationTypeFor(Type valueType) {
+    public static ParameterizedType augmentationTypeFor(final Type valueType) {
         final Type augmentation = typeForClass(Augmentation.class);
         return parameterizedTypeFor(augmentation, valueType);
     }
@@ -202,7 +223,7 @@ public final class Types {
          * @param name
          *            string with the name of the type
          */
-        private ConcreteTypeImpl(String pkName, String name, Restrictions restrictions) {
+        private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
             super(pkName, name);
             this.restrictions = restrictions;
         }
@@ -222,12 +243,12 @@ public final class Types {
         /**
          * Array of JAVA actual type parameters.
          */
-        private Type[] actualTypes;
+        private final Type[] actualTypes;
 
         /**
          * JAVA raw type (like List, Set, Map...)
          */
-        private Type rawType;
+        private final Type rawType;
 
         @Override
         public Type[] getActualTypeArguments() {
@@ -249,7 +270,7 @@ public final class Types {
          * @param actTypes
          *            array of actual parameters
          */
-        public ParametrizedTypeImpl(Type rawType, Type[] actTypes) {
+        public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
             super(rawType.getPackageName(), rawType.getName());
             this.rawType = rawType;
             this.actualTypes = Arrays.copyOf(actTypes, actTypes.length);
@@ -271,7 +292,7 @@ public final class Types {
          * @param typeName
          *            string with the name of type
          */
-        public WildcardTypeImpl(String packageName, String typeName) {
+        public WildcardTypeImpl(final String packageName, final String typeName) {
             super(packageName, typeName);
         }
     }
index 86b40c027efada8b0e20fe16e2f421f1e31d65e8..987850821c582ecb2284fc507fe333117edf4f5d 100644 (file)
@@ -144,7 +144,7 @@ public final class BaseYangTypes {
          * @return java <code>Type</code> representation of <code>type</code>
          */
         @Override
-        public Type javaTypeForYangType(String type) {
+        public Type javaTypeForYangType(final String type) {
             return typeMap.get(type);
         }
 
@@ -159,7 +159,7 @@ public final class BaseYangTypes {
          *         returned.
          */
         @Override
-        public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode) {
+        public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode) {
             if (type != null) {
                 return typeMap.get(type.getQName().getLocalName());
             }
@@ -168,12 +168,12 @@ public final class BaseYangTypes {
         }
 
         @Override
-        public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode,
-                Restrictions restrictions) {
+        public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode,
+                final Restrictions restrictions) {
             String typeName = type.getQName().getLocalName();
             switch (typeName) {
             case "binary":
-                return Types.primitiveType("byte[]", restrictions);
+                return restrictions == null ? Types.BYTE_ARRAY : Types.primitiveType("byte[]", restrictions);
             case "decimal64":
                 return Types.typeForClass(BigDecimal.class, restrictions);
             case "enumeration":
@@ -204,17 +204,17 @@ public final class BaseYangTypes {
         }
 
         @Override
-        public String getTypeDefaultConstruction(LeafSchemaNode node) {
+        public String getTypeDefaultConstruction(final LeafSchemaNode node) {
             return null;
         }
 
         @Override
-        public String getConstructorPropertyName(SchemaNode node) {
+        public String getConstructorPropertyName(final SchemaNode node) {
             return null;
         }
 
         @Override
-        public String getParamNameFromType(TypeDefinition<?> type) {
+        public String getParamNameFromType(final TypeDefinition<?> type) {
             return "_" + BindingGeneratorUtil.parseToValidParamName(type.getQName().getLocalName());
         }
     };
index d48046a48b23bc76cb174d92fe70b0e9d68f686d..54217fbc0a0f2d1f36efb01019a65e5e3bde1dc6 100644 (file)
@@ -13,7 +13,6 @@ import static org.opendaylight.yangtools.binding.generator.util.BindingGenerator
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNodeForRelativeXPath;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
-
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Sets;
 import com.google.common.io.BaseEncoding;
@@ -226,7 +225,7 @@ public final class TypeProviderImpl implements TypeProvider {
         return returnType;
     }
 
-    private GeneratedTransferObject shadedTOWithRestrictions(GeneratedTransferObject gto, Restrictions r) {
+    private GeneratedTransferObject shadedTOWithRestrictions(final GeneratedTransferObject gto, final Restrictions r) {
         GeneratedTOBuilder gtob = new GeneratedTOBuilderImpl(gto.getPackageName(), gto.getName());
         GeneratedTransferObject parent = gto.getSuperType();
         if (parent != null) {
@@ -818,7 +817,7 @@ public final class TypeProviderImpl implements TypeProvider {
         }
 
         final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
-        genPropBuilder.setReturnType(Types.primitiveType("char[]", null));
+        genPropBuilder.setReturnType(Types.CHAR_ARRAY);
         resultTOBuilder.addEqualsIdentity(genPropBuilder);
         resultTOBuilder.addHashIdentity(genPropBuilder);
         resultTOBuilder.addToStringProperty(genPropBuilder);