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: OOB in JDTCommentBuilder #2840

Merged
merged 4 commits into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,7 @@ public void visitCtIf(CtIf ifElement) {
List<CtComment> comments = elementPrinterHelper.getComments(ifElement, CommentOffset.INSIDE);
if (thenStmt != null) {
SourcePosition thenPosition = thenStmt.getPosition();
if (!thenPosition.isValidPosition()) {
if (!thenPosition.isValidPosition() && thenStmt instanceof CtBlock) {
CtStatement thenExpression = ((CtBlock) thenStmt).getStatement(0);
thenPosition = thenExpression.getPosition();
}
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,16 @@ public void visitCtIf(CtIf e) {
}
CtStatement elseStatement = e.getElseStatement();
if (elseStatement != null && thenStatement != null) {
CtStatement thenExpression = ((CtBlock) thenStatement).getStatement(0);
CtStatement elseExpression = ((CtBlock) thenStatement).getStatement(0);
SourcePosition thenPosition = thenStatement.getPosition().isValidPosition() ? thenStatement.getPosition() : thenExpression.getPosition();
SourcePosition elsePosition = elseStatement.getPosition().isValidPosition() ? elseStatement.getPosition() : elseExpression.getPosition();
SourcePosition thenPosition = thenStatement.getPosition();
if (!thenPosition.isValidPosition() && thenStatement instanceof CtBlock) {
CtStatement thenExpression = ((CtBlock) thenStatement).getStatement(0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thenStatement isn't always CtBlock in this context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see, but it's exactly the way the original code was written (I did not really want to change it):

SourcePosition thenPosition = e.getThenStatement().getPosition().isValidPosition() == false ? ((CtBlock) e.getThenStatement()).getStatement(0).getPosition() : e.getThenStatement().getPosition();
SourcePosition elsePosition = e.getElseStatement().getPosition().isValidPosition() == false ? ((CtBlock) e.getElseStatement()).getStatement(0).getPosition() : e.getElseStatement().getPosition();

Would you like me to add an additional check like thenStatement instanceof CtBlock?

thenPosition = thenExpression.getPosition();
}
SourcePosition elsePosition = elseStatement.getPosition();
if (!elsePosition.isValidPosition() && elseStatement instanceof CtBlock) {
CtStatement elseExpression = ((CtBlock) elseStatement).getStatement(0);
elsePosition = elseExpression.getPosition();
}
if (comment.getPosition().getSourceStart() > thenPosition.getSourceEnd() && comment.getPosition().getSourceEnd() < elsePosition.getSourceStart()) {
elseStatement.addComment(comment);
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/spoon/test/condition/testclasses/Foo2.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ void bug2() {
else
System.out.println("invalid");
}

void bug3() {
if (false) {} // some comment
else if (false) {}
}
}