Skip bindingHashCode() generation on properties' absence 94/91494/5
authorIlya Igushev <illia.ihushev@pantheon.tech>
Tue, 21 Jul 2020 08:02:32 +0000 (11:02 +0300)
committerRobert Varga <nite@hq.sk>
Mon, 3 Aug 2020 07:56:07 +0000 (07:56 +0000)
When DataObject implementation has no properties, augmentations, its
hashcode() wouldn't be overridden, so bindingHashcode() remains unused.

JIRA: MDSAL-471
Change-Id: If2f7bbb65f9dccf2353ba22b49d530f2a1ab4a71
Signed-off-by: illia.ihushev <illia.ihushev@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 62d1df8d5c358f7c01c64be62711173885e4e5d2..9007afcfdf2712630b91cd2e786f10726d290d38 100644 (file)
@@ -224,34 +224,36 @@ class InterfaceTemplate extends BaseTemplate {
     @VisibleForTesting
     def generateBindingHashCode() '''
         «val augmentable = analyzeType»
-        /**
-         * Default implementation of {@link «Object.importedName»#hashCode()} contract for this interface.
-         * Implementations of this interface are encouraged to defer to this method to get consistent hashing
-         * results across all implementations.
-         *
-         «IF augmentable»
-         * @param <T$$> implementation type, which has to also implement «AUGMENTATION_HOLDER.importedName» interface
-         *              contract.
-         «ENDIF»
-         * @param obj Object for which to generate hashCode() result.
-         * @return Hash code value of data modeled by this interface.
-         * @throws «NPE.importedName» if {@code obj} is null
-         */
-        «IF augmentable»
-        static <T$$ extends «type.fullyQualifiedName» & «AUGMENTATION_HOLDER.importedName»<?>> int «BINDING_HASHCODE_NAME»(final @«NONNULL.importedName» T$$ obj) {
-        «ELSE»
-        static int «BINDING_HASHCODE_NAME»(final «type.fullyQualifiedName» obj) {
-        «ENDIF»
-            final int prime = 31;
-            int result = 1;
-            «FOR property : typeAnalysis.value»
-                result = prime * result + «property.importedUtilClass».hashCode(obj.«property.getterMethodName»());
-            «ENDFOR»
+        «IF augmentable || !typeAnalysis.value.empty»
+            /**
+             * Default implementation of {@link «Object.importedName»#hashCode()} contract for this interface.
+             * Implementations of this interface are encouraged to defer to this method to get consistent hashing
+             * results across all implementations.
+             *
+             «IF augmentable»
+             * @param <T$$> implementation type, which has to also implement «AUGMENTATION_HOLDER.importedName» interface
+             *              contract.
+             «ENDIF»
+             * @param obj Object for which to generate hashCode() result.
+             * @return Hash code value of data modeled by this interface.
+             * @throws «NPE.importedName» if {@code obj} is null
+             */
             «IF augmentable»
-                result = prime * result + «CODEHELPERS.importedName».hashAugmentations(obj);
+                static <T$$ extends «type.fullyQualifiedName» & «AUGMENTATION_HOLDER.importedName»<?>> int «BINDING_HASHCODE_NAME»(final @«NONNULL.importedName» T$$ obj) {
+            «ELSE»
+                static int «BINDING_HASHCODE_NAME»(final «type.fullyQualifiedName» obj) {
             «ENDIF»
-            return result;
-        }
+                final int prime = 31;
+                int result = 1;
+                «FOR property : typeAnalysis.value»
+                    result = prime * result + «property.importedUtilClass».hashCode(obj.«property.getterMethodName»());
+                «ENDFOR»
+                «IF augmentable»
+                    result = prime * result + «CODEHELPERS.importedName».hashAugmentations(obj);
+                «ENDIF»
+                return result;
+            }
+        «ENDIF»
     '''
 
     def private generateBindingEquals() '''
index 08c0482d83750b2dc18f7449dae34dda48b3a7c4..7fb78485e519cce09f62fe5fc4e2db228c0a6c2f 100644 (file)
@@ -56,6 +56,11 @@ public class BuilderGeneratorTest {
                 + "}\n", genHashCode(genType).toString());
     }
 
+    @Test
+    public void builderTemplateGenerateHashCodeWithoutAnyPropertyTest() throws Exception {
+        assertEquals("", genHashCode(mockGenType(TEST)).toString());
+    }
+
     @Test
     public void builderTemplateGenerateHashCodeWithMorePropertiesTest() throws Exception {
         assertEquals("/**\n"