From 0d0cba104a757eaa0b6fc889cd6d7c06e3e79026 Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Thu, 11 Aug 2022 12:15:48 +0200 Subject: [PATCH 1/6] Reproduce absence of comment in the AST of CtCaseExpression --- .../spoon/reflect/ast/AstCheckerTest.java | 18 ++++++++++++++++++ .../comment/CommentsOnCaseExpression.java | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/test/resources/comment/CommentsOnCaseExpression.java diff --git a/src/test/java/spoon/reflect/ast/AstCheckerTest.java b/src/test/java/spoon/reflect/ast/AstCheckerTest.java index c085dc15f2f..9b3a7d44e7d 100644 --- a/src/test/java/spoon/reflect/ast/AstCheckerTest.java +++ b/src/test/java/spoon/reflect/ast/AstCheckerTest.java @@ -22,8 +22,10 @@ import spoon.reflect.declaration.CtClass; import spoon.reflect.reference.CtExecutableReference; import spoon.support.modelobs.FineModelChangeListener; +import spoon.reflect.CtModel; import spoon.reflect.code.CtBinaryOperator; import spoon.reflect.code.CtBlock; +import spoon.reflect.code.CtComment; import spoon.reflect.code.CtExpression; import spoon.reflect.code.CtFieldRead; import spoon.reflect.code.CtIf; @@ -57,6 +59,22 @@ public class AstCheckerTest { + @Test + void ctLiteralsInCtCaseExpressionShouldHaveCommentsAttached() { + // contract: literal nodes should have comments attached to them. + // arrange + Launcher launcher = new Launcher(); + launcher.addInputResource("src/test/resources/comment/CommentsOnCaseExpression.java"); + launcher.getEnvironment().setCommentEnabled(true); + CtModel model = launcher.buildModel(); + + // act + List comments = model.getElements(new TypeFilter<>(CtComment.class)); + + // assert + assertThat(comments.size(), equalTo(3)); + } + @Test void leftOperandShouldBeGivenPriorityForStoringTheNestedOperator_stringLiteralConcatenation() { // contract: string concatenation should be left associative. diff --git a/src/test/resources/comment/CommentsOnCaseExpression.java b/src/test/resources/comment/CommentsOnCaseExpression.java new file mode 100644 index 00000000000..3b62f1e3403 --- /dev/null +++ b/src/test/resources/comment/CommentsOnCaseExpression.java @@ -0,0 +1,19 @@ +public class CommentsOnCaseExpression { + public void commentsShouldBeAttachedToCtLiteral(int stage) { + String stageStr; + boolean fullStatus; + switch (stage) { + case (1/*org.apache.coyote.Constants.STAGE_PARSE*/): + stageStr = "P"; + fullStatus = false; + break; + case (2/*org.apache.coyote.Constants.STAGE_PREPARE*/): + stageStr = "P"; + fullStatus = false; + break; + case (3/*org.apache.coyote.Constants.STAGE_SERVICE*/): + stageStr = "S"; + break; + } + } +} From 5d105542460a9f3394e8488a63a661beb6f0683e Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Mon, 15 Aug 2022 11:30:01 +0200 Subject: [PATCH 2/6] Attach comment to `CtLiteral` --- .../java/spoon/support/compiler/jdt/JDTCommentBuilder.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java index f3352ebb89f..87e37c3c12e 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java @@ -22,6 +22,7 @@ import spoon.reflect.code.CtConditional; import spoon.reflect.code.CtIf; import spoon.reflect.code.CtLambda; +import spoon.reflect.code.CtLiteral; import spoon.reflect.code.CtNewArray; import spoon.reflect.code.CtStatement; import spoon.reflect.code.CtStatementList; @@ -434,6 +435,11 @@ public void visitCtIf(CtIf e) { } } + @Override + public void visitCtLiteral(CtLiteral e) { + e.addComment(comment); + } + @Override public void scanCtStatement(CtStatement s) { if (!(s instanceof CtStatementList || s instanceof CtSwitch || s instanceof CtVariable)) { From 87fed7d8eb24b5141d699a97acde63b30142f932 Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Mon, 15 Aug 2022 11:30:20 +0200 Subject: [PATCH 3/6] Fix failing test case --- src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java b/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java index 18df86e9129..1533ade2be3 100644 --- a/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java +++ b/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java @@ -36,6 +36,7 @@ import spoon.reflect.code.CtStatement; import spoon.reflect.code.CtTypeAccess; import spoon.reflect.code.CtUnaryOperator; +import spoon.reflect.code.CtVariableRead; import spoon.reflect.code.CtVariableWrite; import spoon.reflect.code.UnaryOperatorKind; import spoon.reflect.declaration.CtClass; @@ -415,7 +416,7 @@ class CounterScanner extends CtScanner { @Override public void visitCtFieldWrite(CtFieldWrite fieldWrite) { visited++; - assertEquals("a", ((CtVariableWrite) fieldWrite.getTarget()).getVariable().getSimpleName()); + assertEquals("a", ((CtVariableRead) fieldWrite.getTarget()).getVariable().getSimpleName()); assertEquals("l", fieldWrite.getVariable().getSimpleName()); } } From c53f9904932326c5ca00c236d49f3b20aeafb3fa Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Mon, 15 Aug 2022 11:45:20 +0200 Subject: [PATCH 4/6] Add inline comment in test file --- src/test/java/spoon/reflect/ast/AstCheckerTest.java | 2 +- src/test/resources/comment/CommentsOnCaseExpression.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/spoon/reflect/ast/AstCheckerTest.java b/src/test/java/spoon/reflect/ast/AstCheckerTest.java index 9b3a7d44e7d..3314cff9d47 100644 --- a/src/test/java/spoon/reflect/ast/AstCheckerTest.java +++ b/src/test/java/spoon/reflect/ast/AstCheckerTest.java @@ -72,7 +72,7 @@ void ctLiteralsInCtCaseExpressionShouldHaveCommentsAttached() { List comments = model.getElements(new TypeFilter<>(CtComment.class)); // assert - assertThat(comments.size(), equalTo(3)); + assertThat(comments.size(), equalTo(4)); } @Test diff --git a/src/test/resources/comment/CommentsOnCaseExpression.java b/src/test/resources/comment/CommentsOnCaseExpression.java index 3b62f1e3403..08ff1514316 100644 --- a/src/test/resources/comment/CommentsOnCaseExpression.java +++ b/src/test/resources/comment/CommentsOnCaseExpression.java @@ -3,6 +3,7 @@ public void commentsShouldBeAttachedToCtLiteral(int stage) { String stageStr; boolean fullStatus; switch (stage) { + // Inline comments are also a part now case (1/*org.apache.coyote.Constants.STAGE_PARSE*/): stageStr = "P"; fullStatus = false; From 59ccc5a6c5e454197ae795b72cfcbcdfa874ed8e Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Mon, 15 Aug 2022 12:01:35 +0200 Subject: [PATCH 5/6] Revert "Fix failing test case" This reverts commit 87fed7d8eb24b5141d699a97acde63b30142f932. --- src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java b/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java index 1533ade2be3..18df86e9129 100644 --- a/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java +++ b/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java @@ -36,7 +36,6 @@ import spoon.reflect.code.CtStatement; import spoon.reflect.code.CtTypeAccess; import spoon.reflect.code.CtUnaryOperator; -import spoon.reflect.code.CtVariableRead; import spoon.reflect.code.CtVariableWrite; import spoon.reflect.code.UnaryOperatorKind; import spoon.reflect.declaration.CtClass; @@ -416,7 +415,7 @@ class CounterScanner extends CtScanner { @Override public void visitCtFieldWrite(CtFieldWrite fieldWrite) { visited++; - assertEquals("a", ((CtVariableRead) fieldWrite.getTarget()).getVariable().getSimpleName()); + assertEquals("a", ((CtVariableWrite) fieldWrite.getTarget()).getVariable().getSimpleName()); assertEquals("l", fieldWrite.getVariable().getSimpleName()); } } From 3f914ab074319423f076fcf6a667acc734fe724b Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Mon, 15 Aug 2022 12:41:47 +0200 Subject: [PATCH 6/6] Use ModelTest annotation --- src/test/java/spoon/reflect/ast/AstCheckerTest.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/test/java/spoon/reflect/ast/AstCheckerTest.java b/src/test/java/spoon/reflect/ast/AstCheckerTest.java index 3314cff9d47..da7b5d51450 100644 --- a/src/test/java/spoon/reflect/ast/AstCheckerTest.java +++ b/src/test/java/spoon/reflect/ast/AstCheckerTest.java @@ -44,6 +44,7 @@ import spoon.support.comparator.CtLineElementComparator; import spoon.support.util.internal.ElementNameMap; import spoon.support.util.ModelList; +import spoon.testing.utils.ModelTest; import java.util.Arrays; import java.util.Collection; @@ -59,15 +60,9 @@ public class AstCheckerTest { - @Test - void ctLiteralsInCtCaseExpressionShouldHaveCommentsAttached() { + @ModelTest("src/test/resources/comment/CommentsOnCaseExpression.java") + void ctLiteralsInCtCaseExpressionShouldHaveCommentsAttached(CtModel model) { // contract: literal nodes should have comments attached to them. - // arrange - Launcher launcher = new Launcher(); - launcher.addInputResource("src/test/resources/comment/CommentsOnCaseExpression.java"); - launcher.getEnvironment().setCommentEnabled(true); - CtModel model = launcher.buildModel(); - // act List comments = model.getElements(new TypeFilter<>(CtComment.class));