mirror of
https://github.com/jlengrand/error-prone-support.git
synced 2026-03-10 08:11:25 +00:00
Create JUnit matchers as preparation for additional JUnit BugCheckers
This commit is contained in:
committed by
Rick Ossendrijver
parent
f079c53914
commit
ec79df92e7
@@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<String> 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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user