diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreASTHelpers.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreASTHelpers.java index 67a10da59c3..001f0eedfe4 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreASTHelpers.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreASTHelpers.java @@ -7,7 +7,6 @@ import com.sun.source.tree.ClassTree; import com.sun.source.tree.MethodTree; import com.sun.source.tree.Tree; -import javax.lang.model.element.Name; /** * A set of helper methods for working with the AST. @@ -18,14 +17,15 @@ public final class MoreASTHelpers { private MoreASTHelpers() {} /** - * Finds methods with the given name in the given class. + * Finds methods with the given name in the enclosing class. * - * @param enclosingClass The class to search in. * @param methodName The method name to search for. - * @return The {@link MethodTree}s of the methods with the given name in the given class. + * @param state A {@link VisitorState} describing the context in which the given {@link Tree} is + * to be found. + * @return The {@link MethodTree}s of the methods with the given name in the enclosing class. */ - public static ImmutableList findMethods(ClassTree enclosingClass, String methodName) { - return enclosingClass.getMembers().stream() + public static ImmutableList findMethods(String methodName, VisitorState state) { + return state.findEnclosing(ClassTree.class).getMembers().stream() .filter(MethodTree.class::isInstance) .map(MethodTree.class::cast) .filter(method -> method.getName().contentEquals(methodName)) @@ -33,19 +33,14 @@ public static ImmutableList findMethods(ClassTree enclosingClass, St } /** - * Determines if there are any methods with the given name in the given class. + * Determines if there are any methods with the given name in the enclosing class. * * @param methodName The method name to search for. * @param state A {@link VisitorState} describing the context in which the given {@link Tree} is - * found. - * @return Whether there are any methods with the given name in the given class. + * to be found. + * @return Whether there are any methods with the given name in the enclosing class. */ public static boolean isMethodInEnclosingClass(String methodName, VisitorState state) { - return state.findEnclosing(ClassTree.class).getMembers().stream() - .filter(MethodTree.class::isInstance) - .map(MethodTree.class::cast) - .map(MethodTree::getName) - .map(Name::toString) - .anyMatch(methodName::equals); + return !findMethods(methodName, state).isEmpty(); } } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreJUnitMatchers.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreJUnitMatchers.java index f158a6f4dd6..abe98b07d42 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreJUnitMatchers.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreJUnitMatchers.java @@ -1,7 +1,6 @@ package tech.picnic.errorprone.bugpatterns.util; import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.AT_LEAST_ONE; -import static com.google.errorprone.matchers.Matchers.allOf; import static com.google.errorprone.matchers.Matchers.annotations; import static com.google.errorprone.matchers.Matchers.anyOf; import static com.google.errorprone.matchers.Matchers.isType; @@ -20,7 +19,7 @@ import javax.lang.model.type.TypeKind; /** - * A set of JUnit Jupiter-specific helper methods and {@link Matcher Matchers}. + * A set of JUnit-specific helper methods and {@link Matcher Matchers}. * *

These are additions to the ones from {@link com.google.errorprone.matchers.JUnitMatchers}. */ @@ -47,7 +46,7 @@ public final class MoreJUnitMatchers { * Matches methods that have a {@link org.junit.jupiter.params.provider.MethodSource} annotation. */ public static final Matcher HAS_METHOD_SOURCE = - allOf(annotations(AT_LEAST_ONE, isType("org.junit.jupiter.params.provider.MethodSource"))); + annotations(AT_LEAST_ONE, isType("org.junit.jupiter.params.provider.MethodSource")); private MoreJUnitMatchers() {}