Search in sources :

Example 1 with IdentifierTree

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

the class TreeUtils method isSelfAccess.

/**
     * Returns true if the tree is a tree that 'looks like' either an access
     * of a field or an invocation of a method that are owned by the same
     * accessing instance.
     *
     * It would only return true if the access tree is of the form:
     * <pre>
     *   field
     *   this.field
     *
     *   method()
     *   this.method()
     * </pre>
     *
     * It does not perform any semantical check to differentiate between
     * fields and local variables; local methods or imported static methods.
     *
     * @param tree  expression tree representing an access to object member
     * @return {@code true} iff the member is a member of {@code this} instance
     */
public static boolean isSelfAccess(final ExpressionTree tree) {
    ExpressionTree tr = TreeUtils.skipParens(tree);
    // If method invocation check the method select
    if (tr.getKind() == Tree.Kind.ARRAY_ACCESS)
        return false;
    if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) {
        tr = ((MethodInvocationTree) tree).getMethodSelect();
    }
    tr = TreeUtils.skipParens(tr);
    if (tr.getKind() == Tree.Kind.TYPE_CAST)
        tr = ((TypeCastTree) tr).getExpression();
    tr = TreeUtils.skipParens(tr);
    if (tr.getKind() == Tree.Kind.IDENTIFIER)
        return true;
    if (tr.getKind() == Tree.Kind.MEMBER_SELECT) {
        tr = ((MemberSelectTree) tr).getExpression();
        if (tr.getKind() == Tree.Kind.IDENTIFIER) {
            Name ident = ((IdentifierTree) tr).getName();
            return ident.contentEquals("this") || ident.contentEquals("super");
        }
    }
    return false;
}
Also used : TypeCastTree(com.sun.source.tree.TypeCastTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Name(javax.lang.model.element.Name)

Example 2 with IdentifierTree

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

the class GuardedBySymbolResolver method resolveTypeLiteral.

@Override
public Symbol resolveTypeLiteral(ExpressionTree expr) {
    checkGuardedBy(expr instanceof IdentifierTree, "bad type literal: %s", expr);
    IdentifierTree ident = (IdentifierTree) expr;
    Symbol type = resolveType(ident.getName().toString(), SearchSuperTypes.YES);
    if (type instanceof Symbol.ClassSymbol) {
        return type;
    }
    return null;
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 3 with IdentifierTree

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

the class ElementsCountedInLoop method matchEnhancedForLoop.

@Override
public Description matchEnhancedForLoop(EnhancedForLoopTree tree, VisitorState state) {
    JCEnhancedForLoop enhancedForLoop = (JCEnhancedForLoop) tree;
    IdentifierTree identifier = getIncrementedIdentifer(extractSingleStatement(enhancedForLoop.body));
    if (identifier != null) {
        ExpressionTree expression = tree.getExpression();
        Fix fix;
        if (isSubtypeOf("java.util.Collection").matches(expression, state)) {
            String replacement = identifier + " += " + expression + ".size();";
            fix = SuggestedFix.replace(tree, replacement);
        } else if (isArrayType().matches(expression, state)) {
            String replacement = identifier + " += " + expression + ".length;";
            fix = SuggestedFix.replace(tree, replacement);
        } else {
            String replacement = identifier + " += Iterables.size(" + expression + ");";
            fix = SuggestedFix.builder().replace(tree, replacement).addImport("com.google.common.collect.Iterables").build();
        }
        return describeMatch(tree, fix);
    }
    return Description.NO_MATCH;
}
Also used : Fix(com.google.errorprone.fixes.Fix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) JCEnhancedForLoop(com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop) IdentifierTree(com.sun.source.tree.IdentifierTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 4 with IdentifierTree

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

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

the class UFreeIdent method defaultAction.

@Override
protected Choice<Unifier> defaultAction(Tree target, final Unifier unifier) {
    if (target instanceof JCExpression) {
        JCExpression expression = (JCExpression) target;
        JCExpression currentBinding = unifier.getBinding(key());
        // Check that the expression does not reference any template-local variables.
        boolean isGood = trueOrNull(new TreeScanner<Boolean, Void>() {

            @Override
            public Boolean reduce(@Nullable Boolean left, @Nullable Boolean right) {
                return trueOrNull(left) && trueOrNull(right);
            }

            @Override
            public Boolean visitIdentifier(IdentifierTree ident, Void v) {
                Symbol identSym = ASTHelpers.getSymbol(ident);
                for (ULocalVarIdent.Key key : Iterables.filter(unifier.getBindings().keySet(), ULocalVarIdent.Key.class)) {
                    if (identSym == unifier.getBinding(key).getSymbol()) {
                        return false;
                    }
                }
                return true;
            }
        }.scan(expression, null));
        if (!isGood) {
            return Choice.none();
        } else if (currentBinding == null) {
            unifier.putBinding(key(), expression);
            return Choice.of(unifier);
        } else if (currentBinding.toString().equals(expression.toString())) {
            // If it's the same code, treat it as the same expression.
            return Choice.of(unifier);
        }
    }
    return Choice.none();
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) TreeScanner(com.sun.source.util.TreeScanner) Symbol(com.sun.tools.javac.code.Symbol) IdentifierTree(com.sun.source.tree.IdentifierTree) Nullable(javax.annotation.Nullable)

Aggregations

IdentifierTree (com.sun.source.tree.IdentifierTree)82 ExpressionTree (com.sun.source.tree.ExpressionTree)41 MemberSelectTree (com.sun.source.tree.MemberSelectTree)36 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)28 Element (javax.lang.model.element.Element)24 Tree (com.sun.source.tree.Tree)21 ExecutableElement (javax.lang.model.element.ExecutableElement)18 VariableTree (com.sun.source.tree.VariableTree)17 TypeElement (javax.lang.model.element.TypeElement)16 MethodTree (com.sun.source.tree.MethodTree)13 VariableElement (javax.lang.model.element.VariableElement)13 ClassTree (com.sun.source.tree.ClassTree)12 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)11 ArrayAccessTree (com.sun.source.tree.ArrayAccessTree)10 NewClassTree (com.sun.source.tree.NewClassTree)10 AssignmentTree (com.sun.source.tree.AssignmentTree)9 BinaryTree (com.sun.source.tree.BinaryTree)9 LiteralTree (com.sun.source.tree.LiteralTree)8 StatementTree (com.sun.source.tree.StatementTree)8 Symbol (com.sun.tools.javac.code.Symbol)8