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

Suppress existing ProxyNonConstantType failures to ease rollout #1850

Merged
merged 2 commits into from
Jul 23, 2021
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 @@ -23,13 +23,15 @@
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.method.MethodMatchers;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.NewArrayTree;
import com.sun.source.tree.Tree;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

Expand Down Expand Up @@ -59,15 +61,15 @@ public final class ProxyNonConstantType extends BugChecker implements BugChecker
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (REFLECTION_NEW_PROXY.matches(tree, state)) {
return describeMatch(tree);
return describeMatchWithFix(tree, state);
}
if (NEW_PROXY_INSTANCE_MATCHER.matches(tree, state)) {
ExpressionTree interfaces = tree.getArguments().get(1);
if (interfaces instanceof NewArrayTree) {
NewArrayTree newArrayTree = (NewArrayTree) interfaces;
for (ExpressionTree element : newArrayTree.getInitializers()) {
if (!isDirectClassAccess(element) && !TestCheckUtils.isTestCode(state)) {
return describeMatch(interfaces);
return describeMatchWithFix(interfaces, state);
}
}
}
Expand All @@ -76,6 +78,12 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
return Description.NO_MATCH;
}

private Description describeMatchWithFix(Tree tree, VisitorState state) {
return buildDescription(tree)
.addFix(SuggestedFixes.addSuppressWarnings(state, canonicalName()))
.build();
}

private static boolean isDirectClassAccess(ExpressionTree expressionTree) {
return expressionTree instanceof MemberSelectTree
&& ((MemberSelectTree) expressionTree).getIdentifier().contentEquals("class");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ void testGuavaReflectionNewProxy() {
.doTest();
}

@Test
void testGuavaReflectionNewProxySuppression() {
fix().addInputLines(
"Test.java",
"import com.google.common.reflect.Reflection;",
"class Test {",
" void f() {",
" Reflection.newProxy(Test.class, null);",
" }",
"}")
.addOutputLines(
"Test.java",
"import com.google.common.reflect.Reflection;",
"class Test {",
" @SuppressWarnings(\"ProxyNonConstantType\")",
" void f() {",
" Reflection.newProxy(Test.class, null);",
" }",
"}")
.doTest();
}

@Test
void testConstantInterfacesInline() {
helper().addSourceLines(
Expand Down Expand Up @@ -62,6 +84,28 @@ void testConstantInterfacesDynamic() {
.doTest();
}

@Test
void testConstantInterfacesDynamicSuppression() {
fix().addInputLines(
"Test.java",
"import java.lang.reflect.Proxy;",
"class Test {",
" void f(Class<?> iface) {",
" Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{iface}, null);",
" }",
"}")
.addOutputLines(
"Test.java",
"import java.lang.reflect.Proxy;",
"class Test {",
" @SuppressWarnings(\"ProxyNonConstantType\")",
" void f(Class<?> iface) {",
" Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{iface}, null);",
" }",
"}")
.doTest();
}

@Test
void testIgnoresTestCode() {
helper().addSourceLines(
Expand All @@ -81,4 +125,8 @@ void testIgnoresTestCode() {
private CompilationTestHelper helper() {
return CompilationTestHelper.newInstance(ProxyNonConstantType.class, getClass());
}

private RefactoringValidator fix() {
return RefactoringValidator.of(ProxyNonConstantType.class, getClass());
}
}
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1850.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Suppress existing `ProxyNonConstantType` failures to ease rollout
links:
- https://github.com/palantir/gradle-baseline/pull/1850
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class BaselineErrorProneExtension {
"PreferSafeLoggableExceptions",
"PreferSafeLoggingPreconditions",
"PreferStaticLoggers",
"ProxyNonConstantType",
"PublicConstructorForAbstractClass",
"ReadReturnValueIgnored",
"RedundantMethodReference",
Expand Down