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.
ElementNameMap.equals() only returns equal if the InsertOrderWrapper internal instances are exactly the same, as they don't implement hashCode() and equals() so that unfortunately means that equal() can only return true if the other object is the same ElementNameMap instance.
ElementNameMap.equals() does not consider the order of the wrapped elements. That is the wrapped elements in two separate ElementNameMap instances can have substantially different insertionNumbers, but have the same order and contain equivalent objects, so that for those cases the equal() test should return true. This can be solved by comparing the elements in equal() by their correct order using entrySet(...) to check.
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
Describe the bug
ElementNameMap.equals() only returns equal if the InsertOrderWrapper internal instances are exactly the same, as they don't implement hashCode() and equals() so that unfortunately means that equal() can only return true if the other object is the same ElementNameMap instance.
ElementNameMap.equals() does not consider the order of the wrapped elements. That is the wrapped elements in two separate ElementNameMap instances can have substantially different insertionNumbers, but have the same order and contain equivalent objects, so that for those cases the equal() test should return true. This can be solved by comparing the elements in equal() by their correct order using entrySet(...) to check.
Proposed solution patch:
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