Search in sources :

Example 6 with ExpressionStatementTree

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

the class AbstractTestExceptionChecker method buildFix.

// TODO(cushon): extracting one statement into a lambda may not compile if the statement has
// side effects (e.g. it references a variable in the method that isn't effectively final).
// If this is a problem, consider trying to detect and avoid that case.
protected static SuggestedFix buildFix(VisitorState state, SuggestedFix.Builder fix, JCExpression expectedException, List<? extends StatementTree> statements) {
    fix.addStaticImport("org.junit.Assert.assertThrows");
    StringBuilder prefix = new StringBuilder();
    prefix.append(String.format("assertThrows(%s, () -> ", state.getSourceForNode(expectedException)));
    if (statements.size() == 1 && getOnlyElement(statements) instanceof ExpressionStatementTree) {
        ExpressionTree expression = ((ExpressionStatementTree) getOnlyElement(statements)).getExpression();
        fix.prefixWith(expression, prefix.toString());
        fix.postfixWith(expression, ")");
    } else {
        prefix.append(" {");
        fix.prefixWith(statements.iterator().next(), prefix.toString());
        fix.postfixWith(getLast(statements), "});");
    }
    return fix.build();
}
Also used : ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 7 with ExpressionStatementTree

use of com.sun.source.tree.ExpressionStatementTree 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 8 with ExpressionStatementTree

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

the class FunctionalInterfaceMethodChanged method matchMethod.

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
    ClassTree enclosingClazz = ASTHelpers.findEnclosingNode(state.getPath(), ClassTree.class);
    if (tree.getModifiers().getFlags().contains(Modifier.DEFAULT) && IS_FUNCTIONAL_INTERFACE.matches(enclosingClazz, state)) {
        Types types = Types.instance(state.context);
        Set<Symbol> functionalSuperInterfaceSams = enclosingClazz.getImplementsClause().stream().filter(t -> IS_FUNCTIONAL_INTERFACE.matches(t, state)).map(ASTHelpers::getSymbol).map(TypeSymbol.class::cast).map(// TypeSymbol to single abstract method of the type
        types::findDescriptorSymbol).collect(toImmutableSet());
        // We designate an override of a superinterface SAM "behavior preserving" if it just
        // calls the SAM of this interface.
        Symbol thisInterfaceSam = types.findDescriptorSymbol(ASTHelpers.getSymbol(enclosingClazz));
        // relatively crude: doesn't verify that the same args are passed in the same order
        // so it can get false positives for behavior-preservingness (false negatives for the check)
        TreeVisitor<Boolean, VisitorState> behaviorPreserving = new SimpleTreeVisitor<Boolean, VisitorState>(false) {

            @Override
            public Boolean visitMethod(MethodTree node, VisitorState state) {
                return node.getBody() != null && node.getBody().accept(this, state);
            }

            @Override
            public Boolean visitBlock(BlockTree node, VisitorState state) {
                return node.getStatements().size() == 1 && Iterables.getOnlyElement(node.getStatements()).accept(this, state);
            }

            @Override
            public Boolean visitExpressionStatement(ExpressionStatementTree node, VisitorState state) {
                return node.getExpression().accept(this, state);
            }

            @Override
            public Boolean visitReturn(ReturnTree node, VisitorState state) {
                return node.getExpression().accept(this, state);
            }

            @Override
            public Boolean visitMethodInvocation(MethodInvocationTree node, VisitorState state) {
                return ASTHelpers.getSymbol(node) == thisInterfaceSam;
            }
        };
        if (!Collections.disjoint(ASTHelpers.findSuperMethods(ASTHelpers.getSymbol(tree), types), functionalSuperInterfaceSams) && !tree.accept(behaviorPreserving, state)) {
            return describeMatch(tree);
        }
    }
    return Description.NO_MATCH;
}
Also used : Types(com.sun.tools.javac.code.Types) MethodTree(com.sun.source.tree.MethodTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) SimpleTreeVisitor(com.sun.source.util.SimpleTreeVisitor) ClassTree(com.sun.source.tree.ClassTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ReturnTree(com.sun.source.tree.ReturnTree) VisitorState(com.google.errorprone.VisitorState) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) BlockTree(com.sun.source.tree.BlockTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 9 with ExpressionStatementTree

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

the class InfiniteRecursion method matchMethod.

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
    if (tree.getBody() == null || tree.getBody().getStatements().size() != 1) {
        return NO_MATCH;
    }
    Tree statement = TreeInfo.skipParens((JCTree) Iterables.getOnlyElement(tree.getBody().getStatements()));
    ExpressionTree expr = statement.accept(new SimpleTreeVisitor<ExpressionTree, Void>() {

        @Override
        public ExpressionTree visitExpressionStatement(ExpressionStatementTree tree, Void unused) {
            return tree.getExpression();
        }

        @Override
        public ExpressionTree visitReturn(ReturnTree tree, Void unused) {
            return tree.getExpression();
        }
    }, null);
    if (!(expr instanceof MethodInvocationTree)) {
        return NO_MATCH;
    }
    ExpressionTree select = ((MethodInvocationTree) expr).getMethodSelect();
    switch(select.getKind()) {
        case IDENTIFIER:
            break;
        case MEMBER_SELECT:
            ExpressionTree receiver = ((MemberSelectTree) select).getExpression();
            if (receiver.getKind() != Kind.IDENTIFIER) {
                return NO_MATCH;
            }
            if (!((IdentifierTree) receiver).getName().contentEquals("this")) {
                return NO_MATCH;
            }
            break;
        default:
            return NO_MATCH;
    }
    MethodSymbol sym = ASTHelpers.getSymbol(tree);
    if (sym == null || !sym.equals(ASTHelpers.getSymbol(expr))) {
        return NO_MATCH;
    }
    return describeMatch(statement);
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ReturnTree(com.sun.source.tree.ReturnTree) MethodTree(com.sun.source.tree.MethodTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) IdentifierTree(com.sun.source.tree.IdentifierTree) ReturnTree(com.sun.source.tree.ReturnTree)

Aggregations

ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)9 ExpressionTree (com.sun.source.tree.ExpressionTree)7 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)7 Tree (com.sun.source.tree.Tree)4 BlockTree (com.sun.source.tree.BlockTree)3 MethodTree (com.sun.source.tree.MethodTree)3 VisitorState (com.google.errorprone.VisitorState)2 ClassTree (com.sun.source.tree.ClassTree)2 IdentifierTree (com.sun.source.tree.IdentifierTree)2 ReturnTree (com.sun.source.tree.ReturnTree)2 StatementTree (com.sun.source.tree.StatementTree)2 TryTree (com.sun.source.tree.TryTree)2 VariableTree (com.sun.source.tree.VariableTree)2 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)2 JCTree (com.sun.tools.javac.tree.JCTree)2 Resources (com.google.common.io.Resources)1 Builder (com.google.errorprone.fixes.SuggestedFix.Builder)1 CodeWriter (com.sun.codemodel.CodeWriter)1 JClass (com.sun.codemodel.JClass)1 JClassAlreadyExistsException (com.sun.codemodel.JClassAlreadyExistsException)1