Skip to content

Commit

Permalink
UnusedMethod: pioneer a list of exempting class annotations in orde…
Browse files Browse the repository at this point in the history
…r to exempt methods on `AnnotationsToApply` classes.

PiperOrigin-RevId: 564371793
  • Loading branch information
graememorgan authored and Error Prone Team committed Sep 15, 2023
1 parent 5c391b4 commit 65d26d8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static com.google.errorprone.util.ASTHelpers.canBeRemoved;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.getType;
import static com.google.errorprone.util.ASTHelpers.hasAnnotation;
import static com.google.errorprone.util.ASTHelpers.isGeneratedConstructor;
import static com.google.errorprone.util.ASTHelpers.isSubtype;
import static com.google.errorprone.util.ASTHelpers.scope;
Expand Down Expand Up @@ -150,6 +151,10 @@ public final class UnusedMethod extends BugChecker implements CompilationUnitTre
"org.junit.jupiter.api.Test",
"org.junit.jupiter.params.ParameterizedTest");

/** Class annotations which exempt methods within the annotated class from findings. */
private static final ImmutableSet<String> EXEMPTING_CLASS_ANNOTATIONS =
ImmutableSet.of("com.google.auto.factory.AutoFactory.AnnotationsToApply");

/** The set of types exempting a type that is extending or implementing them. */
private static final ImmutableSet<String> EXEMPTING_SUPER_TYPES = ImmutableSet.of();

Expand Down Expand Up @@ -188,7 +193,8 @@ class MethodFinder extends SuppressibleTreePathScanner<Void, Void> {

@Override
public Void visitClass(ClassTree tree, Void unused) {
if (exemptedBySuperType(getType(tree), state)) {
if (exemptedBySuperType(getType(tree), state)
|| EXEMPTING_CLASS_ANNOTATIONS.stream().anyMatch(a -> hasAnnotation(tree, a, state))) {
return null;
}
return super.visitClass(tree, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,19 @@ public void effectivelyPrivateMethodMadeVisible_bySubclassImplementingPublicInte
"}")
.doTest();
}

@Test
public void methodExemptedByClassAnnotation() {
helper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.ThreadSafe;",
"class Test {",
" @com.google.auto.factory.AutoFactory.AnnotationsToApply",
" private @interface AutoFactoryThreadSafe {",
" ThreadSafe threadSafe() default @ThreadSafe;",
" }",
"}")
.doTest();
}
}

0 comments on commit 65d26d8

Please sign in to comment.