diff --git a/check_api/src/main/java/com/google/errorprone/matchers/JUnitMatchers.java b/check_api/src/main/java/com/google/errorprone/matchers/JUnitMatchers.java index 220c0ecda84..80fc2a7b3e0 100644 --- a/check_api/src/main/java/com/google/errorprone/matchers/JUnitMatchers.java +++ b/check_api/src/main/java/com/google/errorprone/matchers/JUnitMatchers.java @@ -29,7 +29,7 @@ import static com.google.errorprone.matchers.Matchers.hasMethod; import static com.google.errorprone.matchers.Matchers.hasModifier; import static com.google.errorprone.matchers.Matchers.isSubtypeOf; -import static com.google.errorprone.matchers.Matchers.methodHasParameters; +import static com.google.errorprone.matchers.Matchers.methodHasNoParameters; import static com.google.errorprone.matchers.Matchers.methodHasVisibility; import static com.google.errorprone.matchers.Matchers.methodIsNamed; import static com.google.errorprone.matchers.Matchers.methodNameStartsWith; @@ -144,14 +144,14 @@ private static boolean hasJUnitAttr(MethodSymbol methodSym) { public static final Matcher isJunit3TestCase = allOf( methodNameStartsWith("test"), - methodHasParameters(), + methodHasNoParameters(), Matchers.hasModifier(Modifier.PUBLIC), methodReturns(VOID_TYPE)); /** Common matcher for possible JUnit setUp/tearDown methods. */ private static final Matcher looksLikeJUnitSetUpOrTearDown = allOf( - methodHasParameters(), + methodHasNoParameters(), anyOf( methodHasVisibility(MethodVisibility.Visibility.PUBLIC), methodHasVisibility(MethodVisibility.Visibility.PROTECTED)), diff --git a/check_api/src/main/java/com/google/errorprone/matchers/Matchers.java b/check_api/src/main/java/com/google/errorprone/matchers/Matchers.java index 184374d0e8d..98478e941de 100644 --- a/check_api/src/main/java/com/google/errorprone/matchers/Matchers.java +++ b/check_api/src/main/java/com/google/errorprone/matchers/Matchers.java @@ -887,6 +887,11 @@ public static Matcher methodHasParameters(Matcher... v return methodHasParameters(ImmutableList.copyOf(variableMatcher)); } + /** Matches an AST node that represents a method declaration with no parameters. */ + public static Matcher methodHasNoParameters() { + return methodHasParameters(ImmutableList.of()); + } + /** * Matches an AST node that represents a method declaration, based on the list of * variableMatchers. Applies the variableMatcher at index n to the parameter at index n and @@ -1482,7 +1487,7 @@ public static Matcher equalsMethodDeclaration() { allOf( methodIsNamed("toString"), methodHasVisibility(Visibility.PUBLIC), - methodHasParameters(), + methodHasNoParameters(), methodReturns(STRING_TYPE)); /** Matches {@link Object#toString} method declaration. */ @@ -1494,7 +1499,7 @@ public static Matcher toStringMethodDeclaration() { allOf( methodIsNamed("hashCode"), methodHasVisibility(Visibility.PUBLIC), - methodHasParameters(), + methodHasNoParameters(), methodReturns(INT_TYPE)); /** Matches {@code hashCode} method declaration. */ diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/JUnit3TestNotRun.java b/core/src/main/java/com/google/errorprone/bugpatterns/JUnit3TestNotRun.java index 7b40db42628..5322aadd310 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/JUnit3TestNotRun.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/JUnit3TestNotRun.java @@ -27,7 +27,7 @@ import static com.google.errorprone.matchers.JUnitMatchers.wouldRunInJUnit4; import static com.google.errorprone.matchers.Matchers.allOf; import static com.google.errorprone.matchers.Matchers.enclosingClass; -import static com.google.errorprone.matchers.Matchers.methodHasParameters; +import static com.google.errorprone.matchers.Matchers.methodHasNoParameters; import static com.google.errorprone.matchers.Matchers.methodReturns; import static com.google.errorprone.matchers.Matchers.not; import static com.google.errorprone.suppliers.Suppliers.VOID_TYPE; @@ -77,7 +77,7 @@ public final class JUnit3TestNotRun extends BugChecker implements MethodTreeMatc enclosingClass(isJUnit3TestClass), not(isJunit3TestCase), methodReturns(VOID_TYPE), - methodHasParameters()); + methodHasNoParameters()); /** * Matches iff: