3737
3838import com .google .common .collect .ImmutableSet ;
3939import com .google .errorprone .BugPattern ;
40- import com .google .errorprone .ErrorProneFlags ;
4140import com .google .errorprone .VisitorState ;
4241import com .google .errorprone .bugpatterns .BugChecker .ClassTreeMatcher ;
4342import com .google .errorprone .fixes .SuggestedFix ;
7372 + " is a helper method, reduce its visibility." ,
7473 severity = ERROR )
7574public 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+ ;
7784
7885 private static final ImmutableSet <String > EXEMPTING_METHOD_ANNOTATIONS =
7986 ImmutableSet .of ("com.pdsl.runners.PdslTest" , "com.pholser.junit.quickcheck.Property" );
8087
81- private final boolean improvedHeuristic ;
82-
8388 private static boolean hasParameterisationAnnotation (List <? extends AnnotationTree > annotations ) {
8489 return annotations .stream ()
8590 .anyMatch (
@@ -115,28 +120,7 @@ private static boolean isParameterAnnotation(AnnotationTree annotation, VisitorS
115120 private static final Matcher <Tree > NOT_STATIC = not (hasModifier (STATIC ));
116121
117122 @ 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 () {}
140124
141125 @ Override
142126 public Description matchClass (ClassTree tree , VisitorState state ) {
@@ -148,7 +132,7 @@ public Description matchClass(ClassTree tree, VisitorState state) {
148132 if (!(member instanceof MethodTree methodTree ) || isSuppressed (member , state )) {
149133 continue ;
150134 }
151- if (possibleTestMethod .matches (methodTree , state ) && !isSuppressed (tree , state )) {
135+ if (POSSIBLE_TEST_METHOD .matches (methodTree , state ) && !isSuppressed (tree , state )) {
152136 suspiciousMethods .put (getSymbol (methodTree ), methodTree );
153137 }
154138 }
@@ -204,10 +188,9 @@ private Optional<Description> handleMethod(MethodTree methodTree, VisitorState s
204188 return Optional .empty ();
205189 }
206190
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 ()))) {
211194 return Optional .of (describeFixes (methodTree , state ));
212195 }
213196
0 commit comments