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 ccfd3353..14b08a5c 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 @@ -29,10 +29,10 @@ public final class MoreASTHelpers { ClassTree clazz = state.findEnclosing(ClassTree.class); checkArgument(clazz != null, "Visited node is not enclosed by a class"); return clazz.getMembers().stream() - .filter(MethodTree.class::isInstance) - .map(MethodTree.class::cast) - .filter(method -> method.getName().contentEquals(methodName)) - .collect(toImmutableList()); + .filter(MethodTree.class::isInstance) + .map(MethodTree.class::cast) + .filter(method -> method.getName().contentEquals(methodName)) + .collect(toImmutableList()); } /** 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 c26bda47..be780a49 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 @@ -10,14 +10,19 @@ import static tech.picnic.errorprone.bugpatterns.util.MoreMatchers.hasMetaAnnota import com.google.common.base.Strings; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; import com.google.errorprone.matchers.AnnotationMatcherUtils; import com.google.errorprone.matchers.Matcher; import com.google.errorprone.matchers.MultiMatcher; import com.google.errorprone.util.ASTHelpers; import com.sun.source.tree.AnnotationTree; +import com.sun.source.tree.AssignmentTree; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.MethodTree; import com.sun.source.tree.NewArrayTree; +import com.sun.tools.javac.code.Type; +import java.util.Optional; +import javax.lang.model.type.TypeKind; import org.jspecify.annotations.Nullable; /** @@ -78,4 +83,24 @@ public final class MoreJUnitMatchers { return requireNonNullElse( Strings.emptyToNull(ASTHelpers.constValue(tree, String.class)), annotatedMethodName); } + + /** + * Extracts the name of the JUnit factory method from a {@link + * org.junit.jupiter.params.provider.MethodSource} annotation. + * + * @param methodSourceAnnotation The {@link org.junit.jupiter.params.provider.MethodSource} + * annotation to extract a method name from. + * @return The name of the factory methods referred to in the annotation if there is only one, or + * {@link Optional#empty()} if there is more than one. + */ + public static Optional extractSingleFactoryMethodName( + AnnotationTree methodSourceAnnotation) { + ExpressionTree attributeExpression = + ((AssignmentTree) Iterables.getOnlyElement(methodSourceAnnotation.getArguments())) + .getExpression(); + Type attributeType = ASTHelpers.getType(attributeExpression); + return attributeType.getKind() == TypeKind.ARRAY + ? Optional.empty() + : Optional.of(attributeType.stringValue()); + } }