Generate compatibility constructors for Uint wrapped types 01/84201/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 4 Sep 2019 14:57:10 +0000 (16:57 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 4 Sep 2019 15:02:02 +0000 (17:02 +0200)
We already are generating compatibility setters, this extends this
capability to wrapper classes (i.e. their constructors). As usual
the compatibility code is marked as deprecated for removal.

Also fixes some code formatting issues introduces in previous
patches.

JIRA: MDSAL-330
Change-Id: I0b2db136c27c26de33f8a756292dde4cdf53bebb
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BaseTemplate.xtend
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderTemplate.xtend
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ClassTemplate.xtend

index 92f0d5ad1664b2080a0409f43a8fd9e5c9c9352d..dc7fc89889f6c2db6036c5565cac76bc8a2b7e33 100644 (file)
@@ -12,7 +12,9 @@ import static org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.enc
 import com.google.common.base.CharMatcher
 import com.google.common.base.MoreObjects
 import com.google.common.base.Splitter
+import com.google.common.collect.ImmutableMap
 import com.google.common.collect.Iterables
+import java.math.BigInteger
 import java.util.Collection
 import java.util.List
 import java.util.Locale
@@ -36,6 +38,10 @@ import org.opendaylight.mdsal.binding.model.util.Types
 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
 import org.opendaylight.yangtools.yang.binding.CodeHelpers
 import org.opendaylight.yangtools.yang.common.QName
+import org.opendaylight.yangtools.yang.common.Uint8
+import org.opendaylight.yangtools.yang.common.Uint16
+import org.opendaylight.yangtools.yang.common.Uint32
+import org.opendaylight.yangtools.yang.common.Uint64
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition
@@ -63,6 +69,13 @@ abstract class BaseTemplate extends JavaFileTemplate {
         .addIgnoredStatement(YangStmtMapping.ORGANIZATION)
         .build();
 
+    protected static val UINT_TYPES = ImmutableMap.of(
+        Types.typeForClass(Uint8), Types.typeForClass(Short),
+        Types.typeForClass(Uint16), Types.typeForClass(Integer),
+        Types.typeForClass(Uint32), Types.typeForClass(Long),
+        Types.typeForClass(Uint64), Types.typeForClass(BigInteger)
+    );
+
     new(GeneratedType type) {
         super(type)
     }
index 7c79d82c5111b9578561371e729898c1015477c0..698d0b0fafaa1cc3e8e24d320e70c6965afa1248 100644 (file)
@@ -12,8 +12,6 @@ import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTA
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
 
 import com.google.common.collect.ImmutableList
-import com.google.common.collect.ImmutableMap
-import java.math.BigInteger
 import java.util.ArrayList
 import java.util.Collection
 import java.util.HashMap
@@ -35,10 +33,6 @@ import org.opendaylight.yangtools.concepts.Builder
 import org.opendaylight.yangtools.yang.binding.AugmentationHolder
 import org.opendaylight.yangtools.yang.binding.CodeHelpers
 import org.opendaylight.yangtools.yang.binding.DataObject
-import org.opendaylight.yangtools.yang.common.Uint8
-import org.opendaylight.yangtools.yang.common.Uint16
-import org.opendaylight.yangtools.yang.common.Uint64
-import org.opendaylight.yangtools.yang.common.Uint32
 
 /**
  * Template for generating JAVA builder classes.
@@ -51,13 +45,6 @@ class BuilderTemplate extends AbstractBuilderTemplate {
 
     static val AUGMENTATION_FIELD_UPPER = AUGMENTATION_FIELD.toFirstUpper
 
-    static val UINT_TYPES = ImmutableMap.of(
-        Types.typeForClass(Uint8), Types.typeForClass(Short),
-        Types.typeForClass(Uint16), Types.typeForClass(Integer),
-        Types.typeForClass(Uint32), Types.typeForClass(Long),
-        Types.typeForClass(Uint64), Types.typeForClass(BigInteger)
-    );
-
     /**
      * Constructs new instance of this class.
      * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
@@ -297,31 +284,33 @@ class BuilderTemplate extends AbstractBuilderTemplate {
     def private generateSetter(GeneratedProperty field, Type actualType) '''
         «val restrictions = restrictionsForSetter(actualType)»
         «IF restrictions !== null»
+
             «generateCheckers(field, restrictions, actualType)»
         «ENDIF»
 
         «val setterName = "set" + field.getName.toFirstUpper»
         public «type.getName» «setterName»(final «field.returnType.importedName» value) {
-        «IF restrictions !== null»
-            if (value != null) {
-                «checkArgument(field, restrictions, actualType, "value")»
-            }
-        «ENDIF»
+            «IF restrictions !== null»
+                if (value != null) {
+                    «checkArgument(field, restrictions, actualType, "value")»
+                }
+            «ENDIF»
             this.«field.fieldName.toString» = value;
             return this;
         }
         «val uintType = UINT_TYPES.get(field.returnType)»
         «IF uintType !== null»
 
-        /**
-         * Utility migration setter.
-         *
-         * @deprecated Use {#link «setterName»(«field.returnType.importedName»)} instead.
-         */
-        @Deprecated(forRemoval = true)
-        public «type.getName» «setterName»(final «uintType.importedName» value) {
-            return «setterName»(«CodeHelpers.importedName».compatUint(value));
-        }
+            /**
+             * Utility migration setter.
+             *
+             * @param value field value in legacy type
+             * @deprecated Use {#link «setterName»(«field.returnType.importedName»)} instead.
+             */
+            @Deprecated(forRemoval = true)
+            public «type.getName» «setterName»(final «uintType.importedName» value) {
+                return «setterName»(«CodeHelpers.importedName».compatUint(value));
+            }
         «ENDIF»
     '''
 
index 3bc5fa6abf50801f06b4fc4ea9ca370dc18f9c86..c4b36f694ebfac645b1f8faa257829bb9946fbd4 100644 (file)
@@ -261,6 +261,21 @@ class ClassTemplate extends BaseTemplate {
             «ENDIF»
         «ENDFOR»
     }
+    «val propType = allProperties.get(0).returnType»
+    «val uintType = UINT_TYPES.get(propType)»
+    «IF uintType !== null»
+
+        /**
+         * Utility migration constructor.
+         *
+         * @param value Wrapped value in legacy type
+         * @deprecated Use {#link «type.name»(«propType.importedName»)} instead.
+         */
+        @Deprecated(forRemoval = true)
+        public «type.getName»(final «uintType.importedName» value) {
+            this(«CodeHelpers.importedName».compatUint(value));
+        }
+    «ENDIF»
     '''
 
     def protected genUnionConstructor() '''