Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Comments in catch #3025

Merged
merged 2 commits into from
Jun 19, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: Comments in cath
Egor18 committed Jun 18, 2019
commit 5b6c490af3bf2a0dec132dbcc6df445c1156ca49
Original file line number Diff line number Diff line change
@@ -416,6 +416,8 @@ public <T> void visitCtParameter(CtParameter<T> e) {
public void visitCtCatch(CtCatch e) {
if (comment.getPosition().getLine() <= e.getPosition().getLine()) {
e.addComment(comment);
} else {
e.getBody().addComment(comment);
}
}

20 changes: 18 additions & 2 deletions src/test/java/spoon/test/comment/CommentTest.java
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
import spoon.reflect.CtModel;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtCatch;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtConditional;
import spoon.reflect.code.CtConstructorCall;
@@ -1115,8 +1116,8 @@ public void testCommentGetRawContent() {
" * @version 1.0\r" +
" */", type.getComments().get(0).getRawContent());
}
@Test

@Test
public void testEmptyStatementComments() {
//contract: model building should not produce NPE, comments should exist
Launcher launcher = new Launcher();
@@ -1128,4 +1129,19 @@ public void testEmptyStatementComments() {
assertEquals("comment", conditions.get(0).getComments().get(0).getContent());
assertEquals("comment", conditions.get(1).getComments().get(0).getContent());
}

@Test
public void testCatchComments() {
//contract: comments in catch should be properly added to the AST
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/comment/testclasses/CatchComments.java");
launcher.getEnvironment().setCommentEnabled(true);
CtModel model = launcher.buildModel();

List<CtCatch> catches = model.getElements(new TypeFilter<>(CtCatch.class));
assertEquals(1, catches.get(0).getComments().size());
assertEquals("first comment", catches.get(0).getComments().get(0).getContent());
assertEquals(1, catches.get(0).getBody().getComments().size());
assertEquals("second comment", catches.get(0).getBody().getComments().get(0).getContent());
}
}
14 changes: 14 additions & 0 deletions src/test/java/spoon/test/comment/testclasses/CatchComments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package spoon.test.comment.testclasses;

public class CatchComments {
public static void exampleMethod() {
try {
Object o = new Object();
} catch (Exception e) // first comment
// second comment
{
// third comment
int x = 42;
}
}
}