Search in sources :

Example 61 with ExpressionTree

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

the class ProtoFieldPreconditionsCheckNotNull 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 62 with ExpressionTree

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

the class ProtoFieldPreconditionsCheckNotNull 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 63 with ExpressionTree

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

the class ProtoStringFieldReferenceEquality method matchBinary.

@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
    switch(tree.getKind()) {
        case EQUAL_TO:
        case NOT_EQUAL_TO:
            break;
        default:
            return NO_MATCH;
    }
    ExpressionTree lhs = tree.getLeftOperand();
    ExpressionTree rhs = tree.getRightOperand();
    if (match(lhs, rhs, state) || match(rhs, lhs, state)) {
        String result = String.format("%s.equals(%s)", state.getSourceForNode(lhs), state.getSourceForNode(rhs));
        if (tree.getKind() == Kind.NOT_EQUAL_TO) {
            result = "!" + result;
        }
        return describeMatch(tree, SuggestedFix.replace(tree, result));
    }
    return NO_MATCH;
}
Also used : ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 64 with ExpressionTree

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

the class ASTHelpersTest method expressionStatementMatches.

private Scanner expressionStatementMatches(final String expectedExpression, final Matcher<ExpressionTree> matcher) {
    return new TestScanner() {

        @Override
        public Void visitExpressionStatement(ExpressionStatementTree node, VisitorState state) {
            ExpressionTree expression = node.getExpression();
            if (expression.toString().equals(expectedExpression)) {
                assertMatch(node.getExpression(), state, matcher);
                setAssertionsComplete();
            }
            return super.visitExpressionStatement(node, state);
        }
    };
}
Also used : VisitorState(com.google.errorprone.VisitorState) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 65 with ExpressionTree

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

the class DescendantOfAbstractTest method memberSelectMatches.

protected Scanner memberSelectMatches(final boolean shouldMatch, final DescendantOf toMatch) {
    ScannerTest test = new ScannerTest() {

        private boolean matched = false;

        @Override
        public Void visitMethodInvocation(MethodInvocationTree node, VisitorState visitorState) {
            visitorState = visitorState.withPath(getCurrentPath());
            ExpressionTree methodSelect = node.getMethodSelect();
            if (toMatch.matches(methodSelect, visitorState)) {
                matched = true;
            }
            return super.visitMethodInvocation(node, visitorState);
        }

        @Override
        public void assertDone() {
            assertEquals(matched, shouldMatch);
        }
    };
    tests.add(test);
    return test;
}
Also used : VisitorState(com.google.errorprone.VisitorState) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

ExpressionTree (com.sun.source.tree.ExpressionTree)78 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)27 Tree (com.sun.source.tree.Tree)18 Type (com.sun.tools.javac.code.Type)18 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)16 Symbol (com.sun.tools.javac.code.Symbol)14 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)14 JCTree (com.sun.tools.javac.tree.JCTree)14 Fix (com.google.errorprone.fixes.Fix)13 VariableTree (com.sun.source.tree.VariableTree)13 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)13 IdentifierTree (com.sun.source.tree.IdentifierTree)12 MemberSelectTree (com.sun.source.tree.MemberSelectTree)12 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)11 VisitorState (com.google.errorprone.VisitorState)9 AssignmentTree (com.sun.source.tree.AssignmentTree)9 BinaryTree (com.sun.source.tree.BinaryTree)9 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)8 MethodTree (com.sun.source.tree.MethodTree)8 TreePath (com.sun.source.util.TreePath)8