Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
CoreRasurae opened this issue Oct 23, 2024 · 0 comments
Labels

Comments

@CoreRasurae
Copy link
Contributor

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:

--- 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants
@CoreRasurae and others