Skip to content

Commit

Permalink
fix #1498: Fix InvocationHandlerDelegation false positive (#1499)
Browse files Browse the repository at this point in the history
fix #1498: Fix InvocationHandlerDelegation false positive
  • Loading branch information
carterkozak authored Sep 22, 2020
1 parent 8029f2a commit a141345
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
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

0 comments on commit a141345

Please sign in to comment.