diff --git a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java index 136ee74d858..b0a9f5d53a4 100644 --- a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java +++ b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java @@ -1188,7 +1188,7 @@ public void visitCtAnnotationMethod(CtAnnotationMethod annotationMethod) @SuppressWarnings("rawtypes") public void visitCtNewArray(CtNewArray newArray) { enterCtExpression(newArray); - + elementPrinterHelper.writeComment(newArray, CommentOffset.BEFORE); boolean isNotInAnnotation; try { isNotInAnnotation = (newArray.getParent(CtAnnotationType.class) == null) && (newArray.getParent(CtAnnotation.class) == null); @@ -1244,8 +1244,11 @@ public void visitCtNewArray(CtNewArray newArray) { elementPrinterHelper.writeComment(e, CommentOffset.AFTER); } } + + elementPrinterHelper.writeComment(newArray, CommentOffset.INSIDE); printer.write(" }"); } + elementPrinterHelper.writeComment(newArray, CommentOffset.AFTER); exitCtExpression(newArray); } diff --git a/src/main/java/spoon/reflect/visitor/printer/ElementPrinterHelper.java b/src/main/java/spoon/reflect/visitor/printer/ElementPrinterHelper.java index 7b08fac4a0f..0fe008d8cf8 100644 --- a/src/main/java/spoon/reflect/visitor/printer/ElementPrinterHelper.java +++ b/src/main/java/spoon/reflect/visitor/printer/ElementPrinterHelper.java @@ -336,7 +336,8 @@ public List getComments(CtElement element, CommentOffset offset) { } final int line = element.getPosition().getLine(); final int sourceEnd = element.getPosition().getSourceEnd(); - if (offset == CommentOffset.BEFORE && (comment.getPosition().getLine() < line || sourceEnd >= comment.getPosition().getSourceEnd())) { + final int sourceStart = element.getPosition().getSourceStart(); + if (offset == CommentOffset.BEFORE && (comment.getPosition().getLine() < line || (sourceStart <= comment.getPosition().getSourceStart() && sourceEnd >= comment.getPosition().getSourceEnd()))) { commentsToPrint.add(comment); } else if (offset == CommentOffset.AFTER && comment.getPosition().getSourceStart() > sourceEnd) { commentsToPrint.add(comment); diff --git a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java index d5473f14e52..1b643a1effa 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java @@ -142,6 +142,7 @@ private void buildComment(int[] positions) { private CtElement addCommentToNear(final CtComment comment, final Collection elements) { CtElement best = null; int smallDistance = Integer.MAX_VALUE; + for (CtElement element : elements) { if (element.getPosition() == null) { continue; @@ -157,7 +158,11 @@ private CtElement addCommentToNear(final CtComment comment, final Collection comments = type.getElements(new TypeFilter(CtComment.class)); // verify that the number of comment present in the AST is correct - assertEquals(59, comments.size()); + assertEquals(61, comments.size()); // verify that all comments present in the AST is printed for (CtComment comment : comments) { @@ -128,9 +128,10 @@ public void testInLineComment() { assertEquals(createFakeComment(f, "comment class"), type.getComments().get(1)); CtField field = type.getField("field"); - assertEquals(2, field.getComments().size()); + assertEquals(3, field.getComments().size()); assertEquals(createFakeComment(f, "Comment Field"), field.getComments().get(0)); assertEquals("// Comment Field" + newLine + + "// comment field 2" + newLine + "// comment in field" + newLine + "private int field = 10;", field.toString()); @@ -256,10 +257,13 @@ public void testInLineComment() { + "new java.lang.Double((j / ((double) (i - 1))))", ctLocalVariable1.toString()); CtNewArray ctNewArray = (CtNewArray) ((CtLocalVariable) m1.getBody().getStatement(11)).getDefaultExpression(); + assertEquals(createFakeComment(f, "last comment at the end of array"), ctNewArray.getComments().get(0)); + 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 @@ -634,6 +638,9 @@ public void testCommentsInComment1And2() { type = (CtClass) f.Type().get(Comment2.class); comments = type.getElements(new TypeFilter(CtComment.class)); - assertEquals(1, comments.size()); + assertEquals(2, comments.size()); + + CtComment commentD = comments.get(1); + assertEquals("D", commentD.getContent()); } } diff --git a/src/test/java/spoon/test/comment/testclasses/Comment2.java b/src/test/java/spoon/test/comment/testclasses/Comment2.java index af52dced3fe..2c61f316f70 100644 --- a/src/test/java/spoon/test/comment/testclasses/Comment2.java +++ b/src/test/java/spoon/test/comment/testclasses/Comment2.java @@ -5,4 +5,10 @@ public class Comment2 { // C @interface Code_2{} + public void code_3() + { + int[] myArray = { + 3, 5 // D + }; + } } diff --git a/src/test/java/spoon/test/comment/testclasses/InlineComment.java b/src/test/java/spoon/test/comment/testclasses/InlineComment.java index b196d7481d2..552e2e40b4c 100644 --- a/src/test/java/spoon/test/comment/testclasses/InlineComment.java +++ b/src/test/java/spoon/test/comment/testclasses/InlineComment.java @@ -7,6 +7,7 @@ // comment class public class InlineComment { // Comment Field + // comment field 2 private int field // comment in field = 10; @@ -91,6 +92,7 @@ public void m1() { 1, // comment after array value 2, 3 + // last comment at the end of array }; // comment return return;