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)) { diff --git a/src/test/java/spoon/reflect/ast/AstCheckerTest.java b/src/test/java/spoon/reflect/ast/AstCheckerTest.java index c085dc15f2f..da7b5d51450 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; @@ -42,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; @@ -57,6 +60,16 @@ public class AstCheckerTest { + @ModelTest("src/test/resources/comment/CommentsOnCaseExpression.java") + void ctLiteralsInCtCaseExpressionShouldHaveCommentsAttached(CtModel model) { + // contract: literal nodes should have comments attached to them. + // act + List comments = model.getElements(new TypeFilter<>(CtComment.class)); + + // assert + assertThat(comments.size(), equalTo(4)); + } + @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..08ff1514316 --- /dev/null +++ b/src/test/resources/comment/CommentsOnCaseExpression.java @@ -0,0 +1,20 @@ +public class CommentsOnCaseExpression { + 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; + 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; + } + } +}