INRIA / spoon

Spoon is a metaprogramming library to analyze and transform Java source code. :spoon: is made with :heart:, :beers: and :sparkles:. It parses source files to build a well-designed AST with powerful analysis and transformation API.
http://spoon.gforge.inria.fr/
Other
1.76k stars 352 forks source link

[Bug]: spoon.support.util.internal.NamedElementMap has a bug in equals() method #6033

Open CoreRasurae opened 1 month ago

CoreRasurae commented 1 month ago

Describe the bug

Proposed solution patch:

--- a/src/main/java/spoon/support/util/internal/ElementNameMap.java 2024-10-23 02:02:48.000000000 +0100
+++ b/src/main/java/spoon/support/util/internal/ElementNameMap.java 2024-10-23 16:34:58.573946074 +0100
@@ -71,6 +71,28 @@
            this.value = value;
            this.insertionNumber = insertionNumber;
        }
+       
+       @Override
+       public int hashCode() {
+           return Objects.hash(this.value);
+       }
+
+       @Override
+       public boolean equals(Object o) {
+           if (this == o) {
+               return true;
+           }
+           if (o == null || getClass() != o.getClass()) {
+               return false;
+           }
+            
+           InsertOrderWrapper<?> that = (InsertOrderWrapper<?>) o;
+
+           //Insertion number is not relevant for comparing the wrapped elements as long as they
+           //are presented in the same ordering by entrySet().
+           return Objects.equals(this.value, that.value);
+           }
+
    }

@@ -211,7 +233,7 @@
            return false;
        }
        ElementNameMap<?> that = (ElementNameMap<?>) o;
-       return Objects.equals(map, that.map);
+       return Objects.equals(entrySet(), that.entrySet());
    }

    @Override

Source code you are trying to analyze/transform

No response

Source code for your Spoon processing

No response

Actual output

No response

Expected output

No response

Spoon Version

Latest from master

JVM Version

Not a JVM version issue

What operating system are you using?

NixOS Linux, but again not relevant for the problem