Skip to content

Commit

Permalink
InvalidThrows: don't crash if @throws references elements that aren't…
Browse files Browse the repository at this point in the history
… classes

PiperOrigin-RevId: 530175878
  • Loading branch information
cushon authored and Error Prone Team committed May 8, 2023
1 parent 87d0f20 commit 2000ac2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.sun.source.util.DocTreePathScanner;
import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.TypeTag;
import javax.lang.model.element.Element;

/**
Expand Down Expand Up @@ -84,7 +85,8 @@ public Void visitThrows(ThrowsTree throwsTree, Void unused) {
}

private boolean isCheckedException(Type type) {
return !state.getTypes().isAssignable(type, state.getSymtab().errorType)
return type.hasTag(TypeTag.CLASS)
&& !state.getTypes().isAssignable(type, state.getSymtab().errorType)
&& !state.getTypes().isAssignable(type, state.getSymtab().runtimeExceptionType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,20 @@ public void unrecognisedException_noComplaint() {
.expectUnchanged()
.doTest(TestMode.TEXT_MATCH);
}

@Test
public void throwsInvalidType() {
refactoring
.addInputLines(
"Test.java",
"interface Test {",
" /**",
" * @throws bar when failed",
" */",
" void foo();",
" void bar();",
"}")
.expectUnchanged()
.doTest(TestMode.TEXT_MATCH);
}
}

0 comments on commit 2000ac2

Please sign in to comment.