Add JavaTypeNames for annotation types
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / ClassTemplate.xtend
index e5140900662c29e06c08d3a0e8470d6ccc360860..6c330ef91b482389d22a03160f4755943708ecc0 100644 (file)
@@ -201,9 +201,14 @@ class ClassTemplate extends BaseTemplate {
     def protected constructors() '''
         «IF genTO.unionType»
             «genUnionConstructor»
+        «ELSEIF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
+            «typedefConstructor»
+            «legacyConstructor»
         «ELSE»
             «allValuesConstructor»
+            «legacyConstructor»
         «ENDIF»
+
         «IF !allProperties.empty»
             «copyConstructor»
         «ENDIF»
@@ -212,44 +217,77 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
     '''
 
-    def protected allValuesConstructor() '''
-    «IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
-        @«ConstructorParameters.importedName»("value")
-        @«ConstructorProperties.importedName»("value")
-    «ENDIF»
+    def private allValuesConstructor() '''
     public «type.name»(«allProperties.asArgumentsDeclaration») {
         «IF false == parentProperties.empty»
             super(«parentProperties.asArguments»);
         «ENDIF»
         «FOR p : allProperties»
-            «generateRestrictions(type, p.fieldName.toString, p.returnType)»
+            «generateRestrictions(type, p.fieldName, p.returnType)»
         «ENDFOR»
 
-        «/*
-         * If we have patterns, we need to apply them to the value field. This is a sad
-         * consequence of how this code is structured.
-         */
-        IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
-            «CodeHelpers.importedName».requireValue(_value);
-            «genPatternEnforcer("_value")»
+        «FOR p : properties»
+            «val fieldName = p.fieldName»
+            «IF p.returnType.name.endsWith("[]")»
+                this.«fieldName» = «fieldName» == null ? null : «fieldName».clone();
+            «ELSE»
+                this.«fieldName» = «fieldName»;
+            «ENDIF»
+        «ENDFOR»
+    }
+    '''
+
+    def private typedefConstructor() '''
+    @«ConstructorParameters.importedName»("value")
+    @«ConstructorProperties.importedName»("value")
+    public «type.name»(«allProperties.asArgumentsDeclaration») {
+        «IF false == parentProperties.empty»
+            super(«parentProperties.asArguments»);
         «ENDIF»
+        «FOR p : allProperties»
+            «generateRestrictions(type, p.fieldName, p.returnType)»
+        «ENDFOR»
+        «/*
+         * If we have patterns, we need to apply them to the value field. This is a sad consequence of how this code is
+         * structured.
+         */»
+        «CodeHelpers.importedName».requireValue(_value);
+        «genPatternEnforcer("_value")»
 
         «FOR p : properties»
             «val fieldName = p.fieldName»
             «IF p.returnType.name.endsWith("[]")»
-                «IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
                 this.«fieldName» = «fieldName».clone();
-                «ELSE»
-                this.«fieldName» = «fieldName» == null ? null : «fieldName».clone();
-                «ENDIF»
             «ELSE»
-            this.«fieldName» = «fieldName»;
+                this.«fieldName» = «fieldName»;
             «ENDIF»
         «ENDFOR»
     }
-
     '''
 
+    def private legacyConstructor() {
+        if (!hasUintProperties) {
+            return ""
+        }
+
+        val compatUint = CodeHelpers.importedName + ".compatUint("
+        return '''
+
+            /**
+             * Utility migration constructor.
+             *
+             «FOR prop : allProperties»
+             * @param «prop.fieldName» «prop.name»«IF prop.isUintType» in legacy Java type«ENDIF»
+             «ENDFOR»
+             * @deprecated Use {#link «type.name»(«FOR prop : allProperties SEPARATOR ", "»«prop.returnType.importedJavadocName»«ENDFOR»)} instead.
+             */
+            @Deprecated(forRemoval = true)
+            public «type.getName»(«FOR prop : allProperties SEPARATOR ", "»«prop.legacyType.importedName» «prop.fieldName»«ENDFOR») {
+                this(«FOR prop : allProperties SEPARATOR ", "»«IF prop.isUintType»«compatUint»«prop.fieldName»)«ELSE»«prop.fieldName»«ENDIF»«ENDFOR»);
+            }
+        '''
+    }
+
     def protected genUnionConstructor() '''
     «FOR p : allProperties»
         «val List<GeneratedProperty> other = new ArrayList(properties)»
@@ -257,7 +295,6 @@ class ClassTemplate extends BaseTemplate {
             «genConstructor(p, other)»
         «ENDIF»
     «ENDFOR»
-
     '''
 
     def protected genConstructor(GeneratedProperty property, Iterable<GeneratedProperty> other) '''
@@ -267,7 +304,7 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
 
         «val fieldName = property.fieldName»
-        «generateRestrictions(type, fieldName.toString, property.returnType)»
+        «generateRestrictions(type, fieldName, property.returnType)»
 
         this.«fieldName» = «property.name»;
         «FOR p : other»
@@ -504,7 +541,7 @@ class ClassTemplate extends BaseTemplate {
             return ""
         }
         return '''
-            @«Override.importedName»
+            @«OVERRIDE.importedName»
             public int hashCode() {
                 «IF size != 1»
                     «hashCodeResult(genTO.hashCodeIdentifiers)»
@@ -523,7 +560,7 @@ class ClassTemplate extends BaseTemplate {
      */
     def private generateEquals() '''
         «IF !genTO.equalsIdentifiers.empty»
-            @«Override.importedName»
+            @«OVERRIDE.importedName»
             public final boolean equals(java.lang.Object obj) {
                 if (this == obj) {
                     return true;
@@ -549,7 +586,28 @@ class ClassTemplate extends BaseTemplate {
                 return prop;
             }
         }
-        return null;
+        return null
+    }
+
+    def private hasUintProperties() {
+        for (GeneratedProperty prop : allProperties) {
+            if (prop.isUintType) {
+                return true
+            }
+        }
+        return false
+    }
+
+    def private static isUintType(GeneratedProperty prop) {
+        UINT_TYPES.containsKey(prop.returnType)
     }
 
+    def private static legacyType(GeneratedProperty prop) {
+        val type = prop.returnType
+        val uint = UINT_TYPES.get(type)
+        if (uint !== null) {
+            return uint
+        }
+        return type
+    }
 }