diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java index 178fdf087d3..5b6a160cd0f 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java @@ -5483,4 +5483,36 @@ interface I { assertEquals(1, variableElement.getSourceRange().getLength()); } +public void testSVDStartPositionIssue() 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 statements = block.statements(); + TryStatement tryStatement = (TryStatement) statements.get(0); + List 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(), 18); + assertEquals("Single Variable Declaration startPosition is not correct", svd.getStartPosition(), 116); +} + } diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/CommentRecorderParser.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/CommentRecorderParser.java index 4ad300fbe1f..4731e95335e 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/CommentRecorderParser.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/CommentRecorderParser.java @@ -203,6 +203,11 @@ public int flushCommentsDefinedPriorTo(int position) { return position; } + @Override + protected void consumeExitTryBlock() { + flushCommentsDefinedPriorTo(this.scanner.currentPosition); + } + protected int getCommentPtr() { int lastComment = this.scanner.commentPtr; if (lastComment == -1 && this.currentElement != null) {