Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle multiple arguments in thenThrow. #4915

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,43 +52,46 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
if (!THEN_THROW.matches(tree, state)) {
return NO_MATCH;
}
ExpressionTree exceptionTree = tree.getArguments().get(0);
var thrownType = getType(exceptionTree);
if (!isCheckedExceptionType(thrownType, state)) {
return NO_MATCH;
}
// Heuristic: if the type being thrown is Exception/Throwable, but doesn't come directly from a
// constructor, it might be a parameter, and we can't know that it's not always given sensible
// types.
if (!(exceptionTree instanceof NewClassTree)
&& (isSameType(thrownType, state.getSymtab().exceptionType, state)
|| isSameType(thrownType, state.getSymtab().throwableType, state))) {
return NO_MATCH;
}
for (var receiver = getReceiver(tree);
receiver instanceof MethodInvocationTree whenMit;
receiver = getReceiver(receiver)) {
if (WHEN.matches(receiver, state)
&& whenMit.getArguments().get(0) instanceof MethodInvocationTree mit
&& getType(mit.getMethodSelect()).getThrownTypes().stream()
.noneMatch(
throwableType -> state.getTypes().isAssignable(thrownType, throwableType))) {
var thrownTypes = getType(mit.getMethodSelect()).getThrownTypes();
return buildDescription(whenMit.getArguments().get(0))
.setMessage(
thrownTypes.isEmpty()
? format(
"%s is not throwable by this method; only unchecked exceptions can be"
+ " thrown.",
thrownType.tsym.getSimpleName())
: format(
"%s is not throwable by this method; possible exception types are %s, or"
+ " any unchecked exception.",
thrownType.tsym.getSimpleName(),
thrownTypes.stream()
.map(t -> t.tsym.getSimpleName().toString())
.collect(joining(", "))))
.build();
for (ExpressionTree exceptionTree : tree.getArguments()) {
var thrownType = getType(exceptionTree);
if (!isCheckedExceptionType(thrownType, state)) {
continue;
}
// Heuristic: if the type being thrown is Exception/Throwable, but doesn't come directly from
// a constructor, it might be a parameter, and we can't know that it's not always given
// sensible types.
if (!(exceptionTree instanceof NewClassTree)
&& (isSameType(thrownType, state.getSymtab().exceptionType, state)
|| isSameType(thrownType, state.getSymtab().throwableType, state))) {
continue;
}
for (var receiver = getReceiver(tree);
receiver instanceof MethodInvocationTree whenMit;
receiver = getReceiver(receiver)) {
if (WHEN.matches(receiver, state)
&& whenMit.getArguments().get(0) instanceof MethodInvocationTree mit
&& getType(mit.getMethodSelect()).getThrownTypes().stream()
.noneMatch(
throwableType -> state.getTypes().isAssignable(thrownType, throwableType))) {
var thrownTypes = getType(mit.getMethodSelect()).getThrownTypes();
state.reportMatch(
buildDescription(whenMit.getArguments().get(0))
.setMessage(
thrownTypes.isEmpty()
? format(
"%s is not throwable by this method; only unchecked exceptions can be"
+ " thrown.",
thrownType.tsym.getSimpleName())
: format(
"%s is not throwable by this method; possible exception types are %s,"
+ " or any unchecked exception.",
thrownType.tsym.getSimpleName(),
thrownTypes.stream()
.map(t -> t.tsym.getSimpleName().toString())
.collect(joining(", "))))
.build());
break;
}
}
}
return NO_MATCH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ void test(Test t) {
.doTest();
}

@Test
public void positive_multipleThrows() {
compilationHelper
.addSourceLines(
"Test.java",
"""
import static org.mockito.Mockito.when;

abstract class Test {
abstract Object foo();

void test(Test t) {
// BUG: Diagnostic contains: only unchecked
when(t.foo()).thenThrow(new IllegalStateException(), new Exception());
}
}
""")
.doTest();
}

@Test
public void positiveWithSpecificType() {
compilationHelper
Expand Down Expand Up @@ -108,6 +128,25 @@ void test(Test t) throws Exception {
.doTest();
}

@Test
public void nothingThrown_noFinding() {
compilationHelper
.addSourceLines(
"Test.java",
"""
import static org.mockito.Mockito.when;

abstract class Test {
abstract Object foo() throws Exception;

void test(Test t) throws Exception {
when(t.foo()).thenThrow();
}
}
""")
.doTest();
}

@Test
public void genericException() {
compilationHelper
Expand Down
Loading