diff options
author | Jeff Johnston | 2021-01-28 21:39:29 +0000 |
---|---|---|
committer | Jeff Johnston | 2021-02-11 19:01:33 +0000 |
commit | 44818033c46c99d90718ff5475803ef85e086173 (patch) | |
tree | ffaeffcb1abb8bb1661ac8e92e204185f6701670 | |
parent | 1256b8ec4c0ff791d33f292e6f171848e670db26 (diff) | |
download | eclipse.jdt.core-I20210211-1810.tar.gz eclipse.jdt.core-I20210211-1810.tar.xz eclipse.jdt.core-I20210211-1810.zip |
Bug 560055 - Empty @throws-tag is not visibleI20210211-1810
- fix AbstractCommentParser.parseThrows() to reset properly
after an invalid tag (e.g. missing throws name) to prevent
an exception later and allowing the empty tag to be added
- add new tests to JavadocTestForMethod and ASTRewritingJavadocTest
Change-Id: I111099c144529e6609a246b72ad89bee67a961d8
3 files changed, 157 insertions, 3 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/JavadocTestForMethod.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/JavadocTestForMethod.java index e4d3006ec3..99c0af5c46 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/JavadocTestForMethod.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/JavadocTestForMethod.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2014 IBM Corporation and others. + * Copyright (c) 2000, 2021 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -4941,4 +4941,42 @@ public class JavadocTestForMethod extends JavadocTest { "Javadoc: Invalid parameters declaration\n" + "----------\n"); } + + public void test164() { + this.runNegativeReferenceTest( + new String[] { + "X.java", + "package test;\n" + + "public class X {\n" + + " /**\n" + + " * Invalid param and throws tags\n" + + " * \n" + + " * @param\n" + + " * @throws\n" + + " */\n" + + " public void s_foo(int a) throws Exception {\n" + + " }\n" + + "}\n" }, + "----------\n" + + "1. ERROR in X.java (at line 6)\n" + + " * @param\n" + + " ^^^^^\n" + + "Javadoc: Missing parameter name\n" + + "----------\n" + + "2. ERROR in X.java (at line 7)\n" + + " * @throws\n" + + " ^^^^^^\n" + + "Javadoc: Missing class name\n" + + "----------\n" + + "3. ERROR in X.java (at line 9)\n" + + " public void s_foo(int a) throws Exception {\n" + + " ^\n" + + "Javadoc: Missing tag for parameter a\n" + + "----------\n" + + "4. ERROR in X.java (at line 9)\n" + + " public void s_foo(int a) throws Exception {\n" + + " ^^^^^^^^^\n" + + "Javadoc: Missing tag for declared exception Exception\n" + + "----------\n"); + } } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingJavadocTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingJavadocTest.java index 348e246ac1..5719c502da 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingJavadocTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingJavadocTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2015 IBM Corporation and others. + * Copyright (c) 2000, 2021 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -86,6 +86,116 @@ public class ASTRewritingJavadocTest extends ASTRewritingTest { assertEqualString(preview, buf.toString()); } + public void testEmptyParamName() throws Exception { + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=560055 + IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); + StringBuffer buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("public class E {\n"); + buf.append("\n"); + buf.append(" /**\n"); + buf.append(" * @param\n"); + buf.append(" */\n"); + buf.append(" public void gee(String name) {\n"); + buf.append(" }\n"); + buf.append("}\n"); + ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); + + CompilationUnit astRoot= createAST(cu); + ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); + AST ast= astRoot.getAST(); + + { // insert method at first position + TypeDeclaration type= findTypeDeclaration(astRoot, "E"); + MethodDeclaration methodDecl= findMethodDeclaration(type, "gee"); + + Javadoc javadoc= methodDecl.getJavadoc(); + List tags= javadoc.tags(); + assertTrue("Has one tag", tags.size() == 1); + + TagElement tagElement= (TagElement) tags.get(0); + List fragments= tagElement.fragments(); + assertTrue("Has fragments", fragments.isEmpty()); + + TagElement newTagElement = ast.newTagElement(); + newTagElement.setTagName(TagElement.TAG_PARAM); + + SimpleName newName= ast.newSimpleName("newName"); + newTagElement.fragments().add(newName); + + rewrite.replace(tagElement, newTagElement, null); + } + + String preview= evaluateRewrite(cu, rewrite); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("public class E {\n"); + buf.append("\n"); + buf.append(" /**\n"); + buf.append(" * @param newName\n"); + buf.append(" */\n"); + buf.append(" public void gee(String name) {\n"); + buf.append(" }\n"); + buf.append("}\n"); + assertEqualString(preview, buf.toString()); + } + + public void testEmptyThrows() throws Exception { + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=560055 + IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); + StringBuffer buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("public class E {\n"); + buf.append("\n"); + buf.append(" /**\n"); + buf.append(" * @throws\n"); + buf.append(" */\n"); + buf.append(" public void gee(String name) {\n"); + buf.append(" }\n"); + buf.append("}\n"); + ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); + + CompilationUnit astRoot= createAST(cu); + ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); + AST ast= astRoot.getAST(); + + { // insert method at first position + TypeDeclaration type= findTypeDeclaration(astRoot, "E"); + MethodDeclaration methodDecl= findMethodDeclaration(type, "gee"); + + Javadoc javadoc= methodDecl.getJavadoc(); + List tags= javadoc.tags(); + assertTrue("Has one tag", tags.size() == 1); + + TagElement tagElement= (TagElement) tags.get(0); + List fragments= tagElement.fragments(); + assertTrue("Has fragments", fragments.isEmpty()); + + TagElement newTagElement = ast.newTagElement(); + newTagElement.setTagName(TagElement.TAG_THROWS); + + SimpleName newName= ast.newSimpleName("Exception"); + newTagElement.fragments().add(newName); + + rewrite.replace(tagElement, newTagElement, null); + } + + String preview= evaluateRewrite(cu, rewrite); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("public class E {\n"); + buf.append("\n"); + buf.append(" /**\n"); + buf.append(" * @throws Exception\n"); + buf.append(" */\n"); + buf.append(" public void gee(String name) {\n"); + buf.append(" }\n"); + buf.append("}\n"); + assertEqualString(preview, buf.toString()); + } + public void testSeeTag1() throws Exception { IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf= new StringBuffer(); diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java index 937e392886..179236d111 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2020 IBM Corporation and others. + * Copyright (c) 2000, 2021 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -1443,6 +1443,7 @@ public abstract class AbstractCommentParser implements JavadocTagConstants { */ protected boolean parseThrows() { int start = this.scanner.currentPosition; + boolean isCompletionParser = (this.kind & COMPLETION_PARSER) != 0; try { Object typeRef = parseQualifiedName(true); if (this.abort) return false; // May be aborted by specialized parser @@ -1455,6 +1456,11 @@ public abstract class AbstractCommentParser implements JavadocTagConstants { } catch (InvalidInputException ex) { if (this.reportProblems) this.sourceParser.problemReporter().javadocInvalidThrowsClass(start, getTokenEndPosition()); } + if (!isCompletionParser) { + this.scanner.currentPosition = start; + this.index = start; + } + this.currentTokenType = -1; return false; } |