INRIA / INRIA/spoon

[Bug]: Possible inconsistency in how CtComment is handled in CtFieldReferenceImpl

Open
#6,039 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
2k
Forks
392
Avg merge
11h 24m
Merged PRs (30d)
36

Description

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

```Java
@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 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 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
java.lang.AssertionError: expected: 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

```Java
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)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.