-
-
Notifications
You must be signed in to change notification settings - Fork 352
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
equals()
semantics on CtType
?
#5536
Comments
Hi! This seems like a bug that we do not consider the package of the type in our diff --git a/src/main/java/spoon/support/visitor/equals/EqualsChecker.java b/src/main/java/spoon/support/visitor/equals/EqualsChecker.java
index 20117347a..6a0e7d9ee 100644
--- a/src/main/java/spoon/support/visitor/equals/EqualsChecker.java
+++ b/src/main/java/spoon/support/visitor/equals/EqualsChecker.java
@@ -22,6 +22,8 @@ import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtModifiable;
import spoon.reflect.declaration.CtNamedElement;
import spoon.reflect.declaration.CtParameter;
+import spoon.reflect.declaration.CtType;
+import spoon.reflect.declaration.CtTypeInformation;
import spoon.reflect.path.CtRole;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtExecutableReference;
@@ -119,6 +121,15 @@ public class EqualsChecker extends CtInheritanceScanner {
super.scanCtCodeSnippet(snippet);
}
+ @Override
+ public <T> void scanCtTypeInformation(CtTypeInformation typeInformation) {
+ final CtTypeInformation peek = (CtTypeInformation) this.other;
+ if (!typeInformation.getQualifiedName().equals(peek.getQualifiedName())) {
+ setNotEqual(null);
+ }
+ super.scanCtTypeInformation(typeInformation);
+ }
+
@Override
public <T, A extends T> void visitCtAssignment(CtAssignment<T, A> assignment) {
if (!(assignment instanceof CtOperatorAssignment) && this.other instanceof CtOperatorAssignment) {
However, we must also check for implicit packages, |
That is an interesting question. I think currently equality checks whether the elements have the same content and attributes, ignoring the parents. As the qualified name is a derived property (based on the parent chain) it is not included in the comparison. Currently you can easily emulate the nominal behaviour by using a WDYT @MartinWitt @SirYwell? |
So based on Nominal type system, it would check two things:
How would you compare the type declaration without visiting each child's node? |
You don't. I thought they only wanted equality to be based on the name. Doing both (full name and content) sounds a bit weird to me at first glance. But the wrapper was something they can implement in their code, not in Spoon itself :) |
Hmmm. If full names are the same, then classes must be the same; if different, they must be different. |
This makes sense and explains this behavior.
Of course. I had to implement a little workaround for my use case, but it does not mean that Spoon should work any differently. Thank you both for clarifying. |
I'm trying to understand the semantics of
equals()
on Spoon'sCtType
s.Consider two packages,
a
andb
, which both contain an empty classC
:In this case, the
equals()
method ona.C
andb.C
returnstrue
, even though their qualified names differ and they are arguably two different types.In the following case, the
equals()
method ona.C
andb.D
unsurprisingly returnsfalse
:Now, if we insert two identical methods in
a.C
andb.C
,equals()
returnstrue
again:But if their names differ,
equals()
returnsfalse
again:It seems that
equals()
is checking for structural equivalence rather than nominal equivalence (but not strictly because in the second caseC
andD
are not considered equal). Is this the intended semantics? The main issue is that it makes some analyses awkward to write. If I analyze a project with Spoon and insert all its types in aSet
, many of them disappear, though they are indeed distinct types according to Java's semantics. Generally, manipulating Spoon types in collections is often awkward. It seems that the valid way to check for equality according to Java's semantics is to compare qualified names, or to always useCtTypeReference
instead which does not suffer from this problem.For reference, the minimal test. This is with Spoon 10.4.2.
The text was updated successfully, but these errors were encountered: