Skip to content

Commit 4833c76

Browse files
Incorrect startPosition of a SingleVariableDeclaration in the catch block (eclipse-jdt#2896)
* Flush all Javadoc comment so far when we exit the try block so that they don't make it to the Exception variable.
1 parent 7cd2e50 commit 4833c76

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5483,4 +5483,65 @@ interface I {
54835483
assertEquals(1, variableElement.getSourceRange().getLength());
54845484
}
54855485

5486+
public void testSVDStartPositionIssue_1() throws JavaModelException {
5487+
String contents = """
5488+
public class X {
5489+
public static void example() {
5490+
try {
5491+
System.out.println("try");
5492+
}
5493+
/** */
5494+
catch (RuntimeException e) {
5495+
System.out.println("catch");
5496+
}
5497+
}
5498+
}
5499+
""";
5500+
this.workingCopy = getWorkingCopy("/Converter22/src/xyz/X.java", true/*resolve*/);
5501+
CompilationUnit cu = (CompilationUnit) buildAST(contents, this.workingCopy);
5502+
TypeDeclaration typedeclaration = (TypeDeclaration) getASTNode(cu, 0);
5503+
MethodDeclaration methodDeclaration = (MethodDeclaration) typedeclaration.bodyDeclarations().get(0);
5504+
Block block = methodDeclaration.getBody();
5505+
List<ASTNode> statements = block.statements();
5506+
TryStatement tryStatement = (TryStatement) statements.get(0);
5507+
List<ASTNode> catchClauseList = tryStatement.catchClauses();
5508+
CatchClause catchClause = (CatchClause) catchClauseList.get(0);
5509+
SingleVariableDeclaration svd = catchClause.getException();
5510+
5511+
assertEquals("Not a Single Variable Declaration", ASTNode.SINGLE_VARIABLE_DECLARATION, svd.getNodeType());
5512+
assertEquals("Not a Simple Type", ASTNode.SIMPLE_TYPE, svd.getType().getNodeType());
5513+
assertEquals("Not a Simple Name", ASTNode.SIMPLE_NAME, svd.getName().getNodeType());
5514+
assertEquals("Single Variable Declaration length is not correct", svd.getLength(), contents.substring(contents.indexOf("RuntimeException e")).indexOf(')'));
5515+
assertEquals("Single Variable Declaration startPosition is not correct", svd.getStartPosition(), contents.indexOf("RuntimeException"));
5516+
}
5517+
5518+
public void testSVDStartPositionIssue_2() throws JavaModelException {
5519+
String contents = """
5520+
public class X {
5521+
public static void example() {
5522+
try {
5523+
System.out.println("try");
5524+
}
5525+
/** */
5526+
catch(/** abc*/ RuntimeException e) {
5527+
System.out.println("catch");
5528+
}
5529+
}
5530+
}
5531+
""";
5532+
this.workingCopy = getWorkingCopy("/Converter22/src/xyz/X.java", true/*resolve*/);
5533+
CompilationUnit cu = (CompilationUnit) buildAST(contents, this.workingCopy);
5534+
TypeDeclaration typedeclaration = (TypeDeclaration) getASTNode(cu, 0);
5535+
MethodDeclaration methodDeclaration = (MethodDeclaration) typedeclaration.bodyDeclarations().get(0);
5536+
Block block = methodDeclaration.getBody();
5537+
List<ASTNode> statements = block.statements();
5538+
TryStatement tryStatement = (TryStatement) statements.get(0);
5539+
List<ASTNode> catchClauseList = tryStatement.catchClauses();
5540+
CatchClause catchClause = (CatchClause) catchClauseList.get(0);
5541+
SingleVariableDeclaration svd = catchClause.getException();
5542+
5543+
assertEquals("Single Variable Declaration length is not correct", svd.getLength(), contents.substring(contents.indexOf("/** abc*/ RuntimeException e")).indexOf(')'));
5544+
assertEquals("Single Variable Declaration startPosition is not correct", svd.getStartPosition(), contents.indexOf("/** abc*/ RuntimeException"));
5545+
}
5546+
54865547
}

org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/CommentRecorderParser.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ public int flushCommentsDefinedPriorTo(int position) {
195195
return position;
196196
}
197197

198+
@Override
199+
protected void consumeExitTryBlock() {
200+
flushCommentsDefinedPriorTo(this.scanner.currentPosition);
201+
super.consumeExitTryBlock();
202+
}
203+
198204
protected int getCommentPtr() {
199205
int lastComment = this.scanner.commentPtr;
200206
if (lastComment == -1 && this.currentElement != null) {

0 commit comments

Comments
 (0)