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

Incorrect startPosition of a SingleVariableDeclaration in the catch block #2896

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 @@ -5483,4 +5483,65 @@ interface I {
assertEquals(1, variableElement.getSourceRange().getLength());
}

public void testSVDStartPositionIssue_1() throws JavaModelException {
String contents = """
public class X {
public static void example() {
try {
System.out.println("try");
}
/** */
catch (RuntimeException e) {
System.out.println("catch");
}
}
}
""";
this.workingCopy = getWorkingCopy("/Converter22/src/xyz/X.java", true/*resolve*/);
CompilationUnit cu = (CompilationUnit) buildAST(contents, this.workingCopy);
TypeDeclaration typedeclaration = (TypeDeclaration) getASTNode(cu, 0);
MethodDeclaration methodDeclaration = (MethodDeclaration) typedeclaration.bodyDeclarations().get(0);
Block block = methodDeclaration.getBody();
List<ASTNode> statements = block.statements();
TryStatement tryStatement = (TryStatement) statements.get(0);
List<ASTNode> catchClauseList = tryStatement.catchClauses();
CatchClause catchClause = (CatchClause) catchClauseList.get(0);
SingleVariableDeclaration svd = catchClause.getException();

assertEquals("Not a Single Variable Declaration", ASTNode.SINGLE_VARIABLE_DECLARATION, svd.getNodeType());
assertEquals("Not a Simple Type", ASTNode.SIMPLE_TYPE, svd.getType().getNodeType());
assertEquals("Not a Simple Name", ASTNode.SIMPLE_NAME, svd.getName().getNodeType());
assertEquals("Single Variable Declaration length is not correct", svd.getLength(), contents.substring(contents.indexOf("RuntimeException e")).indexOf(')'));
assertEquals("Single Variable Declaration startPosition is not correct", svd.getStartPosition(), contents.indexOf("RuntimeException"));
}

public void testSVDStartPositionIssue_2() throws JavaModelException {
String contents = """
public class X {
public static void example() {
try {
System.out.println("try");
}
/** */
catch(/** abc*/ RuntimeException e) {
System.out.println("catch");
}
}
}
""";
this.workingCopy = getWorkingCopy("/Converter22/src/xyz/X.java", true/*resolve*/);
CompilationUnit cu = (CompilationUnit) buildAST(contents, this.workingCopy);
TypeDeclaration typedeclaration = (TypeDeclaration) getASTNode(cu, 0);
MethodDeclaration methodDeclaration = (MethodDeclaration) typedeclaration.bodyDeclarations().get(0);
Block block = methodDeclaration.getBody();
List<ASTNode> statements = block.statements();
TryStatement tryStatement = (TryStatement) statements.get(0);
List<ASTNode> catchClauseList = tryStatement.catchClauses();
CatchClause catchClause = (CatchClause) catchClauseList.get(0);
SingleVariableDeclaration svd = catchClause.getException();

assertEquals("Single Variable Declaration length is not correct", svd.getLength(), contents.substring(contents.indexOf("/** abc*/ RuntimeException e")).indexOf(')'));
assertEquals("Single Variable Declaration startPosition is not correct", svd.getStartPosition(), contents.indexOf("/** abc*/ RuntimeException"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ public int flushCommentsDefinedPriorTo(int position) {
return position;
}

@Override
protected void consumeExitTryBlock() {
flushCommentsDefinedPriorTo(this.scanner.currentPosition);
subyssurendran666 marked this conversation as resolved.
Show resolved Hide resolved
super.consumeExitTryBlock();
}

protected int getCommentPtr() {
int lastComment = this.scanner.commentPtr;
if (lastComment == -1 && this.currentElement != null) {
Expand Down