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]: Possible inconsistency in how CtComment is handled in CtFieldReferenceImpl #6039

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

Comments

@CoreRasurae
Copy link
Contributor

Describe the bug

Comments can be added to CtFieldReferenceImpl, but they are not considered for cloning and equals methods. Given that comments are disallowed for references in general then why can we add them to CtFieldReferenceImpl and they get stored?
Maybe it is just a misunderstanding of mine, but there seems to be an inconsistency in how this is handled compared to other Ct elements, as for example there are other Ct references that discard the comments when they are added.

Source code you are trying to analyze/transform

No response

Source code for your Spoon processing

@Test
    public void spoonTest() {
        spoon.support.DefaultCoreFactory dfc = new DefaultCoreFactory();
        spoon.compiler.Environment e = new spoon.support.StandardEnvironment();
        spoon.reflect.factory.Factory f = new spoon.reflect.factory.FactoryImpl(dfc, e);
        CtComment cmt = f.createComment();
        cmt.setContent("This is a comment");
        CtFieldReference<String> cfr = f.createFieldReference();
        cfr.setSimpleName("field1");
        //NOTE: References do not allow comments to be set... but we can add one... 
        //that it is not cloned, but preferably it should not be added either...
        cfr.addComment(cmt);
        cmt.setParent(cmt);
        
        assertEquals("FieldReference should have one comment", 1, cfr.getComments().size());
        
        CtFieldReference<String> clonedCfr = cfr.clone();
        assertEquals("Cloned and original objects should be equal", cfr, clonedCfr);
        assertEquals("Cloned object should have one comment", clonedCfr.getComments().size());
    }

Actual output

java.lang.AssertionError: expected:<Cloned object should have one comment> but was:<0>
	at org.junit.Assert.fail(Assert.java:89)
	at org.junit.Assert.failNotEquals(Assert.java:835)
	at org.junit.Assert.assertEquals(Assert.java:120)
	at org.junit.Assert.assertEquals(Assert.java:146)
	at FieldReferenceImplTest.spoonTest(LaastFieldReferenceImplTest.java:17)

Expected output

If comments are not supported for references, then why can we add one to CtFieldReference? I believe that at a minimum the addComment(...) should discard the comment, or an exception should be thrown.

Spoon Version

11.1.0

JVM Version

Java 17

What operating system are you using?

NixOS 24.11 (Vicuna)

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