diff --git a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java index 0d783b7473d..51baf0b7da4 100644 --- a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java +++ b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java @@ -819,15 +819,37 @@ public void visitCtClass(CtClass ctClass) { public void visitCtConditional(CtConditional conditional) { enterCtExpression(conditional); - scan(conditional.getCondition()); + CtExpression condition = conditional.getCondition(); + if (!(condition instanceof CtStatement)) { + printComment(condition, CommentOffset.BEFORE); + } + scan(condition); + if (!(condition instanceof CtStatement)) { + printComment(condition, CommentOffset.AFTER); + } write(" ? "); - scan(conditional.getThenExpression()); + CtExpression thenExpression = conditional.getThenExpression(); + if (!(thenExpression instanceof CtStatement)) { + printComment(thenExpression, CommentOffset.BEFORE); + } + scan(thenExpression); + if (!(thenExpression instanceof CtStatement)) { + printComment(thenExpression, CommentOffset.AFTER); + } write(" : "); + + CtExpression elseExpression = conditional.getElseExpression(); boolean isAssign = false; - if ((isAssign = conditional.getElseExpression() instanceof CtAssignment)) { + if ((isAssign = elseExpression instanceof CtAssignment)) { write("("); } - scan(conditional.getElseExpression()); + if (!(elseExpression instanceof CtStatement)) { + printComment(elseExpression, CommentOffset.BEFORE); + } + scan(elseExpression); + if (!(elseExpression instanceof CtStatement)) { + printComment(elseExpression, CommentOffset.AFTER); + } if (isAssign) { write(")"); } @@ -1614,7 +1636,8 @@ private List getComments(CtElement e, CommentOffset offset) { } continue; } - if (offset == CommentOffset.BEFORE && comment.getPosition().getLine() <= e.getPosition().getLine()) { + if (offset == CommentOffset.BEFORE && (comment.getPosition().getLine() < e.getPosition().getLine() + || e.getPosition().getSourceEnd() >= comment.getPosition().getSourceEnd())) { commentsToPrint.add(comment); } else if (offset == CommentOffset.AFTER && comment.getPosition().getSourceStart() >= e.getPosition().getSourceEnd()) { commentsToPrint.add(comment); @@ -1700,7 +1723,14 @@ public void visitCtNewArray(CtNewArray newArray) { for (int i = 0; ref instanceof CtArrayTypeReference; i++) { write("["); if (newArray.getDimensionExpressions().size() > i) { - scan(newArray.getDimensionExpressions().get(i)); + CtExpression e = newArray.getDimensionExpressions().get(i); + if (!(e instanceof CtStatement)) { + printComment(e, CommentOffset.BEFORE); + } + scan(e); + if (!(e instanceof CtStatement)) { + printComment(e, CommentOffset.AFTER); + } } write("]"); ref = ((CtArrayTypeReference) ref).getComponentType(); @@ -1709,8 +1739,14 @@ public void visitCtNewArray(CtNewArray newArray) { if (newArray.getDimensionExpressions().size() == 0) { write("{ "); for (CtExpression e : newArray.getElements()) { + if (!(e instanceof CtStatement)) { + printComment(e, CommentOffset.BEFORE); + } scan(e); write(" , "); + if (!(e instanceof CtStatement)) { + printComment(e, CommentOffset.AFTER); + } } if (newArray.getElements().size() > 0) { removeLastChar(); diff --git a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java index 237dd3c1a88..a63e91c7727 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java @@ -24,6 +24,7 @@ import spoon.reflect.code.CtBlock; import spoon.reflect.code.CtCase; import spoon.reflect.code.CtComment; +import spoon.reflect.code.CtConditional; import spoon.reflect.code.CtIf; import spoon.reflect.code.CtNewArray; import spoon.reflect.code.CtStatement; @@ -53,7 +54,7 @@ * The comment builder that will insert all element of a CompilationUnitDeclaration into the Spoon AST */ @SuppressWarnings("unchecked") -public class JDTCommentBuilder { +class JDTCommentBuilder { private static final Logger LOGGER = Logger.getLogger(JDTCommentBuilder.class); @@ -69,7 +70,7 @@ public class JDTCommentBuilder { * @param declarationUnit the declaration unit * @param factory the Spoon AST */ - public JDTCommentBuilder(CompilationUnitDeclaration declarationUnit, Factory factory) { + JDTCommentBuilder(CompilationUnitDeclaration declarationUnit, Factory factory) { this.declarationUnit = declarationUnit; if (declarationUnit.comments == null) { return; @@ -147,13 +148,12 @@ private CtElement addCommentToNear(final CtComment comment, final Collection void visitCtConstructor(CtConstructor e) { e.addComment(comment); } + @Override + public void visitCtConditional(CtConditional e) { + List elements = new ArrayList(); + elements.add(e.getElseExpression()); + elements.add(e.getThenExpression()); + elements.add(e.getCondition()); + addCommentToNear(comment, elements); + } + @Override public void visitCtBinaryOperator(CtBinaryOperator e) { List elements = new ArrayList(); diff --git a/src/test/java/spoon/test/comment/CommentTest.java b/src/test/java/spoon/test/comment/CommentTest.java index 5b0c41de255..03b009879a3 100644 --- a/src/test/java/spoon/test/comment/CommentTest.java +++ b/src/test/java/spoon/test/comment/CommentTest.java @@ -3,12 +3,14 @@ import org.junit.Test; import spoon.Launcher; import spoon.reflect.code.CtComment; +import spoon.reflect.code.CtConditional; import spoon.reflect.code.CtConstructorCall; import spoon.reflect.code.CtDo; import spoon.reflect.code.CtFor; import spoon.reflect.code.CtIf; import spoon.reflect.code.CtInvocation; import spoon.reflect.code.CtLocalVariable; +import spoon.reflect.code.CtNewArray; import spoon.reflect.code.CtReturn; import spoon.reflect.code.CtSwitch; import spoon.reflect.code.CtSynchronized; @@ -16,6 +18,7 @@ import spoon.reflect.declaration.CtAnonymousExecutable; import spoon.reflect.declaration.CtClass; import spoon.reflect.declaration.CtConstructor; +import spoon.reflect.declaration.CtElement; import spoon.reflect.declaration.CtField; import spoon.reflect.declaration.CtMethod; import spoon.reflect.declaration.CtParameter; @@ -27,6 +30,7 @@ import java.util.List; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; public class CommentTest { @@ -69,7 +73,7 @@ public void testInLineComment() { List comments = type.getElements(new TypeFilter(CtComment.class)); // verify that the number of comment present in the AST is correct - assertEquals(50, comments.size()); + assertEquals(57, comments.size()); // verify that all comments present in the AST is printed for (CtComment comment : comments) { @@ -77,6 +81,7 @@ public void testInLineComment() { // the header of the file is not printed with the toString continue; } + assertNotNull(comment.getParent()); assertTrue(comment.toString() + ":" + comment.getParent() + " is not printed", strType.contains(comment.toString())); } @@ -197,7 +202,25 @@ public void testInLineComment() { + " // comment in synchronized" + newLine + "}", ctSynchronized.toString()); - CtReturn ctReturn = m1.getBody().getStatement(10); + CtLocalVariable ctLocalVariable1 = m1.getBody().getStatement(10); + CtConditional ctConditional = (CtConditional) ctLocalVariable1.getDefaultExpression(); + assertEquals(createFakeComment(f, "comment after condition CtConditional"), ctConditional.getCondition().getComments().get(0)); + assertEquals(createFakeComment(f, "comment before then CtConditional"), ctConditional.getThenExpression().getComments().get(0)); + assertEquals(createFakeComment(f, "comment after then CtConditional"), ctConditional.getThenExpression().getComments().get(1)); + assertEquals(createFakeComment(f, "comment before else CtConditional"), ctConditional.getElseExpression().getComments().get(0)); + assertEquals(createFakeComment(f, "comment after else CtConditional"), ctLocalVariable1.getComments().get(0)); + assertEquals("java.lang.Double dou = i == 1// comment after condition CtConditional" + newLine + + " ? // comment before then CtConditional" + newLine + + "null// comment after then CtConditional" + newLine + + " : // comment before else CtConditional" + newLine + + "new java.lang.Double((j / ((double)((i - 1)))))", ctLocalVariable1.toString()); + + CtNewArray ctNewArray = (CtNewArray) ((CtLocalVariable) m1.getBody().getStatement(11)).getDefaultExpression(); + CtElement arrayValue = (CtElement) ctNewArray.getElements().get(0); + assertEquals(createFakeComment(f, "comment before array value"), arrayValue.getComments().get(0)); + assertEquals(createFakeComment(f, "comment after array value"), arrayValue.getComments().get(1)); + + CtReturn ctReturn = m1.getBody().getStatement(12); assertEquals(createFakeComment(f, "comment return"), ctReturn.getComments().get(0)); assertEquals("// comment return" + newLine + "return ", ctReturn.toString()); @@ -239,6 +262,7 @@ public void testBlockComment() { // the header of the file is not printed with the toString continue; } + assertNotNull(comment.getParent()); assertTrue(comment.toString() + ":" + comment.getParent() + " is not printed", strType.contains(comment.toString())); } diff --git a/src/test/java/spoon/test/comment/testclasses/InlineComment.java b/src/test/java/spoon/test/comment/testclasses/InlineComment.java index ef0e9fca029..0c0b3c1eef8 100644 --- a/src/test/java/spoon/test/comment/testclasses/InlineComment.java +++ b/src/test/java/spoon/test/comment/testclasses/InlineComment.java @@ -75,6 +75,20 @@ public void m1() { // comment in synchronized } + Double dou = + (i == 1) // comment after condition CtConditional + ? + // comment before then CtConditional + null // comment after then CtConditional + : + // comment before else CtConditional + new Double(j / (double) (i - 1)); // comment after else CtConditional + int[] arr = new int[] { + // comment before array value + 1, // comment after array value + 2, + 3 + }; // comment return return; }