From 5b5dd6489c33d47b4b0c311d2141f31297fb0160 Mon Sep 17 00:00:00 2001 From: Gijs de Jong Date: Tue, 22 Nov 2022 13:49:31 +0100 Subject: [PATCH] Use JUnit utilities introduced in #335 --- .../bugpatterns/JUnitValueSource.java | 27 ++----------------- .../bugpatterns/util/MoreJUnitMatchers.java | 2 +- 2 files changed, 3 insertions(+), 26 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitValueSource.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitValueSource.java index efc7d08a551..2a8eb1848fb 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitValueSource.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitValueSource.java @@ -1,13 +1,10 @@ package tech.picnic.errorprone.bugpatterns; -import static com.google.common.collect.ImmutableSet.toImmutableSet; import static com.google.errorprone.BugPattern.LinkType.CUSTOM; import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION; import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.ALL; -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.argument; import static com.google.errorprone.matchers.Matchers.argumentCount; @@ -15,26 +12,24 @@ import static com.google.errorprone.matchers.Matchers.hasArguments; import static com.google.errorprone.matchers.Matchers.isPrimitiveOrBoxedPrimitiveType; import static com.google.errorprone.matchers.Matchers.isSameType; -import static com.google.errorprone.matchers.Matchers.isType; import static com.google.errorprone.matchers.Matchers.methodHasParameters; import static com.google.errorprone.matchers.Matchers.staticMethod; import static com.google.errorprone.matchers.Matchers.toType; import static java.util.function.Predicate.not; import static java.util.stream.Collectors.joining; import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL; +import static tech.picnic.errorprone.bugpatterns.util.MoreJUnitMatchers.HAS_METHOD_SOURCE; +import static tech.picnic.errorprone.bugpatterns.util.MoreJUnitMatchers.getMethodSourceFactoryNames; import com.google.auto.service.AutoService; -import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.errorprone.BugPattern; import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker; import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher; import com.google.errorprone.fixes.SuggestedFix; -import com.google.errorprone.matchers.AnnotationMatcherUtils; import com.google.errorprone.matchers.Description; 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.ClassTree; @@ -107,8 +102,6 @@ public final class JUnitValueSource extends BugChecker implements MethodTreeMatc "com.google.common.collect.ImmutableSet") .named("of"), hasArguments(ALL, SUPPORTED_VALUE_FACTORY_VALUES))); - private static final MultiMatcher HAS_METHOD_SOURCE = - annotations(AT_LEAST_ONE, isType("org.junit.jupiter.params.provider.MethodSource")); private static final Matcher IS_UNARY_METHOD_WITH_SUPPORTED_PARAMETER = methodHasParameters( anyOf( @@ -190,22 +183,6 @@ private static Optional findMethodSourceAnnotation( return HAS_METHOD_SOURCE.multiMatchResult(tree, state).matchingNodes().stream().findFirst(); } - private static ImmutableSet getMethodSourceFactoryNames( - AnnotationTree methodSourceAnnotation, MethodTree method) { - ExpressionTree value = AnnotationMatcherUtils.getArgument(methodSourceAnnotation, "value"); - - if (!(value instanceof NewArrayTree)) { - return ImmutableSet.of(ASTHelpers.constValue(value, String.class)); - } - - NewArrayTree arrayTree = (NewArrayTree) value; - return arrayTree.getInitializers().isEmpty() - ? ImmutableSet.of(method.getName().toString()) - : arrayTree.getInitializers().stream() - .map(name -> ASTHelpers.constValue(name, String.class)) - .collect(toImmutableSet()); - } - private static Optional tryConstructValueSourceFix( Type parameterType, AnnotationTree methodSourceAnnotation, 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 59aacd17910..e33da6437ea 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 @@ -58,7 +58,7 @@ private MoreJUnitMatchers() {} * @param methodSourceAnnotation The annotation from which to extract value factory method names. * @return One or more value factory names. */ - static ImmutableSet getMethodSourceFactoryNames( + public static ImmutableSet getMethodSourceFactoryNames( AnnotationTree methodSourceAnnotation, MethodTree method) { String methodName = method.getName().toString(); ExpressionTree value = AnnotationMatcherUtils.getArgument(methodSourceAnnotation, "value");