Search in sources :

Example 6 with MethodInvocationTree

use of com.sun.source.tree.MethodInvocationTree in project error-prone by google.

the class DefaultCharset method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    if (state.isAndroidCompatible()) {
        return NO_MATCH;
    }
    if (STRING_GET_BYTES.matches(tree, state)) {
        Description.Builder description = buildDescription(tree);
        Tree parent = state.getPath().getParentPath().getLeaf();
        if (parent instanceof ExpressionTree && BYTESTRING_COPY_FROM.matches((ExpressionTree) parent, state)) {
            byteStringFixes(description, tree, (ExpressionTree) parent, state);
        } else {
            appendCharsets(description, tree, tree.getMethodSelect(), tree.getArguments(), state);
        }
        return description.build();
    }
    if (FILE_NEW_WRITER.matches(tree, state)) {
        Description.Builder description = buildDescription(tree);
        appendCharsets(description, tree, tree.getMethodSelect(), tree.getArguments(), state);
        return description.build();
    }
    return NO_MATCH;
}
Also used : Description(com.google.errorprone.matchers.Description) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NewClassTree(com.sun.source.tree.NewClassTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 7 with MethodInvocationTree

use of com.sun.source.tree.MethodInvocationTree in project error-prone by google.

the class IsInstanceOfClass method classify.

static Operand classify(JCTree tree, VisitorState state) {
    CharSequence source = state.getSourceForNode(tree);
    if (tree instanceof MethodInvocationTree) {
        // expr.getClass() -> "expr"
        MethodInvocationTree receiverInvocation = (MethodInvocationTree) tree;
        MethodSymbol sym = ASTHelpers.getSymbol(receiverInvocation);
        if (sym != null) {
            if (sym.getSimpleName().contentEquals("getClass") && sym.params().isEmpty()) {
                if (receiverInvocation.getMethodSelect() instanceof IdentifierTree) {
                    // unqualified `getClass()`
                    return Operand.create(Kind.EXPR, state.getSourceForNode(tree), source);
                }
                return Operand.create(Kind.GET_CLASS, state.getSourceForNode((JCTree) ASTHelpers.getReceiver(receiverInvocation)), source);
            }
        }
    } else if (tree instanceof MemberSelectTree) {
        // Foo.class -> "Foo"
        MemberSelectTree select = (MemberSelectTree) tree;
        if (select.getIdentifier().contentEquals("class")) {
            return Operand.create(Kind.LITERAL, state.getSourceForNode((JCTree) select.getExpression()), source);
        }
    }
    return Operand.create(Kind.EXPR, source, source);
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) IdentifierTree(com.sun.source.tree.IdentifierTree) JCTree(com.sun.tools.javac.tree.JCTree)

Example 8 with MethodInvocationTree

use of com.sun.source.tree.MethodInvocationTree in project error-prone by google.

the class ProtoFieldNullComparison method isGetListMethodInvocation.

private static boolean isGetListMethodInvocation(ExpressionTree tree, VisitorState state) {
    if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) {
        MethodInvocationTree method = (MethodInvocationTree) tree;
        if (!method.getArguments().isEmpty()) {
            return false;
        }
        if (!returnsListMatcher.matches(method, state)) {
            return false;
        }
        ExpressionTree expressionTree = method.getMethodSelect();
        if (expressionTree instanceof JCFieldAccess) {
            JCFieldAccess access = (JCFieldAccess) expressionTree;
            String methodName = access.sym.getQualifiedName().toString();
            return isFieldGetMethod(methodName);
        }
        return true;
    }
    return false;
}
Also used : JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 9 with MethodInvocationTree

use of com.sun.source.tree.MethodInvocationTree in project error-prone by google.

the class ProtoFieldNullComparison method isGetMethodInvocation.

private static boolean isGetMethodInvocation(ExpressionTree tree, VisitorState state) {
    if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) {
        MethodInvocationTree method = (MethodInvocationTree) tree;
        if (!method.getArguments().isEmpty()) {
            return false;
        }
        if (returnsListMatcher.matches(method, state)) {
            return false;
        }
        ExpressionTree expressionTree = method.getMethodSelect();
        if (expressionTree instanceof JCFieldAccess) {
            JCFieldAccess access = (JCFieldAccess) expressionTree;
            String methodName = access.sym.getQualifiedName().toString();
            return isFieldGetMethod(methodName);
        }
        return true;
    }
    return false;
}
Also used : JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 10 with MethodInvocationTree

use of com.sun.source.tree.MethodInvocationTree in project error-prone by google.

the class RandomModInteger method matchBinary.

@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
    if (tree.getKind() == Kind.REMAINDER && tree.getLeftOperand() instanceof MethodInvocationTree && RANDOM_NEXT_INT.matches(tree.getLeftOperand(), state)) {
        ExpressionTree randomExpr = ASTHelpers.getReceiver(tree.getLeftOperand());
        ExpressionTree modulus = tree.getRightOperand();
        return describeMatch(tree, SuggestedFix.replace(tree, String.format("%s.nextInt(%s)", state.getSourceForNode(randomExpr), state.getSourceForNode(modulus))));
    }
    return Description.NO_MATCH;
}
Also used : MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)35 ExpressionTree (com.sun.source.tree.ExpressionTree)24 Tree (com.sun.source.tree.Tree)13 IdentifierTree (com.sun.source.tree.IdentifierTree)10 MemberSelectTree (com.sun.source.tree.MemberSelectTree)9 VariableTree (com.sun.source.tree.VariableTree)9 JCTree (com.sun.tools.javac.tree.JCTree)9 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)8 MethodTree (com.sun.source.tree.MethodTree)8 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)8 VisitorState (com.google.errorprone.VisitorState)7 StatementTree (com.sun.source.tree.StatementTree)7 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)7 BinaryTree (com.sun.source.tree.BinaryTree)5 BlockTree (com.sun.source.tree.BlockTree)5 NewClassTree (com.sun.source.tree.NewClassTree)5 ReturnTree (com.sun.source.tree.ReturnTree)5 AssignmentTree (com.sun.source.tree.AssignmentTree)4 ClassTree (com.sun.source.tree.ClassTree)4 Description (com.google.errorprone.matchers.Description)3