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

fix #1498: Fix InvocationHandlerDelegation false positive #1499

Merged
merged 4 commits into from
Sep 22, 2020
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 @@ -26,6 +26,10 @@
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.Matchers;
import com.google.errorprone.matchers.method.MethodMatchers;
import com.google.errorprone.predicates.TypePredicate;
import com.google.errorprone.predicates.TypePredicates;
import com.google.errorprone.predicates.type.DescendantOf;
import com.google.errorprone.suppliers.Suppliers;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ConditionalExpressionTree;
import com.sun.source.tree.ExpressionTree;
Expand All @@ -37,6 +41,7 @@
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.TryTree;
import com.sun.tools.javac.code.Type;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -83,8 +88,22 @@ public final class InvocationHandlerDelegation extends BugChecker implements Bug
private static final Matcher<Tree> CONTAINS_UNWRAP_THROWABLE =
Matchers.contains(ExpressionTree.class, UNWRAP_THROWABLE);

private static final TypePredicate IS_ITE_SUBTYPE =
new DescendantOf(Suppliers.typeFromClass(InvocationTargetException.class));

private static final TypePredicate IS_ITE_UNION = (TypePredicate) (type, state) -> {
if (type.isUnion()) {
for (Type unionType : MoreASTHelpers.expandUnion(type)) {
if (IS_ITE_SUBTYPE.apply(unionType, state)) {
return true;
}
}
}
return false;
};

private static final Matcher<ExpressionTree> UNWRAP_ITE = MethodMatchers.instanceMethod()
.onDescendantOf(InvocationTargetException.class.getName())
.onClass(TypePredicates.anyOf(IS_ITE_SUBTYPE, IS_ITE_UNION))
// getTargetException is deprecated, but does work correctly.
.namedAnyOf("getCause", "getTargetException")
.withParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ void testCorrectInvocationHandler_doesntRethrow() {
.doTest();
}

@Test
void testInvocationHandler_catchUnion() {
helper().addSourceLines(
"Test.java",
"import java.lang.reflect.InvocationHandler;",
"import java.lang.reflect.Method;",
"import java.lang.reflect.InvocationTargetException;",
"import java.lang.reflect.UndeclaredThrowableException;",
"final class Test implements InvocationHandler {",
" @Override",
" public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {",
" try {",
" return method.invoke(this, args);",
" } catch (InvocationTargetException | UndeclaredThrowableException e) {",
" throw e.getCause();",
" }",
" }",
"}")
.doTest();
}

@Test
void testCorrectInvocationHandler_lambda() {
helper().addSourceLines(
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1499.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Fix InvocationHandlerDelegation false positive
links:
- https://github.com/palantir/gradle-baseline/pull/1499