From a9fb8a82a135d84e9bf4cef10e2798ed7b149c59 Mon Sep 17 00:00:00 2001 From: Suby S Surendran Date: Tue, 3 Sep 2024 23:05:04 +0530 Subject: [PATCH 1/3] Corrected the starting position of the JavaDocComment --- .../core/tests/dom/ASTConverter18Test.java | 32 +++++++++++++++++++ .../core/util/CommentRecorderParser.java | 5 +++ 2 files changed, 37 insertions(+) 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 db43a7c6a1f..f6251810196 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 @@ -195,6 +195,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) { From 15b011231e9265a4a1f5d4a050d4286088106459 Mon Sep 17 00:00:00 2001 From: Suby S Surendran Date: Wed, 25 Sep 2024 12:15:50 +0530 Subject: [PATCH 2/3] in-corporated the code review changes --- .../org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java | 4 ++-- .../eclipse/jdt/internal/core/util/CommentRecorderParser.java | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) 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 5b6a160cd0f..4d78eb0849f 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 @@ -5511,8 +5511,8 @@ public static void example() { 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); + 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")); } } 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 f6251810196..eae734262ab 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 @@ -198,6 +198,7 @@ public int flushCommentsDefinedPriorTo(int position) { @Override protected void consumeExitTryBlock() { flushCommentsDefinedPriorTo(this.scanner.currentPosition); + super.consumeExitTryBlock(); } protected int getCommentPtr() { From 8d66756a35c23bfd43d99fbc965a0f91c174f00e Mon Sep 17 00:00:00 2001 From: Suby S Surendran Date: Thu, 26 Sep 2024 11:59:00 +0530 Subject: [PATCH 3/3] incorporated the code review changes and added a new test case --- .../core/tests/dom/ASTConverter18Test.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) 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 4d78eb0849f..4d218aa2646 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,7 +5483,7 @@ interface I { assertEquals(1, variableElement.getSourceRange().getLength()); } -public void testSVDStartPositionIssue() throws JavaModelException { +public void testSVDStartPositionIssue_1() throws JavaModelException { String contents = """ public class X { public static void example() { @@ -5515,4 +5515,33 @@ public static void example() { 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 statements = block.statements(); + TryStatement tryStatement = (TryStatement) statements.get(0); + List 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")); +} + }