diff --git a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java index 9f3546defa3..136ee74d858 100644 --- a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java +++ b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java @@ -457,6 +457,7 @@ public void visitCtCase(CtCase caseStatement) { @Override public void visitCtCatch(CtCatch catchBlock) { + elementPrinterHelper.writeComment(catchBlock, CommentOffset.BEFORE); printer.write(" catch ("); CtCatchVariable parameter = catchBlock.getParameter(); if (parameter.getMultiTypes().size() > 0) { diff --git a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java index 3037c93293a..d5473f14e52 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.CtBodyHolder; import spoon.reflect.code.CtCase; +import spoon.reflect.code.CtCatch; import spoon.reflect.code.CtComment; import spoon.reflect.code.CtConditional; import spoon.reflect.code.CtIf; @@ -187,6 +188,7 @@ private void insertCommentInAST(final CtComment comment) { // visitor that inserts the comment in the element CtInheritanceScanner insertionVisitor = new CtInheritanceScanner() { private boolean isScanned = false; + @Override public void scan(CtElement e) { if (e == null) { @@ -373,6 +375,14 @@ public void visitCtNewArray(CtNewArray e) { public void visitCtParameter(CtParameter e) { e.addComment(comment); } + + @Override + public void visitCtCatch(CtCatch e) { + if (comment.getPosition().getLine() <= e.getPosition().getLine()) { + e.addComment(comment); + return; + } + } }; insertionVisitor.scan(commentParent); try { diff --git a/src/test/java/spoon/test/comment/CommentTest.java b/src/test/java/spoon/test/comment/CommentTest.java index 5f39704ed97..6a779d096ca 100644 --- a/src/test/java/spoon/test/comment/CommentTest.java +++ b/src/test/java/spoon/test/comment/CommentTest.java @@ -29,8 +29,10 @@ import spoon.reflect.declaration.CtPackage; import spoon.reflect.declaration.CtParameter; import spoon.reflect.declaration.ModifierKind; +import spoon.reflect.declaration.ParentNotInitializedException; import spoon.reflect.factory.Factory; import spoon.reflect.factory.FactoryImpl; +import spoon.reflect.visitor.AstParentConsistencyChecker; import spoon.reflect.visitor.filter.AbstractFilter; import spoon.reflect.visitor.filter.TypeFilter; import spoon.support.DefaultCoreFactory; @@ -38,6 +40,8 @@ import spoon.support.StandardEnvironment; import spoon.support.compiler.jdt.JDTSnippetCompiler; import spoon.test.comment.testclasses.BlockComment; +import spoon.test.comment.testclasses.Comment1; +import spoon.test.comment.testclasses.Comment2; import spoon.test.comment.testclasses.InlineComment; import java.io.FileOutputStream; @@ -107,7 +111,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(57, comments.size()); + assertEquals(59, comments.size()); // verify that all comments present in the AST is printed for (CtComment comment : comments) { @@ -225,7 +229,9 @@ public void testInLineComment() { + "try {" + newLine + " // comment in try" + newLine + " i++;" + newLine - + "} catch (java.lang.Exception e) {" + newLine + + "}// between" + newLine + + "// try/catch" + newLine + + " catch (java.lang.Exception e) {" + newLine + " // comment in catch" + newLine + "}", ctTry.toString()); @@ -618,4 +624,16 @@ public boolean matches(CtElement element) { write(codeElementsDocumentationPage.toString(), new FileOutputStream("doc/code_elements.md")); } } + + @Test + public void testCommentsInComment1And2() { + Factory f = getSpoonFactory(); + CtClass type = (CtClass) f.Type().get(Comment1.class); + List comments = type.getElements(new TypeFilter(CtComment.class)); + assertEquals(4, comments.size()); + + type = (CtClass) f.Type().get(Comment2.class); + comments = type.getElements(new TypeFilter(CtComment.class)); + assertEquals(1, comments.size()); + } } diff --git a/src/test/java/spoon/test/comment/testclasses/Comment1.java b/src/test/java/spoon/test/comment/testclasses/Comment1.java new file mode 100644 index 00000000000..a782b800d6e --- /dev/null +++ b/src/test/java/spoon/test/comment/testclasses/Comment1.java @@ -0,0 +1,16 @@ +package spoon.test.comment.testclasses; + +// comment 1 +// comment 2 +public class Comment1 { + + public void code_1() + { + try { } + // A + // B + catch (Exception ex) + { } + } + +} diff --git a/src/test/java/spoon/test/comment/testclasses/Comment2.java b/src/test/java/spoon/test/comment/testclasses/Comment2.java new file mode 100644 index 00000000000..af52dced3fe --- /dev/null +++ b/src/test/java/spoon/test/comment/testclasses/Comment2.java @@ -0,0 +1,8 @@ +package spoon.test.comment.testclasses; + +public class Comment2 { + + // C + @interface Code_2{} + +} diff --git a/src/test/java/spoon/test/comment/testclasses/InlineComment.java b/src/test/java/spoon/test/comment/testclasses/InlineComment.java index 0c0b3c1eef8..b196d7481d2 100644 --- a/src/test/java/spoon/test/comment/testclasses/InlineComment.java +++ b/src/test/java/spoon/test/comment/testclasses/InlineComment.java @@ -67,7 +67,10 @@ public void m1() { try { // comment in try i++; - } catch (Exception e) { + } + // between + // try/catch + catch (Exception e) { // comment in catch } // comment synchronized