Skip to content

Commit

Permalink
Add Matchers.methodHasNoParameters to disambiguate `methodHasParame…
Browse files Browse the repository at this point in the history
…ters()`

I'll make methodHasParameters(M, M...) after a JB release.

PiperOrigin-RevId: 438833949
  • Loading branch information
graememorgan authored and Error Prone Team committed Apr 1, 2022
1 parent d8961c2 commit d175bb6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -144,14 +144,14 @@ private static boolean hasJUnitAttr(MethodSymbol methodSym) {
public static final Matcher<MethodTree> isJunit3TestCase =
allOf(
methodNameStartsWith("test"),
methodHasParameters(),
methodHasNoParameters(),
Matchers.<MethodTree>hasModifier(Modifier.PUBLIC),
methodReturns(VOID_TYPE));

/** Common matcher for possible JUnit setUp/tearDown methods. */
private static final Matcher<MethodTree> looksLikeJUnitSetUpOrTearDown =
allOf(
methodHasParameters(),
methodHasNoParameters(),
anyOf(
methodHasVisibility(MethodVisibility.Visibility.PUBLIC),
methodHasVisibility(MethodVisibility.Visibility.PROTECTED)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,11 @@ public static Matcher<MethodTree> methodHasParameters(Matcher<VariableTree>... v
return methodHasParameters(ImmutableList.copyOf(variableMatcher));
}

/** Matches an AST node that represents a method declaration with no parameters. */
public static Matcher<MethodTree> 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
Expand Down Expand Up @@ -1482,7 +1487,7 @@ public static Matcher<MethodTree> equalsMethodDeclaration() {
allOf(
methodIsNamed("toString"),
methodHasVisibility(Visibility.PUBLIC),
methodHasParameters(),
methodHasNoParameters(),
methodReturns(STRING_TYPE));

/** Matches {@link Object#toString} method declaration. */
Expand All @@ -1494,7 +1499,7 @@ public static Matcher<MethodTree> toStringMethodDeclaration() {
allOf(
methodIsNamed("hashCode"),
methodHasVisibility(Visibility.PUBLIC),
methodHasParameters(),
methodHasNoParameters(),
methodReturns(INT_TYPE));

/** Matches {@code hashCode} method declaration. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,7 +77,7 @@ public final class JUnit3TestNotRun extends BugChecker implements MethodTreeMatc
enclosingClass(isJUnit3TestClass),
not(isJunit3TestCase),
methodReturns(VOID_TYPE),
methodHasParameters());
methodHasNoParameters());

/**
* Matches iff:
Expand Down

0 comments on commit d175bb6

Please sign in to comment.