Search in sources :

Example 1 with StatementTree

use of com.sun.source.tree.StatementTree in project j2objc by google.

the class TreeConverter method convertBlock.

private TreeNode convertBlock(JCTree.JCBlock node) {
    Block newNode = new Block();
    for (StatementTree stmt : node.getStatements()) {
        TreeNode tree = convert(stmt);
        if (tree instanceof AbstractTypeDeclaration) {
            tree = new TypeDeclarationStatement().setDeclaration((AbstractTypeDeclaration) tree);
        }
        newNode.addStatement((Statement) tree);
    }
    return newNode;
}
Also used : StatementTree(com.sun.source.tree.StatementTree) TreeNode(com.google.devtools.j2objc.ast.TreeNode) TypeDeclarationStatement(com.google.devtools.j2objc.ast.TypeDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 2 with StatementTree

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

the class TryFailThrowable method tryTreeMatches.

private static MatchResult tryTreeMatches(TryTree tryTree, VisitorState state) {
    BlockTree tryBlock = tryTree.getBlock();
    List<? extends StatementTree> statements = tryBlock.getStatements();
    if (statements.isEmpty()) {
        return doesNotMatch();
    }
    // Check if any of the statements is a fail or assert* method (i.e. any
    // method that can throw an AssertionFailedError)
    StatementTree failStatement = null;
    for (StatementTree statement : statements) {
        if (!(statement instanceof ExpressionStatementTree)) {
            continue;
        }
        if (failOrAssert.matches(((ExpressionStatementTree) statement).getExpression(), state)) {
            failStatement = statement;
            break;
        }
    }
    if (failStatement == null) {
        return doesNotMatch();
    }
    // Verify that the only catch clause catches Throwable
    List<? extends CatchTree> catches = tryTree.getCatches();
    if (catches.size() != 1) {
        // to be checked - it would either be Throwable or Error.
        return doesNotMatch();
    }
    CatchTree catchTree = catches.get(0);
    VariableTree catchType = catchTree.getParameter();
    boolean catchesThrowable = javaLangThrowable.matches(catchType, state);
    boolean catchesError = javaLangError.matches(catchType, state);
    boolean catchesOtherError = someAssertionFailure.matches(catchType, state);
    if (!catchesThrowable && !catchesError && !catchesOtherError) {
        return doesNotMatch();
    }
    // Verify that the catch block is empty or contains only comments.
    List<? extends StatementTree> catchStatements = catchTree.getBlock().getStatements();
    for (StatementTree catchStatement : catchStatements) {
        // or a list of empty statements.
        if (!Matchers.<Tree>kindIs(EMPTY_STATEMENT).matches(catchStatement, state)) {
            return doesNotMatch();
        }
    }
    return matches(failStatement, catchesThrowable ? JAVA_LANG_THROWABLE : catchesError ? JAVA_LANG_ERROR : SOME_ASSERTION_FAILURE);
}
Also used : ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) StatementTree(com.sun.source.tree.StatementTree) CatchTree(com.sun.source.tree.CatchTree) VariableTree(com.sun.source.tree.VariableTree) BlockTree(com.sun.source.tree.BlockTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) CatchTree(com.sun.source.tree.CatchTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) BlockTree(com.sun.source.tree.BlockTree) StatementTree(com.sun.source.tree.StatementTree) TryTree(com.sun.source.tree.TryTree)

Example 3 with StatementTree

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

the class FilesLinesLeak method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    if (!MATCHER.matches(tree, state)) {
        return NO_MATCH;
    }
    if (inTWR(state)) {
        return NO_MATCH;
    }
    Description.Builder description = buildDescription(tree);
    Tree parent = state.getPath().getParentPath().getLeaf();
    if (parent instanceof MemberSelectTree) {
        MemberSelectTree select = (MemberSelectTree) parent;
        StatementTree statement = state.findEnclosing(StatementTree.class);
        SuggestedFix.Builder fix = SuggestedFix.builder();
        if (statement instanceof VariableTree) {
            VariableTree var = (VariableTree) statement;
            int pos = ((JCTree) var).getStartPosition();
            int initPos = ((JCTree) var.getInitializer()).getStartPosition();
            int eqPos = pos + state.getSourceForNode(var).substring(0, initPos - pos).lastIndexOf('=');
            fix.replace(eqPos, initPos, String.format(";\ntry (Stream<String> stream = %s) {\n%s =", state.getSourceForNode(tree), var.getName().toString()));
        } else {
            fix.prefixWith(statement, String.format("try (Stream<String> stream = %s) {\n", state.getSourceForNode(tree)));
            fix.replace(select.getExpression(), "stream");
        }
        fix.replace(tree, "stream");
        fix.postfixWith(statement, "}");
        fix.addImport("java.util.stream.Stream");
        description.addFix(fix.build());
    }
    return description.build();
}
Also used : StatementTree(com.sun.source.tree.StatementTree) Description(com.google.errorprone.matchers.Description) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) MemberSelectTree(com.sun.source.tree.MemberSelectTree) VariableTree(com.sun.source.tree.VariableTree) ExpressionTree(com.sun.source.tree.ExpressionTree) VariableTree(com.sun.source.tree.VariableTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) StatementTree(com.sun.source.tree.StatementTree) Tree(com.sun.source.tree.Tree) JCTree(com.sun.tools.javac.tree.JCTree)

Example 4 with StatementTree

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

the class DivZero method matchDivZero.

private Description matchDivZero(Tree tree, ExpressionTree operand, VisitorState state) {
    if (!anyOf(kindIs(Kind.DIVIDE), kindIs(Kind.DIVIDE_ASSIGNMENT)).matches(tree, state)) {
        return Description.NO_MATCH;
    }
    if (!kindIs(Kind.INT_LITERAL).matches(operand, state)) {
        return Description.NO_MATCH;
    }
    LiteralTree rightOperand = (LiteralTree) operand;
    if (((Integer) rightOperand.getValue()) != 0) {
        return Description.NO_MATCH;
    }
    // Find and replace enclosing Statement.
    StatementTree enclosingStmt = ASTHelpers.findEnclosingNode(state.getPath(), StatementTree.class);
    return (enclosingStmt != null) ? describeMatch(tree, SuggestedFix.replace(enclosingStmt, "throw new ArithmeticException(\"/ by zero\");")) : describeMatch(tree);
}
Also used : StatementTree(com.sun.source.tree.StatementTree) LiteralTree(com.sun.source.tree.LiteralTree)

Example 5 with StatementTree

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

the class NextStatement method matches.

@Override
public boolean matches(T statement, VisitorState state) {
    List<? extends StatementTree> blockStatements = state.findEnclosing(BlockTree.class).getStatements();
    int statementIndex = blockStatements.indexOf(statement);
    if (statementIndex == -1) {
        // return a; isn't contained directly in a BlockTree :-|
        return false;
    }
    // find next statement
    statementIndex++;
    StatementTree nextStmt = null;
    if (statementIndex < blockStatements.size()) {
        nextStmt = blockStatements.get(statementIndex);
    }
    // TODO(glorioso): return false always instead of allowing the matcher to fail to match null?
    return matcher.matches(nextStmt, state);
}
Also used : StatementTree(com.sun.source.tree.StatementTree) BlockTree(com.sun.source.tree.BlockTree)

Aggregations

StatementTree (com.sun.source.tree.StatementTree)37 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)19 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)18 BlockTree (com.sun.source.tree.BlockTree)16 Tree (com.sun.source.tree.Tree)16 ExpressionTree (com.sun.source.tree.ExpressionTree)12 TreePath (com.sun.source.util.TreePath)9 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)8 MethodTree (com.sun.source.tree.MethodTree)8 MemberSelectTree (com.sun.source.tree.MemberSelectTree)7 TryTree (com.sun.source.tree.TryTree)7 VariableTree (com.sun.source.tree.VariableTree)7 CatchTree (com.sun.source.tree.CatchTree)6 JCTree (com.sun.tools.javac.tree.JCTree)6 ClassTree (com.sun.source.tree.ClassTree)5 IdentifierTree (com.sun.source.tree.IdentifierTree)5 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)5 VisitorState (com.google.errorprone.VisitorState)4 Description (com.google.errorprone.matchers.Description)4 EnhancedForLoopTree (com.sun.source.tree.EnhancedForLoopTree)4