Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-picnic committed Nov 8, 2022
1 parent eb24648 commit a1c3556
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -18,34 +17,30 @@ 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<MethodTree> findMethods(ClassTree enclosingClass, String methodName) {
return enclosingClass.getMembers().stream()
public static ImmutableList<MethodTree> 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))
.collect(toImmutableList());
}

/**
* 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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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}.
*
* <p>These are additions to the ones from {@link com.google.errorprone.matchers.JUnitMatchers}.
*/
Expand All @@ -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<MethodTree> 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() {}

Expand Down

0 comments on commit a1c3556

Please sign in to comment.