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

Implement ProxyNonConstantType #1799

Merged
merged 2 commits into from
Jun 22, 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
@@ -0,0 +1,82 @@
/*
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.baseline.errorprone;

import com.google.auto.service.AutoService;
import com.google.common.reflect.Reflection;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
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 java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

@AutoService(BugChecker.class)
@BugPattern(
name = "ProxyNonConstantType",
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks",
linkType = LinkType.CUSTOM,
severity = SeverityLevel.SUGGESTION,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently a non-enforcing suggestion

summary = "Proxy instances should be created using constant types known at compile time to allow native-image "
+ "behavior to match hotspot. Methods which build proxies should take a "
+ "`Function<InvocationHandler, ? extends T>` instead of arbitrary class references. "
+ "The proxy annotation processor can make this process much easier: "
+ "https://github.com/palantir/proxy-processor\n"
+ "See https://www.graalvm.org/reference-manual/native-image/DynamicProxy/#automatic-detection")
public final class ProxyNonConstantType extends BugChecker implements BugChecker.MethodInvocationTreeMatcher {

private static final Matcher<ExpressionTree> NEW_PROXY_INSTANCE_MATCHER =
MethodMatchers.staticMethod().onClass(Proxy.class.getName()).named("newProxyInstance");

private static final Matcher<ExpressionTree> REFLECTION_NEW_PROXY = MethodMatchers.staticMethod()
.onClass(Reflection.class.getName())
.named("newProxy")
.withParameters(Class.class.getName(), InvocationHandler.class.getName());

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (REFLECTION_NEW_PROXY.matches(tree, state)) {
return describeMatch(tree);
}
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)) {
return describeMatch(interfaces);
}
}
}
}

return Description.NO_MATCH;
}

private static boolean isDirectClassAccess(ExpressionTree expressionTree) {
return expressionTree instanceof MemberSelectTree
&& ((MemberSelectTree) expressionTree).getIdentifier().contentEquals("class");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* (c) Copyright 2021 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.baseline.errorprone;

import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;

class ProxyNonConstantTypeTest {

@Test
void testGuavaReflectionNewProxy() {
helper().addSourceLines(
"Test.java",
"import com.google.common.reflect.Reflection;",
"class Test {",
" void f() {",
" // BUG: Diagnostic contains: proxy",
" Reflection.newProxy(Test.class, null);",
" }",
"}")
.doTest();
}

@Test
void testConstantInterfacesInline() {
helper().addSourceLines(
"Test.java",
"import java.lang.reflect.Proxy;",
"class Test {",
" void f() {",
" Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{Test.class}, null);",
" }",
"}")
.doTest();
}

@Test
void testConstantInterfacesDynamic() {
helper().addSourceLines(
"Test.java",
"import java.lang.reflect.Proxy;",
"class Test {",
" void f(Class<?> iface) {",
" // BUG: Diagnostic contains: proxy",
" Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{iface}, null);",
" }",
"}")
.doTest();
}

private CompilationTestHelper helper() {
return CompilationTestHelper.newInstance(ProxyNonConstantType.class, getClass());
}
}
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1799.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Implement ProxyNonConstantType
links:
- https://github.com/palantir/gradle-baseline/pull/1799