2222import static com .google .common .collect .Iterables .getLast ;
2323import static com .google .common .collect .Iterables .size ;
2424import static com .google .common .collect .Multimaps .asMap ;
25- import static com .google .common .collect .Sets .union ;
2625import static com .google .errorprone .BugPattern .SeverityLevel .WARNING ;
2726import static com .google .errorprone .fixes .SuggestedFix .emptyFix ;
2827import static com .google .errorprone .fixes .SuggestedFixes .replaceIncludingComments ;
3433import static com .google .errorprone .util .ASTHelpers .hasAnnotation ;
3534import static com .google .errorprone .util .ASTHelpers .isGeneratedConstructor ;
3635import static com .google .errorprone .util .ASTHelpers .isSubtype ;
37- import static com .google .errorprone .util .ASTHelpers .shouldKeep ;
3836import static com .google .errorprone .util .MoreAnnotations .asStrings ;
3937import static com .google .errorprone .util .MoreAnnotations .getAnnotationValue ;
4038import static java .lang .String .format ;
4644import com .google .common .collect .ImmutableListMultimap ;
4745import com .google .common .collect .ImmutableSet ;
4846import com .google .errorprone .BugPattern ;
49- import com .google .errorprone .ErrorProneFlags ;
5047import com .google .errorprone .VisitorState ;
5148import com .google .errorprone .bugpatterns .BugChecker .CompilationUnitTreeMatcher ;
5249import com .google .errorprone .fixes .SuggestedFix ;
6966import com .sun .tools .javac .code .Symbol ;
7067import com .sun .tools .javac .code .Symbol .ClassSymbol ;
7168import com .sun .tools .javac .code .Symbol .MethodSymbol ;
72- import com .sun .tools .javac .code .Symbol .TypeSymbol ;
7369import com .sun .tools .javac .code .Type ;
7470import com .sun .tools .javac .tree .JCTree .JCAnnotation ;
7571import com .sun .tools .javac .tree .JCTree .JCAssign ;
@@ -96,77 +92,6 @@ public final class UnusedMethod extends BugChecker implements CompilationUnitTre
9692 private static final String JUNIT_PARAMS_VALUE = "value" ;
9793 private static final String JUNIT_PARAMS_ANNOTATION_TYPE = "junitparams.Parameters" ;
9894
99- /**
100- * Annotations that exempt methods from being considered unused.
101- *
102- * <p>Try to avoid adding more annotations here. Annotating these annotations with {@code @Keep}
103- * has the same effect; this list is chiefly for third-party annotations which cannot be
104- * annotated.
105- */
106- private static final ImmutableSet <String > EXEMPTING_METHOD_ANNOTATIONS =
107- ImmutableSet .of (
108- "android.webkit.JavascriptInterface" ,
109- "com.fasterxml.jackson.annotation.JsonCreator" ,
110- "com.fasterxml.jackson.annotation.JsonProperty" ,
111- "com.fasterxml.jackson.annotation.JsonSetter" ,
112- "com.fasterxml.jackson.annotation.JsonValue" ,
113- "com.google.acai.AfterTest" ,
114- "com.google.acai.BeforeSuite" ,
115- "com.google.acai.BeforeTest" ,
116- "com.google.caliper.Benchmark" ,
117- "com.google.common.eventbus.Subscribe" ,
118- "com.google.inject.Provides" ,
119- "com.google.inject.Inject" ,
120- "com.google.inject.multibindings.ProvidesIntoMap" ,
121- "com.google.inject.multibindings.ProvidesIntoSet" ,
122- "com.google.inject.throwingproviders.CheckedProvides" ,
123- "com.tngtech.java.junit.dataprovider.DataProvider" ,
124- "jakarta.annotation.PreDestroy" ,
125- "jakarta.annotation.PostConstruct" ,
126- "jakarta.inject.Inject" ,
127- "jakarta.persistence.PostLoad" ,
128- "jakarta.persistence.PostPersist" ,
129- "jakarta.persistence.PostRemove" ,
130- "jakarta.persistence.PostUpdate" ,
131- "jakarta.persistence.PrePersist" ,
132- "jakarta.persistence.PreRemove" ,
133- "jakarta.persistence.PreUpdate" ,
134- "jakarta.validation.constraints.AssertFalse" ,
135- "jakarta.validation.constraints.AssertTrue" ,
136- "javax.annotation.PreDestroy" ,
137- "javax.annotation.PostConstruct" ,
138- "javax.inject.Inject" ,
139- "javax.persistence.PostLoad" ,
140- "javax.persistence.PostPersist" ,
141- "javax.persistence.PostRemove" ,
142- "javax.persistence.PostUpdate" ,
143- "javax.persistence.PrePersist" ,
144- "javax.persistence.PreRemove" ,
145- "javax.persistence.PreUpdate" ,
146- "javax.validation.constraints.AssertFalse" ,
147- "javax.validation.constraints.AssertTrue" ,
148- "net.bytebuddy.asm.Advice.OnMethodEnter" ,
149- "net.bytebuddy.asm.Advice.OnMethodExit" ,
150- "org.apache.beam.sdk.transforms.DoFn.FinishBundle" ,
151- "org.apache.beam.sdk.transforms.DoFn.ProcessElement" ,
152- "org.apache.beam.sdk.transforms.DoFn.StartBundle" ,
153- "org.aspectj.lang.annotation.Pointcut" ,
154- "org.aspectj.lang.annotation.After" ,
155- "org.aspectj.lang.annotation.Before" ,
156- "org.springframework.context.annotation.Bean" ,
157- "org.testng.annotations.AfterClass" ,
158- "org.testng.annotations.AfterMethod" ,
159- "org.testng.annotations.BeforeClass" ,
160- "org.testng.annotations.BeforeMethod" ,
161- "org.testng.annotations.DataProvider" ,
162- "org.junit.jupiter.api.BeforeAll" ,
163- "org.junit.jupiter.api.AfterAll" ,
164- "org.junit.jupiter.api.AfterEach" ,
165- "org.junit.jupiter.api.BeforeEach" ,
166- "org.junit.jupiter.api.RepeatedTest" ,
167- "org.junit.jupiter.api.Test" ,
168- "org.junit.jupiter.params.ParameterizedTest" );
169-
17095 /**
17196 * Class annotations which exempt methods within the annotated class from findings.
17297 *
@@ -185,15 +110,11 @@ public final class UnusedMethod extends BugChecker implements CompilationUnitTre
185110 */
186111 private static final ImmutableSet <String > EXEMPTING_SUPER_TYPES = ImmutableSet .of ();
187112
188- private final ImmutableSet < String > exemptingMethodAnnotations ;
113+ private final WellKnownKeep wellKnownKeep ;
189114
190115 @ Inject
191- UnusedMethod (ErrorProneFlags errorProneFlags ) {
192- this .exemptingMethodAnnotations =
193- union (
194- errorProneFlags .getSetOrEmpty ("UnusedMethod:ExemptingMethodAnnotations" ),
195- EXEMPTING_METHOD_ANNOTATIONS )
196- .immutableCopy ();
116+ UnusedMethod (WellKnownKeep wellKnownKeep ) {
117+ this .wellKnownKeep = wellKnownKeep ;
197118 }
198119
199120 @ Override
@@ -281,11 +202,7 @@ private boolean isMethodSymbolEligibleForChecking(
281202 if (exemptedByName (tree .getName ())) {
282203 return false ;
283204 }
284- // Assume the method is called if annotated with a called-reflectively annotation.
285- if (exemptedByAnnotation (tree .getModifiers ().getAnnotations ())) {
286- return false ;
287- }
288- if (shouldKeep (tree )) {
205+ if (wellKnownKeep .shouldKeep (tree )) {
289206 return false ;
290207 }
291208 MethodSymbol methodSymbol = getSymbol (tree );
@@ -506,25 +423,6 @@ public Void visitMethod(MethodTree tree, Void unused) {
506423 return hasAnyNativeMethods .get ();
507424 }
508425
509- /**
510- * Looks at the list of {@code annotations} and see if there is any annotation which exists {@code
511- * exemptingAnnotations}.
512- */
513- private boolean exemptedByAnnotation (List <? extends AnnotationTree > annotations ) {
514- for (AnnotationTree annotation : annotations ) {
515- Type annotationType = getType (annotation );
516- if (annotationType == null ) {
517- continue ;
518- }
519- TypeSymbol tsym = annotationType .tsym ;
520- String annotationName = tsym .getQualifiedName ().toString ();
521- if (exemptingMethodAnnotations .contains (annotationName )) {
522- return true ;
523- }
524- }
525- return false ;
526- }
527-
528426 private static boolean exemptedByName (Name name ) {
529427 return Ascii .toLowerCase (name .toString ()).startsWith (EXEMPT_PREFIX );
530428 }
0 commit comments