Skip to content

Commit

Permalink
Merge pull request apache#2974 from wal-jan/NETBEANS-1615
Browse files Browse the repository at this point in the history
[NETBEANS-1615] fixed @throws hint for type variable
  • Loading branch information
matthiasblaesing authored Jan 9, 2023
2 parents cd38f5a + 64b0555 commit e944fa4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeVariable;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import org.netbeans.api.java.source.CompilationInfo;
Expand Down Expand Up @@ -676,21 +677,23 @@ public Void visitThrows(ThrowsTree tree, List<ErrorDescription> errors) {
DocSourcePositions sp = (DocSourcePositions) javac.getTrees().getSourcePositions();
int start = (int) sp.getStartPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), tree);
int end = (int) sp.getEndPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), tree);
if (ex == null || (ex.asType().getKind() == TypeKind.DECLARED
&& types.isAssignable(ex.asType(), throwable))) {
boolean isType = ex != null && (ex.asType().getKind() == TypeKind.DECLARED || ex.asType().getKind() == TypeKind.TYPEVAR);
if (ex == null || (isType && types.isAssignable(ex.asType(), throwable))) {
switch (currentElement.getKind()) {
case CONSTRUCTOR:
case METHOD:
if (ex == null || !(types.isAssignable(ex.asType(), error)
|| types.isAssignable(ex.asType(), runtime))) {
ExecutableElement ee = (ExecutableElement) currentElement;
String fqn;
if (ex != null) {
fqn = ((TypeElement) ex).getQualifiedName().toString();
} else {
if (ex == null) {
ExpressionTree referenceClass = javac.getTreeUtilities().getReferenceClass(new DocTreePath(currentDocPath, exName));
if(referenceClass == null) break;
fqn = referenceClass.toString();
} else if (ex.asType().getKind() == TypeKind.TYPEVAR) {
fqn = ex.getSimpleName().toString();
} else {
fqn = ((TypeElement) ex).getQualifiedName().toString();
}
checkThrowsDeclared(tree, ex, fqn, ee.getThrownTypes(), dtph, start, end, errors);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,32 @@ public void testAddThrowsTagFix2() throws Exception {
+ "}\n");
}

public void testAddThrowsTagFix_NETBEANS_1615() throws Exception {
// issue NETBEANS-1615
HintTest.create()
.input(
"package test;\n"
+ "interface Zima {\n"
+ " /**\n"
+ " */\n"
+ " <X extends Exception> void leden() throws X;\n"
+ "}\n")
.preference(AVAILABILITY_KEY + true, true)
.preference(SCOPE_KEY, "private")
.run(JavadocHint.class)
.findWarning("4:46-4:47:warning:Missing @throws tag for X")
.applyFix("Add @throws X tag")
.assertCompilable()
.assertOutput(
"package test;\n"
+ "interface Zima {\n"
+ " /**\n"
+ " * @throws X\n"
+ " */\n"
+ " <X extends Exception> void leden() throws X;\n"
+ "}\n");
}

public void testAddThrowsTagFix_NestedClass_160414() throws Exception {
// issue 160414
HintTest.create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ public void testExceptionInheritanceAnalyzer() throws Exception {
.assertNotContainsWarnings("Unknown throwable: @throws java.nio.file.NoSuchFileException");
}

public void testExceptionTypeAnalyzer() throws Exception {
// issue NETBEANS-1615
HintTest.create()
.input(
"package test;\n"
+ "interface Zima {\n"
+ " /**\n"
+ " * @throws X\n"
+ " */\n"
+ " <X extends Exception> void leden() throws X;\n"
+ "}\n")
.preference(AVAILABILITY_KEY + true, true)
.preference(SCOPE_KEY, "private")
.run(JavadocHint.class)
.assertNotContainsWarnings("Missing @throws tag for X");
}

public void testInheritanceAnalyzer() throws Exception {
HintTest.create()
.input(
Expand Down

0 comments on commit e944fa4

Please sign in to comment.