BUG-1485: Switch ClassTemplate length checker 55/22155/3
authorRobert Varga <rovarga@cisco.com>
Sun, 31 May 2015 12:05:02 +0000 (14:05 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 9 Jun 2015 10:02:34 +0000 (10:02 +0000)
Switch ClassTemplate to use LengthGenerator. Also removes the static field
used to hold ranges, as that is only used by deprecated length() method.

Change-Id: I2a084a71d21c8c63a2d9733fc767aa7ecd3cff67
Signed-off-by: Robert Varga <rovarga@cisco.com>
(cherry picked from commit 65cf5ea01d87cbe65633a8e13fcbb6713e334ec2)

code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/TypedefCompilationTest.java

index 70703cf3f293537da58162c9ce4b6458bccc0cbd..cf6f3dd7de920c2c449598399f576a1a59d75361 100644 (file)
@@ -123,13 +123,13 @@ class ClassTemplate extends BaseTemplate {
             «constantsDeclarations»
             «generateFields»
 
-            «IF restrictions != null && (!restrictions.rangeConstraints.nullOrEmpty ||
-                !restrictions.lengthConstraints.nullOrEmpty)»
-            «generateConstraints»
-
-            «IF !restrictions.rangeConstraints.nullOrEmpty»
-            «rangeGenerator.generateRangeChecker("_value", restrictions.rangeConstraints)»
-            «ENDIF»
+            «IF restrictions != null»
+                «IF !restrictions.lengthConstraints.nullOrEmpty»
+                    «LengthGenerator.generateLengthChecker("_value", findProperty(genTO, "value").returnType, restrictions.lengthConstraints)»
+                «ENDIF»
+                «IF !restrictions.rangeConstraints.nullOrEmpty»
+                    «rangeGenerator.generateRangeChecker("_value", restrictions.rangeConstraints)»
+                «ENDIF»
             «ENDIF»
 
             «constructors»
@@ -153,7 +153,7 @@ class ClassTemplate extends BaseTemplate {
 
             «generateToString(genTO.toStringIdentifiers)»
 
-            «generateLengthMethod("length", "_length"
+            «generateLengthMethod()»
 
             «generateRangeMethod()»
 
@@ -178,19 +178,25 @@ class ClassTemplate extends BaseTemplate {
         }
     '''
 
-    def private generateLengthMethod(String methodName, String varName) '''
+    @Deprecated
+    def private generateLengthMethod() '''
         «IF restrictions != null && !(restrictions.lengthConstraints.empty)»
             «val numberClass = restrictions.lengthConstraints.iterator.next.min.class»
             /**
              * @deprecated This method is slated for removal in a future release. See BUG-1485 for details.
              */
             @Deprecated
-            public static «List.importedName»<«Range.importedName»<«numberClass.importedNumber»>> «methodName»() {
-                return «varName»;
+            public static «List.importedName»<«Range.importedName»<«numberClass.importedNumber»>> length() {
+                «List.importedName»<«Range.importedName»<«numberClass.importedName»>> ret = new java.util.ArrayList<>(«restrictions.lengthConstraints.size»);
+                «FOR r : restrictions.lengthConstraints»
+                    ret.add(«Range.importedName».closed(«numericValue(numberClass, r.min)», «numericValue(numberClass, r.max)»));
+                «ENDFOR»
+                return ret;
             }
         «ENDIF»
     '''
 
+    @Deprecated
     private def rangeBody(List<RangeConstraint> restrictions, Class<? extends Number> numberClass) '''
         «List.importedName»<«Range.importedName»<«numberClass.importedName»>> ret = new java.util.ArrayList<>(«restrictions.size»);
         «FOR r : restrictions»
@@ -198,6 +204,7 @@ class ClassTemplate extends BaseTemplate {
         «ENDFOR»
     '''
 
+    @Deprecated
     def private generateRangeMethod() '''
         «IF restrictions != null && !(restrictions.rangeConstraints.empty)»
             «val returnType = allProperties.iterator.next.returnType»
@@ -247,33 +254,6 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
     '''
 
-    def private generateConstraints() '''
-        static {
-            «IF !restrictions.lengthConstraints.nullOrEmpty»
-            «generateLengthConstraints»
-            «ENDIF»
-        }
-    '''
-
-    private def lengthBody(Restrictions restrictions, Class<? extends Number> numberClass, String className, String varName) '''
-        «ImmutableList.importedName».Builder<«Range.importedName»<«numberClass.importedName»>> builder = «ImmutableList.importedName».builder();
-        «FOR r : restrictions.lengthConstraints»
-            builder.add(«Range.importedName».closed(«numericValue(numberClass, r.min)», «numericValue(numberClass, r.max)»));
-        «ENDFOR»
-        «varName» = builder.build();
-    '''
-
-    private def generateLengthConstraints() '''
-        «IF restrictions != null && !(restrictions.lengthConstraints.empty)»
-            «val numberClass = restrictions.lengthConstraints.iterator.next.min.class»
-            «IF numberClass.equals(typeof(BigDecimal))»
-                «lengthBody(restrictions, numberClass, genTO.importedName, "_length")»
-            «ELSE»
-                «lengthBody(restrictions, typeof(BigInteger), genTO.importedName, "_length")»
-            «ENDIF»
-        «ENDIF»
-    '''
-
     def protected allValuesConstructor() '''
     «IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
         @«ConstructorProperties.importedName»("value")
@@ -296,10 +276,9 @@ class ClassTemplate extends BaseTemplate {
 
             «FOR c : consts»
                 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME && c.value instanceof List<?>»
-            for (Pattern p : patterns) {
-                «Preconditions.importedName».checkArgument(p.matcher(_value).matches(), "Supplied value \"%s\" does not match any of the permitted patterns %s", _value, «TypeConstants.PATTERN_CONSTANT_NAME»);
-            }
-
+                for (Pattern p : patterns) {
+                    «Preconditions.importedName».checkArgument(p.matcher(_value).matches(), "Supplied value \"%s\" does not match any of the permitted patterns %s", _value, «TypeConstants.PATTERN_CONSTANT_NAME»);
+                }
                 «ENDIF»
             «ENDFOR»
         «ENDIF»
@@ -340,41 +319,30 @@ class ClassTemplate extends BaseTemplate {
     }
     '''
 
+    def private static paramValue(Type returnType, String paramName) {
+        if (returnType instanceof ConcreteType) {
+            return paramName
+        } else {
+            return paramName + ".getValue()"
+        }
+    }
+
     def private generateRestrictions(Type type, String paramName, Type returnType) '''
         «val restrictions = type.getRestrictions»
         «IF restrictions !== null»
-            «val boolean isNestedType = !(returnType instanceof ConcreteType)»
-            «IF !restrictions.lengthConstraints.empty»
-                «generateLengthRestriction(returnType, restrictions, paramName, isNestedType)»
-            «ENDIF»
-            «IF !restrictions.rangeConstraints.empty»
-                if («paramName» != null) {
-                    «IF isNestedType»
-                        «rangeGenerator.generateRangeCheckerCall(paramName, paramName + ".getValue()")»
-                    «ELSE»
-                        «rangeGenerator.generateRangeCheckerCall(paramName, paramName)»
-                    «ENDIF»
+            «IF !restrictions.lengthConstraints.empty || !restrictions.rangeConstraints.empty»
+            if («paramName» != null) {
+                «IF !restrictions.lengthConstraints.empty»
+                    «LengthGenerator.generateLengthCheckerCall(paramName, paramValue(returnType, paramName))»
+                «ENDIF»
+                «IF !restrictions.rangeConstraints.empty»
+                    «rangeGenerator.generateRangeCheckerCall(paramName, paramValue(returnType, paramName))»
+                «ENDIF»
                 }
             «ENDIF»
         «ENDIF»
     '''
 
-    def private generateLengthRestriction(Type returnType, Restrictions restrictions, String paramName, boolean isNestedType) '''
-        «val clazz = restrictions.lengthConstraints.iterator.next.min.class»
-        if («paramName» != null) {
-            «printLengthConstraint(returnType, clazz, paramName, isNestedType, returnType.name.contains("["))»
-            boolean isValidLength = false;
-            for («Range.importedName»<«clazz.importedNumber»> r : «IF isNestedType»«returnType.importedName».«ENDIF»length()) {
-                if (r.contains(_constraint)) {
-                    isValidLength = true;
-                }
-            }
-            if (!isValidLength) {
-                throw new IllegalArgumentException(String.format("Invalid length: %s, expected: %s.", «paramName», «IF isNestedType»«returnType.importedName».«ENDIF»length()));
-            }
-        }
-    '''
-
     def protected copyConstructor() '''
     /**
      * Creates a copy from Source Object.
@@ -548,14 +516,6 @@ class ClassTemplate extends BaseTemplate {
      * @return string with the class attributes in JAVA format
      */
     def protected generateFields() '''
-        «IF restrictions != null»
-            «val prop = getPropByName("value")»
-            «IF prop != null»
-                «IF !(restrictions.lengthConstraints.empty)»
-                    private static final «List.importedName»<«Range.importedName»<«prop.returnType.importedNumber»>> _length;
-                «ENDIF»
-            «ENDIF»
-        «ENDIF»
         «IF !properties.empty»
             «FOR f : properties»
                 private«IF f.readOnly» final«ENDIF» «f.returnType.importedName» «f.fieldName»;
index f82b9d7d4d5728b8e0efe389ac96de8ca7d506a8..91c0282ff0d1eb303d66c7df8f613c9debdda9fb 100644 (file)
@@ -50,7 +50,6 @@ public class TypedefCompilationTest extends BaseCompilationTest {
     private static final String VAL = "_value";
     private static final String GET_VAL = "getValue";
     private static final String UNITS = "_UNITS";
-    private static final String LENGTH = "_length";
 
     @Test
     public void test() throws Exception {
@@ -183,7 +182,6 @@ public class TypedefCompilationTest extends BaseCompilationTest {
         // typedef string-ext1
         assertFalse(stringExt1Class.isInterface());
         assertContainsField(stringExt1Class, VAL, String.class);
-        assertContainsField(stringExt1Class, LENGTH, List.class);
         assertContainsField(stringExt1Class, "patterns", Pattern[].class);
         assertContainsField(stringExt1Class, "PATTERN_CONSTANTS", List.class);
         assertContainsFieldWithValue(stringExt1Class, "serialVersionUID", Long.TYPE, 6943827552297110991L, String.class);
@@ -208,7 +206,6 @@ public class TypedefCompilationTest extends BaseCompilationTest {
 
         // typedef string-ext2
         assertFalse(stringExt2Class.isInterface());
-        assertContainsField(stringExt2Class, LENGTH, List.class);
         assertContainsFieldWithValue(stringExt2Class, "serialVersionUID", Long.TYPE, 8100233177432072092L, String.class);
         // assertEquals(2, stringExt2Class.getDeclaredFields().length);
         expectedConstructor = assertContainsConstructor(stringExt2Class, String.class);