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

fix: compare package names for equality between CtType #5556

Closed
wants to merge 8 commits into from

Conversation

algomaster99
Copy link
Contributor

Fixes #5536

The types are unequal because they are in different packages, which affects their fully qualified names. Since scanCtNamedElement compares based upon simpleName, the comparison would pass because it would not account for package names in names of types.

I am proposing changes that would compare types based on qualified names and would take any implicit packages into account. Another thing that I noticed was getValueByRole(CtRole.NAME) for a class returns its simple name. So, it is semantically wrong to set the role unequal based on comparing qualified names.

CtExpression<?> evaluated = ctBinaryOperator.partiallyEvaluate();
assertNotNull(
evaluated.getType(),
String.format("type of '%s' is null after evaluation", ctBinaryOperator)
);
assertEquals(currentType, evaluated.getType().getTypeDeclaration());
assertEquals(ctBinaryOperator.getType(), evaluated.getType());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should only check for the type of the binary operator and the evaluated result. There is no need to check the equality between type declarations.

@@ -91,6 +91,7 @@ public void testSpoonifier() throws ClassNotFoundException, MalformedURLExceptio
testSpoonifierWith("src/test/java/spoon/test/spoonifier/testclasses/ArrayRealVector.java", i++);
testSpoonifierWith("src/test/java/spoon/test/prettyprinter/testclasses/FooCasper.java", i++);
testSpoonifierWith("src/test/java/spoon/test/prettyprinter/testclasses/Rule.java", i++);
testSpoonifierWith("src/test/resources/UnnamedPackage.java", i++);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this test just to ensure that classes with unnamed packages can also be spoonified.

src/main/java/spoon/experimental/SpoonifierVisitor.java Outdated Show resolved Hide resolved
@algomaster99

This comment was marked as resolved.

@algomaster99
Copy link
Contributor Author

algomaster99 commented Nov 27, 2023

This test is failing because it clones a CtType into an unnamed package, and when it compares them, they are not equal because now we also compare based on package name. What is the contract around clone? What all should be cloned and what should be skipped? For example, I know that metadata is not cloned.

@algomaster99
Copy link
Contributor Author

07550ab is a bad solution to the problem above. But it passes the test if I explicitly set the package reference after clone. I will look for a better solution.

@I-Al-Istannen
Copy link
Collaborator

What is the contract around clone? What all should be cloned and what should be skipped? For example, I know that metadata is not cloned.

I think so far the parent relationships are not cloned, as that can lead to all kinds of trouble. If you set the parent, it might also attach itself in other ways and vice versa. Changing the parent or setting children potentially modifies quite a bit of the model.

I am not quite sure you always want to compare the package name as well. If you do that, there is no real way to do structural comparisons anymore, it is a lot easier to check the package name as well.

@algomaster99
Copy link
Contributor Author

Another plausible solution I thought was to change the definition of CtRole.NAME for types. Right now, it outputs the simple name, and I was thinking of adding a role handler that would output the qualified name instead. This comes as the cost of consistency in meaning of CtRole.NAME.

@algomaster99
Copy link
Contributor Author

The solution is discussed here.

@algomaster99 algomaster99 deleted the class-equals branch November 28, 2023 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

equals() semantics on CtType?
2 participants