Cache computed fieldName in templates 96/84396/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 4 Sep 2019 14:17:32 +0000 (16:17 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 13 Sep 2019 14:24:08 +0000 (16:24 +0200)
We end up using the same name multiple times, which really is an
invariant. Cache and reuse the constant, so that we end up with
more efficient code.

Change-Id: I6ee5cc162c43c542a08ba84250f42d9a30ba3f0d
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 72585e1dff2c81af33f547b49694f7c976033bed)

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/ClassTemplate.xtend
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/UnionTemplate.xtend

index f9877fb359cae42d757393f009badb34d4f7ee48..44bb515533056ca9bcc5b1700279b8375ec453fe 100644 (file)
@@ -108,10 +108,11 @@ abstract class BaseTemplate extends JavaFileTemplate {
     protected def getterMethod(GeneratedProperty field) {
         '''
             public «field.returnType.importedName» «field.getterMethodName»() {
+                «val fieldName = field.fieldName»
                 «IF field.returnType.importedName.contains("[]")»
-                return «field.fieldName» == null ? null : «field.fieldName».clone();
+                return «fieldName» == null ? null : «fieldName».clone();
                 «ELSE»
-                return «field.fieldName»;
+                return «fieldName»;
                 «ENDIF»
             }
         '''
@@ -453,19 +454,20 @@ abstract class BaseTemplate extends JavaFileTemplate {
                «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value + ".getValue()")»
            «ENDIF»
        «ENDIF»
+       «val fieldName = property.fieldName»
        «IF restrictions.getLengthConstraint.isPresent»
            «IF actualType instanceof ConcreteType»
-               «LengthGenerator.generateLengthCheckerCall(property.fieldName.toString, value)»
+               «LengthGenerator.generateLengthCheckerCall(fieldName.toString, value)»
            «ELSE»
-               «LengthGenerator.generateLengthCheckerCall(property.fieldName.toString, value + ".getValue()")»
+               «LengthGenerator.generateLengthCheckerCall(fieldName.toString, value + ".getValue()")»
            «ENDIF»
        «ENDIF»
 
-       «val fieldUpperCase = property.fieldName.toString.toUpperCase(Locale.ENGLISH)»
+       «val fieldUpperCase = fieldName.toString.toUpperCase(Locale.ENGLISH)»
        «FOR currentConstant : type.getConstantDefinitions»
            «IF currentConstant.getName.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)
                && fieldUpperCase.equals(currentConstant.getName.substring(TypeConstants.PATTERN_CONSTANT_NAME.length))»
-           «CodeHelpers.importedName».checkPattern(value, «Constants.MEMBER_PATTERN_LIST»«property.fieldName», «Constants.MEMBER_REGEX_LIST»«property.fieldName»);
+           «CodeHelpers.importedName».checkPattern(value, «Constants.MEMBER_PATTERN_LIST»«fieldName», «Constants.MEMBER_REGEX_LIST»«fieldName»);
            «ENDIF»
        «ENDFOR»
     '''
index 155681ee9dfb5cb4b3ac68ca1f21327a2f5488f8..bbdc6a3df80f21864955cd68009275918b89da4d 100644 (file)
@@ -226,15 +226,15 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
 
         «FOR p : properties»
+            «val fieldName = p.fieldName»
             «IF p.returnType.importedName.contains("[]")»
-                «IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name
-                .equals("value")»
-                this.«p.fieldName» = «p.fieldName».clone();
+                «IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
+                this.«fieldName» = «fieldName».clone();
                 «ELSE»
-                this.«p.fieldName» = «p.fieldName» == null ? null : «p.fieldName».clone();
+                this.«fieldName» = «fieldName» == null ? null : «fieldName».clone();
                 «ENDIF»
             «ELSE»
-            this.«p.fieldName» = «p.fieldName»;
+            this.«fieldName» = «fieldName»;
             «ENDIF»
         «ENDFOR»
     }
@@ -257,9 +257,10 @@ class ClassTemplate extends BaseTemplate {
             super(«parentProperties.asArguments»);
         «ENDIF»
 
-        «generateRestrictions(type, property.fieldName.toString, property.returnType)»
+        «val fieldName = property.fieldName»
+        «generateRestrictions(type, fieldName.toString, property.returnType)»
 
-        this.«property.fieldName» = «property.name»;
+        this.«fieldName» = «property.name»;
         «FOR p : other»
             this.«p.fieldName» = null;
         «ENDFOR»
@@ -309,7 +310,8 @@ class ClassTemplate extends BaseTemplate {
             super(source);
         «ENDIF»
         «FOR p : properties»
-            this.«p.fieldName» = source.«p.fieldName»;
+            «val fieldName = p.fieldName»
+            this.«fieldName» = source.«fieldName»;
         «ENDFOR»
     }
     '''
index 477ee19ace35a486635d3470bb18b331d9fd0402..fe1b03cc1d70df54a01ff74cfbd6c098de2cfc92 100644 (file)
@@ -170,10 +170,11 @@ class UnionTemplate extends ClassTemplate {
                 super(source);
             «ENDIF»
             «FOR p : properties»
+                «val fieldName = p.fieldName»
                 «IF p.returnType.importedName.contains("[]")»
-                this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
+                this.«fieldName» = source.«fieldName» == null ? null : source.«fieldName».clone();
                 «ELSE»
-                this.«p.fieldName» = source.«p.fieldName»;
+                this.«fieldName» = source.«fieldName»;
                 «ENDIF»
             «ENDFOR»
         }