Extract and improve method

This commit is contained in:
Rick Ossendrijver
2023-01-13 12:58:23 +01:00
parent a2626ee8bf
commit fc6af3337d

View File

@@ -67,7 +67,6 @@ import tech.picnic.errorprone.bugpatterns.util.MoreJUnitMatchers;
tags = STYLE) tags = STYLE)
public final class JUnitFactoryMethodDeclaration extends BugChecker implements MethodTreeMatcher { public final class JUnitFactoryMethodDeclaration extends BugChecker implements MethodTreeMatcher {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final Matcher<MethodTree> HAS_UNMODIFIABLE_SIGNATURE = private static final Matcher<MethodTree> HAS_UNMODIFIABLE_SIGNATURE =
anyOf( anyOf(
annotations(AT_LEAST_ONE, isType("java.lang.Override")), annotations(AT_LEAST_ONE, isType("java.lang.Override")),
@@ -167,13 +166,7 @@ public final class JUnitFactoryMethodDeclaration extends BugChecker implements M
private ImmutableList<Description> getReturnStatementCommentFixes( private ImmutableList<Description> getReturnStatementCommentFixes(
MethodTree testMethod, MethodTree factoryMethod, VisitorState state) { MethodTree testMethod, MethodTree factoryMethod, VisitorState state) {
ImmutableList<String> parameterNames = String expectedComment = createCommentContainingParameters(testMethod);
testMethod.getParameters().stream()
.map(VariableTree::getName)
.map(Object::toString)
.collect(toImmutableList());
String expectedComment = parameterNames.stream().collect(joining(", ", "/* { ", " } */"));
List<? extends StatementTree> statements = factoryMethod.getBody().getStatements(); List<? extends StatementTree> statements = factoryMethod.getBody().getStatements();
@@ -201,6 +194,13 @@ public final class JUnitFactoryMethodDeclaration extends BugChecker implements M
.collect(toImmutableList()); .collect(toImmutableList());
} }
private static String createCommentContainingParameters(MethodTree testMethod) {
return testMethod.getParameters().stream()
.map(VariableTree::getName)
.map(Object::toString)
.collect(joining(", ", "/* { ", " } */"));
}
private static boolean hasExpectedComment( private static boolean hasExpectedComment(
MethodTree factoryMethod, MethodTree factoryMethod,
String expectedComment, String expectedComment,