Do not generate prime when not needed 18/110318/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 23 Feb 2024 22:30:47 +0000 (23:30 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 23 Feb 2024 22:37:08 +0000 (23:37 +0100)
In case we do not have any properties we end up emitting a local
variable which we'll never use -- generating a compiler warning.

Improve InterfaceTemplate to check for this condition and emit the
prime only if there are actually at least one property.

Change-Id: I3341ca075e8c7c0b671a76f226b7bdb99cb12465
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/InterfaceTemplate.xtend
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderGeneratorTest.java

index 53900de421ecca9162bd5a12d1c3f30e6f5d559d..a6d549069fbf7f51ac7221f05655c546c7c6f605 100644 (file)
@@ -299,11 +299,14 @@ class InterfaceTemplate extends BaseTemplate {
              * @throws «NPE.importedName» if {@code obj} is {@code null}
              */
             static int «BINDING_HASHCODE_NAME»(final «type.fullyQualifiedNonNull» obj) {
-                final int prime = 31;
                 int result = 1;
-                «FOR property : typeAnalysis.value»
-                    result = prime * result + «property.importedUtilClass».hashCode(obj.«property.getterMethodName»());
-                «ENDFOR»
+                «val props = typeAnalysis.value»
+                «IF !props.empty»
+                    final int prime = 31;
+                    «FOR property : props»
+                        result = prime * result + «property.importedUtilClass».hashCode(obj.«property.getterMethodName»());
+                    «ENDFOR»
+                «ENDIF»
                 «IF augmentable»
                     for (var augmentation : obj.augmentations().values()) {
                         result += augmentation.hashCode();
index 08e2643c9122dcc314be3dc30d8d3c5608062e1e..c7df05746be788041702f08c8face049d73bd421 100644 (file)
@@ -51,8 +51,8 @@ public class BuilderGeneratorTest {
              * @throws NullPointerException if {@code obj} is {@code null}
              */
             static int bindingHashCode(final test.@NonNull test obj) {
-                final int prime = 31;
                 int result = 1;
+                final int prime = 31;
                 result = prime * result + Objects.hashCode(obj.getTest());
                 return result;
             }
@@ -78,8 +78,8 @@ public class BuilderGeneratorTest {
              * @throws NullPointerException if {@code obj} is {@code null}
              */
             static int bindingHashCode(final test.@NonNull test obj) {
-                final int prime = 31;
                 int result = 1;
+                final int prime = 31;
                 result = prime * result + Objects.hashCode(obj.getTest1());
                 result = prime * result + Objects.hashCode(obj.getTest2());
                 return result;
@@ -101,7 +101,6 @@ public class BuilderGeneratorTest {
              * @throws NullPointerException if {@code obj} is {@code null}
              */
             static int bindingHashCode(final test.@NonNull test obj) {
-                final int prime = 31;
                 int result = 1;
                 for (var augmentation : obj.augmentations().values()) {
                     result += augmentation.hashCode();
@@ -125,8 +124,8 @@ public class BuilderGeneratorTest {
              * @throws NullPointerException if {@code obj} is {@code null}
              */
             static int bindingHashCode(final test.@NonNull test obj) {
-                final int prime = 31;
                 int result = 1;
+                final int prime = 31;
                 result = prime * result + Objects.hashCode(obj.getTest());
                 for (var augmentation : obj.augmentations().values()) {
                     result += augmentation.hashCode();
@@ -150,8 +149,8 @@ public class BuilderGeneratorTest {
              * @throws NullPointerException if {@code obj} is {@code null}
              */
             static int bindingHashCode(final test.@NonNull test obj) {
-                final int prime = 31;
                 int result = 1;
+                final int prime = 31;
                 result = prime * result + Objects.hashCode(obj.getTest1());
                 result = prime * result + Objects.hashCode(obj.getTest2());
                 for (var augmentation : obj.augmentations().values()) {