Skip to content

Commit

Permalink
fix(comments): fix a bug concerning multiple comments before a catch …
Browse files Browse the repository at this point in the history
…block (#1074)
  • Loading branch information
surli authored and monperrus committed Jan 3, 2017
1 parent adf0123 commit ad4d458
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ public <E> void visitCtCase(CtCase<E> caseStatement) {

@Override
public void visitCtCatch(CtCatch catchBlock) {
elementPrinterHelper.writeComment(catchBlock, CommentOffset.BEFORE);
printer.write(" catch (");
CtCatchVariable<? extends Throwable> parameter = catchBlock.getParameter();
if (parameter.getMultiTypes().size() > 0) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -373,6 +375,14 @@ public <T> void visitCtNewArray(CtNewArray<T> e) {
public <T> void visitCtParameter(CtParameter<T> 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 {
Expand Down
22 changes: 20 additions & 2 deletions src/test/java/spoon/test/comment/CommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@
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;
import spoon.support.JavaOutputProcessor;
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;
Expand Down Expand Up @@ -107,7 +111,7 @@ public void testInLineComment() {

List<CtComment> comments = type.getElements(new TypeFilter<CtComment>(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) {
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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<CtComment> comments = type.getElements(new TypeFilter<CtComment>(CtComment.class));
assertEquals(4, comments.size());

type = (CtClass<?>) f.Type().get(Comment2.class);
comments = type.getElements(new TypeFilter<CtComment>(CtComment.class));
assertEquals(1, comments.size());
}
}
16 changes: 16 additions & 0 deletions src/test/java/spoon/test/comment/testclasses/Comment1.java
Original file line number Diff line number Diff line change
@@ -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)
{ }
}

}
8 changes: 8 additions & 0 deletions src/test/java/spoon/test/comment/testclasses/Comment2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package spoon.test.comment.testclasses;

public class Comment2 {

// C
@interface Code_2{}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ad4d458

Please sign in to comment.