37
37
38
38
import com .google .common .collect .ImmutableSet ;
39
39
import com .google .errorprone .BugPattern ;
40
- import com .google .errorprone .ErrorProneFlags ;
41
40
import com .google .errorprone .VisitorState ;
42
41
import com .google .errorprone .bugpatterns .BugChecker .ClassTreeMatcher ;
43
42
import com .google .errorprone .fixes .SuggestedFix ;
73
72
+ " is a helper method, reduce its visibility." ,
74
73
severity = ERROR )
75
74
public class JUnit4TestNotRun extends BugChecker implements ClassTreeMatcher {
76
- private final Matcher <MethodTree > possibleTestMethod ;
75
+ private static final Matcher <MethodTree > POSSIBLE_TEST_METHOD =
76
+ allOf (
77
+ hasModifier (PUBLIC ),
78
+ methodReturns (VOID_TYPE ),
79
+ anyOf (
80
+ (t , s ) -> hasParameterisationAnnotation (t .getModifiers ().getAnnotations ()),
81
+ (t , s ) -> t .getParameters ().stream ().allMatch (v -> isInjectable (v , s ))),
82
+ not (JUnitMatchers ::hasJUnitAnnotation ));
83
+ ;
77
84
78
85
private static final ImmutableSet <String > EXEMPTING_METHOD_ANNOTATIONS =
79
86
ImmutableSet .of ("com.pdsl.runners.PdslTest" , "com.pholser.junit.quickcheck.Property" );
80
87
81
- private final boolean improvedHeuristic ;
82
-
83
88
private static boolean hasParameterisationAnnotation (List <? extends AnnotationTree > annotations ) {
84
89
return annotations .stream ()
85
90
.anyMatch (
@@ -115,28 +120,7 @@ private static boolean isParameterAnnotation(AnnotationTree annotation, VisitorS
115
120
private static final Matcher <Tree > NOT_STATIC = not (hasModifier (STATIC ));
116
121
117
122
@ Inject
118
- JUnit4TestNotRun (ErrorProneFlags flags ) {
119
- this .improvedHeuristic = flags .getBoolean ("JUnit4TestNotRun:ImprovedHeuristic" ).orElse (true );
120
- this .possibleTestMethod =
121
- improvedHeuristic
122
- ? allOf (
123
- hasModifier (PUBLIC ),
124
- methodReturns (VOID_TYPE ),
125
- anyOf (
126
- (t , s ) -> hasParameterisationAnnotation (t .getModifiers ().getAnnotations ()),
127
- (t , s ) -> t .getParameters ().stream ().allMatch (v -> isInjectable (v , s ))),
128
- not (JUnitMatchers ::hasJUnitAnnotation ))
129
- : allOf (
130
- hasModifier (PUBLIC ),
131
- methodReturns (VOID_TYPE ),
132
- (t , s ) ->
133
- t .getParameters ().stream ()
134
- .allMatch (
135
- v ->
136
- v .getModifiers ().getAnnotations ().stream ()
137
- .anyMatch (a -> isParameterAnnotation (a , s ))),
138
- not (JUnitMatchers ::hasJUnitAnnotation ));
139
- }
123
+ JUnit4TestNotRun () {}
140
124
141
125
@ Override
142
126
public Description matchClass (ClassTree tree , VisitorState state ) {
@@ -148,7 +132,7 @@ public Description matchClass(ClassTree tree, VisitorState state) {
148
132
if (!(member instanceof MethodTree methodTree ) || isSuppressed (member , state )) {
149
133
continue ;
150
134
}
151
- if (possibleTestMethod .matches (methodTree , state ) && !isSuppressed (tree , state )) {
135
+ if (POSSIBLE_TEST_METHOD .matches (methodTree , state ) && !isSuppressed (tree , state )) {
152
136
suspiciousMethods .put (getSymbol (methodTree ), methodTree );
153
137
}
154
138
}
@@ -204,10 +188,9 @@ private Optional<Description> handleMethod(MethodTree methodTree, VisitorState s
204
188
return Optional .empty ();
205
189
}
206
190
207
- if (improvedHeuristic
208
- && (hasParameterisationAnnotation (methodTree .getModifiers ().getAnnotations ())
209
- || methodTree .getParameters ().stream ()
210
- .anyMatch (p -> hasParameterisationAnnotation (p .getModifiers ().getAnnotations ())))) {
191
+ if (hasParameterisationAnnotation (methodTree .getModifiers ().getAnnotations ())
192
+ || methodTree .getParameters ().stream ()
193
+ .anyMatch (p -> hasParameterisationAnnotation (p .getModifiers ().getAnnotations ()))) {
211
194
return Optional .of (describeFixes (methodTree , state ));
212
195
}
213
196
0 commit comments