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: attach comment to CtLiteral nodes #4836

Merged
merged 6 commits into from
Aug 15, 2022

Conversation

algomaster99
Copy link
Contributor

I was trying to reproduce #4727 and I came across another possible bug in the Spoon model builder. It seems that when comments are added to CtCaseExpressions, they are not parsed and stored in the model. Is this a bug or does Spoon has some configuration that I need to enable to include comments in the model? Note that I already set one in my knowledge - launcher.getEnvironment().setCommentEnabled(true);.

@SirYwell
Copy link
Collaborator

SirYwell commented Aug 11, 2022

This doesn't look specific to case expressions or am I missing something? It seems like the comment isn't attached to the CtLiteral, but I didn't really figure out yet how the JDTCommentBuilder works (or is supposed to work). Adding a

			@Override
			public <T> void visitCtLiteral(CtLiteral<T> e) {
				e.addComment(comment);
			}

to this visitor

CtInheritanceScanner insertionVisitor = new CtInheritanceScanner() {

seems to do the trick, however I'm not sure if that's an appropriate fix at all (and it prints the comment in front of the literal, but that might be rather a printer issue - I don't now).

@algomaster99
Copy link
Contributor Author

algomaster99 commented Aug 12, 2022

This doesn't look specific to case expressions or am I missing something?

I observed it only for literals inside CtCaseExpression, so I did not want to generalise it. The fix you have proposed seems to intimate that this is a general problem for CtLiterals.

however I'm not sure if that's an appropriate fix at all (and it prints the comment in front of the literal, but that might be rather a printer issue - I don't now).

Let's fix the AST in this PR and then we can focus on printing comments.

EDIT:

I tried your fix. There is a very weird side-effect in this test. I will investigate this later.

@@ -415,7 +416,7 @@ class CounterScanner extends CtScanner {
@Override
public <T> void visitCtFieldWrite(CtFieldWrite<T> fieldWrite) {
visited++;
assertEquals("a", ((CtVariableWrite) fieldWrite.getTarget()).getVariable().getSimpleName());
assertEquals("a", ((CtVariableRead<?>) fieldWrite.getTarget()).getVariable().getSimpleName());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test was failing as I said here. I think it is supposed to be CtVariableRead because fieldWrite.getTarget evaluates to a in a.l in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably I had some local changes and that is why it was failing. I have reverted them in the latest commit.

@algomaster99 algomaster99 changed the title tests: reproduce absence of comment in the AST of CtCaseExpression fix: attach comment to CtLiteral nodes Aug 15, 2022
@algomaster99
Copy link
Contributor Author

@SirYwell Can you have a look again? I added your suggested fix which fixes the AST parsing.

String stageStr;
boolean fullStatus;
switch (stage) {
case (1/*org.apache.coyote.Constants.STAGE_PARSE*/):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you improve the test case and a comment before a case, e.g.,/*org.apache.coyote.Constants.STAGE_PARSE*/1

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 inline comment to add variety.

Copy link
Collaborator

@SirYwell SirYwell left a comment

Choose a reason for hiding this comment

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

Fix looks good, although I think the whole class needs some attention in future.

I left a comment how you can simplify the test.

@@ -57,6 +59,22 @@

public class AstCheckerTest {

@Test
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could use the @ModelTest annotation here instead. This way, you don't need the arrange block.
There are a few examples already you can look into if needed.

Copy link
Collaborator

@MartinWitt MartinWitt left a comment

Choose a reason for hiding this comment

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

Seems fine for me. If you want you can use the ModelTest way to write test cases.

@@ -57,6 +59,22 @@

public class AstCheckerTest {

@Test
void ctLiteralsInCtCaseExpressionShouldHaveCommentsAttached() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could use the new ModelTest Annotation here. This removes all of the arrange code.

@algomaster99 algomaster99 requested a review from SirYwell August 15, 2022 10:42
@MartinWitt MartinWitt merged commit 6a95f50 into INRIA:master Aug 15, 2022
@MartinWitt
Copy link
Collaborator

Thanks @algomaster99

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.

3 participants