22
22
import static com .google .common .collect .Iterables .getLast ;
23
23
import static com .google .common .collect .Iterables .size ;
24
24
import static com .google .common .collect .Multimaps .asMap ;
25
- import static com .google .common .collect .Sets .union ;
26
25
import static com .google .errorprone .BugPattern .SeverityLevel .WARNING ;
27
26
import static com .google .errorprone .fixes .SuggestedFix .emptyFix ;
28
27
import static com .google .errorprone .fixes .SuggestedFixes .replaceIncludingComments ;
34
33
import static com .google .errorprone .util .ASTHelpers .hasAnnotation ;
35
34
import static com .google .errorprone .util .ASTHelpers .isGeneratedConstructor ;
36
35
import static com .google .errorprone .util .ASTHelpers .isSubtype ;
37
- import static com .google .errorprone .util .ASTHelpers .shouldKeep ;
38
36
import static com .google .errorprone .util .MoreAnnotations .asStrings ;
39
37
import static com .google .errorprone .util .MoreAnnotations .getAnnotationValue ;
40
38
import static java .lang .String .format ;
46
44
import com .google .common .collect .ImmutableListMultimap ;
47
45
import com .google .common .collect .ImmutableSet ;
48
46
import com .google .errorprone .BugPattern ;
49
- import com .google .errorprone .ErrorProneFlags ;
50
47
import com .google .errorprone .VisitorState ;
51
48
import com .google .errorprone .bugpatterns .BugChecker .CompilationUnitTreeMatcher ;
52
49
import com .google .errorprone .fixes .SuggestedFix ;
69
66
import com .sun .tools .javac .code .Symbol ;
70
67
import com .sun .tools .javac .code .Symbol .ClassSymbol ;
71
68
import com .sun .tools .javac .code .Symbol .MethodSymbol ;
72
- import com .sun .tools .javac .code .Symbol .TypeSymbol ;
73
69
import com .sun .tools .javac .code .Type ;
74
70
import com .sun .tools .javac .tree .JCTree .JCAnnotation ;
75
71
import com .sun .tools .javac .tree .JCTree .JCAssign ;
@@ -96,77 +92,6 @@ public final class UnusedMethod extends BugChecker implements CompilationUnitTre
96
92
private static final String JUNIT_PARAMS_VALUE = "value" ;
97
93
private static final String JUNIT_PARAMS_ANNOTATION_TYPE = "junitparams.Parameters" ;
98
94
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
-
170
95
/**
171
96
* Class annotations which exempt methods within the annotated class from findings.
172
97
*
@@ -185,15 +110,11 @@ public final class UnusedMethod extends BugChecker implements CompilationUnitTre
185
110
*/
186
111
private static final ImmutableSet <String > EXEMPTING_SUPER_TYPES = ImmutableSet .of ();
187
112
188
- private final ImmutableSet < String > exemptingMethodAnnotations ;
113
+ private final WellKnownKeep wellKnownKeep ;
189
114
190
115
@ 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 ;
197
118
}
198
119
199
120
@ Override
@@ -281,11 +202,7 @@ private boolean isMethodSymbolEligibleForChecking(
281
202
if (exemptedByName (tree .getName ())) {
282
203
return false ;
283
204
}
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 )) {
289
206
return false ;
290
207
}
291
208
MethodSymbol methodSymbol = getSymbol (tree );
@@ -506,25 +423,6 @@ public Void visitMethod(MethodTree tree, Void unused) {
506
423
return hasAnyNativeMethods .get ();
507
424
}
508
425
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
-
528
426
private static boolean exemptedByName (Name name ) {
529
427
return Ascii .toLowerCase (name .toString ()).startsWith (EXEMPT_PREFIX );
530
428
}
0 commit comments