Search in sources :

Example 16 with SuggestedFix

use of com.google.errorprone.fixes.SuggestedFix in project mockito by mockito.

the class AbstractMockitoAnyForPrimitiveType method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    if (!matcher().matches(tree, state)) {
        return NO_MATCH;
    }
    MethodSymbol method = ASTHelpers.getSymbol(tree);
    Type matcherType = method.getReturnType();
    // It is expected that the call to anyX() is itself the argument to another call which is
    // the one being mocked, e.g. something like this:
    // when(mock.call(..., anyInt(), ...))...
    TreePath path = state.getPath();
    Tree parentTree = path.getParentPath().getLeaf();
    if (!(parentTree instanceof MethodInvocationTree)) {
        // TODO: Support casting.
        return NO_MATCH;
    }
    MethodInvocationTree parentCall = (MethodInvocationTree) parentTree;
    MethodSymbol parentMethod = ASTHelpers.getSymbol(parentCall);
    // Find the index of the argument in the parent call.
    int argumentIndex = -1;
    List<? extends ExpressionTree> parentArguments = parentCall.getArguments();
    for (int i = 0; i < parentArguments.size(); i++) {
        ExpressionTree argumentTree = parentArguments.get(i);
        if (argumentTree == tree) {
            argumentIndex = i;
            break;
        }
    }
    if (argumentIndex == -1) {
        throw new IllegalStateException("Cannot find argument " + state.getSourceForNode(tree) + " in argument list from " + state.getSourceForNode(parentTree));
    }
    Type parameterType = getParameterType(parentMethod, argumentIndex);
    TypeKind parameterTypeKind = parameterType.getKind();
    if (parameterTypeKind.isPrimitive() && parameterTypeKind != matcherType.getKind()) {
        String expectedTypeAsString = parameterType.toString();
        String replacementName = "any" + Character.toUpperCase(expectedTypeAsString.charAt(0)) + expectedTypeAsString.substring(1);
        String message = formatMessage(expectedTypeAsString, matcherType, replacementName);
        SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
        ExpressionTree methodSelect = tree.getMethodSelect();
        String replacement;
        if (methodSelect instanceof MemberSelectTree) {
            MemberSelectTree qualifier = (MemberSelectTree) methodSelect;
            replacement = state.getSourceForNode(qualifier.getExpression()) + "." + replacementName;
        } else {
            replacement = replacementName;
            String staticImport = method.owner + "." + replacementName;
            fixBuilder.addStaticImport(staticImport);
        }
        SuggestedFix fix = fixBuilder.replace(tree, replacement + "()").build();
        return buildDescription(tree).setMessage(message).addFix(fix).build();
    }
    return NO_MATCH;
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) TypeKind(javax.lang.model.type.TypeKind) ArrayType(com.sun.tools.javac.code.Type.ArrayType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) TreePath(com.sun.source.util.TreePath) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

SuggestedFix (com.google.errorprone.fixes.SuggestedFix)16 ExpressionTree (com.sun.source.tree.ExpressionTree)5 Tree (com.sun.source.tree.Tree)5 AnnotationTree (com.sun.source.tree.AnnotationTree)4 Symbol (com.sun.tools.javac.code.Symbol)4 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)4 Description (com.google.errorprone.matchers.Description)3 Type (com.sun.tools.javac.code.Type)3 JCTree (com.sun.tools.javac.tree.JCTree)3 ArrayList (java.util.ArrayList)3 Streams (com.google.common.collect.Streams)2 BugPattern (com.google.errorprone.BugPattern)2 JDK (com.google.errorprone.BugPattern.Category.JDK)2 VisitorState (com.google.errorprone.VisitorState)2 ClassTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher)2 Builder (com.google.errorprone.fixes.SuggestedFix.Builder)2 ASTHelpers (com.google.errorprone.util.ASTHelpers)2 ClassTree (com.sun.source.tree.ClassTree)2 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)2 MethodTree (com.sun.source.tree.MethodTree)2