From f43fc0955c2eb1f50d66c1a9b70e88d37427b047 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 4 Sep 2019 16:17:32 +0200 Subject: [PATCH] Cache computed fieldName in templates 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 (cherry picked from commit 72585e1dff2c81af33f547b49694f7c976033bed) --- .../java/api/generator/BaseTemplate.xtend | 14 ++++++++------ .../java/api/generator/ClassTemplate.xtend | 18 ++++++++++-------- .../java/api/generator/UnionTemplate.xtend | 5 +++-- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BaseTemplate.xtend b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BaseTemplate.xtend index f9877fb359..44bb515533 100644 --- a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BaseTemplate.xtend +++ b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BaseTemplate.xtend @@ -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» ''' diff --git a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ClassTemplate.xtend b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ClassTemplate.xtend index 155681ee9d..bbdc6a3df8 100644 --- a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ClassTemplate.xtend +++ b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ClassTemplate.xtend @@ -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» } ''' diff --git a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/UnionTemplate.xtend b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/UnionTemplate.xtend index 477ee19ace..fe1b03cc1d 100644 --- a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/UnionTemplate.xtend +++ b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/UnionTemplate.xtend @@ -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» } -- 2.36.6