-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
PreventUsingIncubatingMethods
error-prone check (conjure) (#1554)
* Add `PreventUsingIncubatingMethods` error-prone check An explicit check which warns against using incubating APIs; you can disable it on a per-project basis, or on a per-usage basis using @SuppressWarnings. * Rename check to 'IncubatingMethod' To be more in line with other checks. * Add conjure-lib test dependency, reusable matcher To ensure we get a compile break if @Incubating ever moves. * Fix entry in README, add changelog * Fix version lock & formatting
- Loading branch information
Michael "Tres" Brenan
authored
Nov 20, 2020
1 parent
b613a1f
commit 8ed48f5
Showing
7 changed files
with
191 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/IncubatingMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* (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.errorprone.BugPattern; | ||
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.Matchers; | ||
import com.sun.source.tree.MemberReferenceTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import com.sun.source.tree.Tree; | ||
|
||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "IncubatingMethod", | ||
providesFix = BugPattern.ProvidesFix.REQUIRES_HUMAN_ATTENTION, | ||
severity = BugPattern.SeverityLevel.ERROR, | ||
summary = "You should avoid using incubating methods where possible, since they have very weak stability" | ||
+ " guarantees. You can explicitly disable this check on a case-by-case basis using" | ||
+ " @SuppressWarnings(\"IncubatingMethod\").") | ||
public final class IncubatingMethod extends BugChecker | ||
implements BugChecker.MethodInvocationTreeMatcher, BugChecker.MemberReferenceTreeMatcher { | ||
|
||
/** Matcher for the Incubating annotation, using the full qualified path. */ | ||
private static final Matcher<Tree> INCUBATING_MATCHER = | ||
Matchers.hasAnnotation("com.palantir.conjure.java.lib.internal.Incubating"); | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
return checkTree(tree, state); | ||
} | ||
|
||
@Override | ||
public Description matchMemberReference(MemberReferenceTree tree, VisitorState state) { | ||
return checkTree(tree, state); | ||
} | ||
|
||
private Description checkTree(Tree tree, VisitorState state) { | ||
if (!INCUBATING_MATCHER.matches(tree, state)) { | ||
return Description.NO_MATCH; | ||
} | ||
|
||
// Allow users to test incubating endpoints in test code without complaining. | ||
if (TestCheckUtils.isTestCode(state)) { | ||
return Description.NO_MATCH; | ||
} | ||
|
||
return describeMatch(tree); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
...line-error-prone/src/test/java/com/palantir/baseline/errorprone/IncubatingMethodTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* (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.errorprone.CompilationTestHelper; | ||
import org.junit.Test; | ||
|
||
public class IncubatingMethodTest { | ||
|
||
// A simple service with one incubating method and one non-incubating method. | ||
private static final String[] SERVICE_DEFINITION = new String[] { | ||
"package com.palantir;", | ||
"import com.palantir.conjure.java.lib.internal.Incubating;", | ||
"public interface Service {", | ||
"@Incubating", | ||
"int test();", | ||
"int test2();", | ||
"}" | ||
}; | ||
|
||
@Test | ||
public void testIncubatingMethod() { | ||
CompilationTestHelper.newInstance(IncubatingMethod.class, getClass()) | ||
.addSourceLines("Service.java", SERVICE_DEFINITION) | ||
.addSourceLines( | ||
"Main.java", | ||
"package com.palantir;", | ||
"public final class Main {", | ||
"public static void main(String[] args) {", | ||
"Service service = null;", | ||
"// BUG: Diagnostic contains: You should avoid using incubating methods", | ||
"service.test();", | ||
"}", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void testIncubatingMethodReference() { | ||
CompilationTestHelper.newInstance(IncubatingMethod.class, getClass()) | ||
.addSourceLines("Service.java", SERVICE_DEFINITION) | ||
.addSourceLines( | ||
"Main.java", | ||
"package com.palantir;", | ||
"import java.util.function.Supplier;", | ||
"public final class Main {", | ||
"public static void main(String[] args) {", | ||
"Service service = null;", | ||
"// BUG: Diagnostic contains: You should avoid using incubating methods", | ||
"Supplier<Integer> supp = service::test;", | ||
"}", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void testNonIncubatingMethod() { | ||
CompilationTestHelper.newInstance(IncubatingMethod.class, getClass()) | ||
.addSourceLines("Service.java", SERVICE_DEFINITION) | ||
.addSourceLines( | ||
"Main.java", | ||
"package com.palantir;", | ||
"import java.util.function.Supplier;", | ||
"public final class Main {", | ||
"public static void main(String[] args) {", | ||
"Service service = null;", | ||
"int result = service.test2();", | ||
"Supplier<Integer> supp = service::test2;", | ||
"}", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void testSuppressedIncubatingMethod() { | ||
CompilationTestHelper.newInstance(IncubatingMethod.class, getClass()) | ||
.addSourceLines("Service.java", SERVICE_DEFINITION) | ||
.addSourceLines( | ||
"Main.java", | ||
"package com.palantir;", | ||
"public final class Main {", | ||
"@SuppressWarnings(\"IncubatingMethod\")", | ||
"public static void main(String[] args) {", | ||
"Service service = null;", | ||
"service.test();", | ||
"}", | ||
"}") | ||
.doTest(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: improvement | ||
improvement: | ||
description: Add `IncubatingMethod` errorprone check, which prevents usage of conjure incubating APIs unless explicitly annotated. | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/1554 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters