Search in sources :

Example 1 with VariableTree

use of com.sun.source.tree.VariableTree in project bazel by bazelbuild.

the class TreeUtils method getAssignmentContext.

/**
     * Returns the tree with the assignment context for the treePath
     * leaf node.
     *
     * The assignment context for the treepath is the most enclosing
     * tree of type:
     * <ul>
     *   <li>AssignmentTree </li>
     *   <li>CompoundAssignmentTree </li>
     *   <li>MethodInvocationTree</li>
     *   <li>NewArrayTree</li>
     *   <li>NewClassTree</li>
     *   <li>ReturnTree</li>
     *   <li>VariableTree</li>
     * </ul>
     *
     * @param treePath
     * @return  the assignment context as described.
     */
public static Tree getAssignmentContext(final TreePath treePath) {
    TreePath path = treePath.getParentPath();
    if (path == null)
        return null;
    Tree node = path.getLeaf();
    if ((node instanceof AssignmentTree) || (node instanceof CompoundAssignmentTree) || (node instanceof MethodInvocationTree) || (node instanceof NewArrayTree) || (node instanceof NewClassTree) || (node instanceof ReturnTree) || (node instanceof VariableTree))
        return node;
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) NewArrayTree(com.sun.source.tree.NewArrayTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) VariableTree(com.sun.source.tree.VariableTree) ReturnTree(com.sun.source.tree.ReturnTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) 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) BlockTree(com.sun.source.tree.BlockTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) StatementTree(com.sun.source.tree.StatementTree) NewClassTree(com.sun.source.tree.NewClassTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) ReturnTree(com.sun.source.tree.ReturnTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree)

Example 2 with VariableTree

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

the class FieldMissingNullable method findDeclaration.

@Nullable
private VariableTree findDeclaration(VisitorState state, Symbol field) {
    JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context);
    TreePath fieldDeclPath = Trees.instance(javacEnv).getPath(field);
    // Skip fields declared in other compilation units since we can't make a fix for them here.
    if (fieldDeclPath != null && fieldDeclPath.getCompilationUnit() == state.getPath().getCompilationUnit() && (fieldDeclPath.getLeaf() instanceof VariableTree)) {
        return (VariableTree) fieldDeclPath.getLeaf();
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) VariableTree(com.sun.source.tree.VariableTree) Nullable(javax.annotation.Nullable)

Example 3 with VariableTree

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

the class ParameterNotNullable method findDeclaration.

@Nullable
private VariableTree findDeclaration(VisitorState state, Symbol parameter) {
    JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context);
    TreePath declPath = Trees.instance(javacEnv).getPath(parameter);
    if (declPath != null && declPath.getCompilationUnit() == state.getPath().getCompilationUnit() && (declPath.getLeaf() instanceof VariableTree)) {
        return (VariableTree) declPath.getLeaf();
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) VariableTree(com.sun.source.tree.VariableTree) Nullable(javax.annotation.Nullable)

Example 4 with VariableTree

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

use of com.sun.source.tree.VariableTree 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)

Aggregations

VariableTree (com.sun.source.tree.VariableTree)125 Tree (com.sun.source.tree.Tree)58 MethodTree (com.sun.source.tree.MethodTree)57 ClassTree (com.sun.source.tree.ClassTree)55 ExpressionTree (com.sun.source.tree.ExpressionTree)50 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)38 NewClassTree (com.sun.source.tree.NewClassTree)38 IdentifierTree (com.sun.source.tree.IdentifierTree)32 MemberSelectTree (com.sun.source.tree.MemberSelectTree)30 TreePath (com.sun.source.util.TreePath)27 JCTree (com.sun.tools.javac.tree.JCTree)27 AssignmentTree (com.sun.source.tree.AssignmentTree)24 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)23 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)22 ArrayList (java.util.ArrayList)22 BlockTree (com.sun.source.tree.BlockTree)21 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)21 AnnotationTree (com.sun.source.tree.AnnotationTree)20 BinaryTree (com.sun.source.tree.BinaryTree)19 NewArrayTree (com.sun.source.tree.NewArrayTree)19