Skip to content

Commit

Permalink
Issue#475 Review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil-Gabaydullin committed Mar 15, 2023
1 parent 26a04b9 commit 63d351c
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.Matchers.staticMethod;
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.TEST_METHOD;
Expand All @@ -15,14 +16,16 @@
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.code.Symbol;
import java.util.List;
import org.jspecify.annotations.Nullable;
import org.mockito.Mockito;
import tech.picnic.errorprone.bugpatterns.util.SourceCode;

/**
* A {@link BugChecker} that flags multiple usages of {@link Mockito#verifyNoInteractions} in favor
Expand All @@ -42,14 +45,16 @@
public final class MockitoVerifyNoInteractionsUsage extends BugChecker
implements MethodTreeMatcher {
private static final long serialVersionUID = 1L;
private static final Matcher<ExpressionTree> VERIFY_NO_INTERACTIONS =
staticMethod().onClass("org.mockito.Mockito").named("verifyNoInteractions");

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
boolean isTestMethod = TEST_METHOD.matches(tree, state);
if (!isTestMethod) {
if (!TEST_METHOD.matches(tree, state)) {
return Description.NO_MATCH;
}
var verifyNoInteractionsInvocations = getVerifyNoInteractionsInvocations(tree);
ImmutableList<MethodInvocationTree> verifyNoInteractionsInvocations =
getVerifyNoInteractionsInvocations(tree, state);
if (verifyNoInteractionsInvocations.size() < 2) {
return Description.NO_MATCH;
}
Expand All @@ -72,21 +77,26 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
"");
}
});
fixBuilder.replace(lastInvocation, "verifyNoInteractions(" + combinedArgument + ")");

String callAsString = SourceCode.treeToString(lastInvocation, state);
fixBuilder.replace(
lastInvocation,
callAsString.startsWith("Mockito.")
? "Mockito.verifyNoInteractions(" + combinedArgument + ")"
: "verifyNoInteractions(" + combinedArgument + ")");

return describeMatch(tree, fixBuilder.build());
}

private static ImmutableList<MethodInvocationTree> getVerifyNoInteractionsInvocations(
MethodTree methodTree) {
MethodTree methodTree, VisitorState state) {
ImmutableList.Builder<MethodInvocationTree> invocationTreeBuilder = ImmutableList.builder();

new TreeScanner<@Nullable Void, @Nullable Void>() {
@Override
public @Nullable Void visitMethodInvocation(
MethodInvocationTree node, @Nullable Void unused) {
Symbol symbol = ASTHelpers.getSymbol(node);
if (symbol.getSimpleName().toString().equals("verifyNoInteractions")) {
if (VERIFY_NO_INTERACTIONS.matches(node, state)) {
invocationTreeBuilder.add(node);
}
return super.visitMethodInvocation(node, unused);
Expand Down
Loading

0 comments on commit 63d351c

Please sign in to comment.