Make wrapped class equals() final 66/83766/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 19 Aug 2019 14:21:51 +0000 (16:21 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 19 Aug 2019 14:26:05 +0000 (16:26 +0200)
This forbids overriding equals() for wrapper classes, meaning
the first class in hierarchy defines the proper equality
contract, which cannot be overridden.

This means that all objects within that hierarchy are considered
equal if they have the same representation (i.e. fields).

JIRA: MDSAL-440
Change-Id: I0c90ad9553071be8a8fc1109b663fe631b5599b9
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/ClassTemplate.xtend

index bd7c838db9e954e8f0a81fa0c807ed020ed46877..8504d1ad0344e6d913452e9b50f9e604d390c635 100644 (file)
@@ -506,20 +506,17 @@ class ClassTemplate extends BaseTemplate {
      *
      * @return string with the <code>equals()</code> method definition in JAVA format
      */
-    def protected generateEquals() '''
+    def private generateEquals() '''
         «IF !genTO.equalsIdentifiers.empty»
             @«Override.importedName»
-            public boolean equals(java.lang.Object obj) {
+            public final boolean equals(java.lang.Object obj) {
                 if (this == obj) {
                     return true;
                 }
-                if (obj == null) {
+                if (!(obj instanceof «type.name»)) {
                     return false;
                 }
-                if (getClass() != obj.getClass()) {
-                    return false;
-                }
-                «type.name» other = («type.name») obj;
+                final «type.name» other = («type.name») obj;
                 «FOR property : genTO.equalsIdentifiers»
                     «val fieldName = property.fieldName»
                     if (!«property.importedUtilClass».equals(«fieldName», other.«fieldName»)) {