Search in sources :

Example 46 with IdentifierTree

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

the class TreeUtils method isFieldAccess.

/**
     * Determine whether <code>tree</code> is a field access expressions, such
     * as
     *
     * <pre>
     *   <em>f</em>
     *   <em>obj</em> . <em>f</em>
     * </pre>
     *
     * @return true iff if tree is a field access expression (implicit or
     *         explicit).
     */
public static boolean isFieldAccess(Tree tree) {
    if (tree.getKind().equals(Tree.Kind.MEMBER_SELECT)) {
        // explicit field access
        MemberSelectTree memberSelect = (MemberSelectTree) tree;
        Element el = TreeUtils.elementFromUse(memberSelect);
        return el.getKind().isField();
    } else if (tree.getKind().equals(Tree.Kind.IDENTIFIER)) {
        // implicit field access
        IdentifierTree ident = (IdentifierTree) tree;
        Element el = TreeUtils.elementFromUse(ident);
        return el.getKind().isField() && !ident.getName().contentEquals("this") && !ident.getName().contentEquals("super");
    }
    return false;
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 47 with IdentifierTree

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

the class TreeUtils method isMethodAccess.

/**
     * Determine whether <code>tree</code> refers to a method element, such
     * as
     *
     * <pre>
     *   <em>m</em>(...)
     *   <em>obj</em> . <em>m</em>(...)
     * </pre>
     *
     * @return true iff if tree is a method access expression (implicit or
     *         explicit).
     */
public static boolean isMethodAccess(Tree tree) {
    if (tree.getKind().equals(Tree.Kind.MEMBER_SELECT)) {
        // explicit method access
        MemberSelectTree memberSelect = (MemberSelectTree) tree;
        Element el = TreeUtils.elementFromUse(memberSelect);
        return el.getKind() == ElementKind.METHOD || el.getKind() == ElementKind.CONSTRUCTOR;
    } else if (tree.getKind().equals(Tree.Kind.IDENTIFIER)) {
        // implicit method access
        IdentifierTree ident = (IdentifierTree) tree;
        // The field "super" and "this" are also legal methods
        if (ident.getName().contentEquals("super") || ident.getName().contentEquals("this")) {
            return true;
        }
        Element el = TreeUtils.elementFromUse(ident);
        return el.getKind() == ElementKind.METHOD || el.getKind() == ElementKind.CONSTRUCTOR;
    }
    return false;
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 48 with IdentifierTree

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

the class SimpleDateFormatConstant method threadLocalFix.

private static Fix threadLocalFix(VariableTree tree, VisitorState state, final VarSymbol sym) {
    SuggestedFix.Builder fix = SuggestedFix.builder().replace(tree.getType(), String.format("ThreadLocal<%s>", state.getSourceForNode(tree.getType()))).prefixWith(tree.getInitializer(), "ThreadLocal.withInitial(() -> ").postfixWith(tree.getInitializer(), ")");
    CompilationUnitTree unit = state.getPath().getCompilationUnit();
    unit.accept(new TreeScanner<Void, Void>() {

        @Override
        public Void visitIdentifier(IdentifierTree tree, Void unused) {
            if (Objects.equals(ASTHelpers.getSymbol(tree), sym)) {
                fix.postfixWith(tree, ".get()");
            }
            return null;
        }
    }, null);
    return fix.build();
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 49 with IdentifierTree

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

the class GuardedBySymbolResolver method resolveEnclosingClass.

@Override
public Symbol resolveEnclosingClass(ExpressionTree expr) {
    checkGuardedBy(expr instanceof IdentifierTree, "bad type literal: %s", expr);
    IdentifierTree ident = (IdentifierTree) expr;
    Symbol type = resolveType(ident.getName().toString(), SearchSuperTypes.NO);
    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 50 with IdentifierTree

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

the class ElementsCountedInLoop method matchWhileLoop.

@Override
public Description matchWhileLoop(WhileLoopTree tree, VisitorState state) {
    JCWhileLoop whileLoop = (JCWhileLoop) tree;
    JCExpression whileExpression = ((JCParens) whileLoop.getCondition()).getExpression();
    if (whileExpression instanceof MethodInvocationTree) {
        MethodInvocationTree methodInvocation = (MethodInvocationTree) whileExpression;
        if (instanceMethod().onDescendantOf("java.util.Iterator").withSignature("hasNext()").matches(methodInvocation, state)) {
            IdentifierTree identifier = getIncrementedIdentifer(extractSingleStatement(whileLoop.body));
            if (identifier != null) {
                return describeMatch(tree);
            }
        }
    }
    return Description.NO_MATCH;
}
Also used : JCWhileLoop(com.sun.tools.javac.tree.JCTree.JCWhileLoop) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) IdentifierTree(com.sun.source.tree.IdentifierTree) JCParens(com.sun.tools.javac.tree.JCTree.JCParens)

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