Search in sources :

Example 1 with ExpressionTree

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

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

the class TreeUtils method methodName.

/**
     * @return the name of the invoked method
     */
public static final Name methodName(MethodInvocationTree node) {
    ExpressionTree expr = node.getMethodSelect();
    if (expr.getKind() == Tree.Kind.IDENTIFIER)
        return ((IdentifierTree) expr).getName();
    else if (expr.getKind() == Tree.Kind.MEMBER_SELECT)
        return ((MemberSelectTree) expr).getIdentifier();
    ErrorReporter.errorAbort("TreeUtils.methodName: cannot be here: " + node);
    // dead code
    return null;
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 3 with ExpressionTree

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

the class AbstractCollectionIncompatibleTypeMatcher method matches.

@Nullable
public final MatchResult matches(MethodInvocationTree tree, VisitorState state) {
    if (!methodMatcher().matches(tree, state)) {
        return null;
    }
    ExpressionTree sourceTree = extractSourceTree(tree, state);
    Type sourceType = extractSourceType(tree, state);
    Type targetType = extractTargetType(tree, state);
    if (sourceTree == null || sourceType == null || targetType == null) {
        return null;
    }
    return MatchResult.create(extractSourceTree(tree, state), extractSourceType(tree, state), extractTargetType(tree, state), this);
}
Also used : Type(com.sun.tools.javac.code.Type) ExpressionTree(com.sun.source.tree.ExpressionTree) Nullable(javax.annotation.Nullable)

Example 4 with ExpressionTree

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

the class SizeGreaterThanOrEqualsZero method isGreaterThanEqualToZero.

private ExpressionType isGreaterThanEqualToZero(BinaryTree tree) {
    ExpressionTree literalOperand;
    ExpressionType returnType;
    switch(tree.getKind()) {
        case GREATER_THAN_EQUAL:
            literalOperand = tree.getRightOperand();
            returnType = ExpressionType.GREATER_THAN_EQUAL;
            break;
        case LESS_THAN_EQUAL:
            literalOperand = tree.getLeftOperand();
            returnType = ExpressionType.LESS_THAN_EQUAL;
            break;
        default:
            return ExpressionType.MISMATCH;
    }
    if (literalOperand.getKind() != Kind.INT_LITERAL) {
        return ExpressionType.MISMATCH;
    }
    if (!((LiteralTree) literalOperand).getValue().equals(0)) {
        return ExpressionType.MISMATCH;
    }
    return returnType;
}
Also used : ExpressionTree(com.sun.source.tree.ExpressionTree) LiteralTree(com.sun.source.tree.LiteralTree)

Example 5 with ExpressionTree

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

the class StringBuilderInitWithChar method matchNewClass.

@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
    if (ASTHelpers.isSameType(state.getSymtab().stringBuilderType, ASTHelpers.getType(tree.getIdentifier()), state) && tree.getArguments().size() == 1) {
        ExpressionTree argument = tree.getArguments().get(0);
        Type type = ((JCTree) argument).type;
        if (type.getKind() == TypeKind.CHAR) {
            if (argument.getKind() == Kind.CHAR_LITERAL) {
                char ch = (Character) ((LiteralTree) argument).getValue();
                return describeMatch(tree, SuggestedFix.replace(argument, "\"" + Convert.quote(Character.toString(ch)) + "\""));
            } else {
                return describeMatch(tree, SuggestedFix.replace(tree, "new StringBuilder().append(" + state.getSourceForNode((JCTree) argument) + ")"));
            }
        }
    }
    return Description.NO_MATCH;
}
Also used : Type(com.sun.tools.javac.code.Type) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree)

Aggregations

ExpressionTree (com.sun.source.tree.ExpressionTree)199 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)60 Tree (com.sun.source.tree.Tree)51 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)45 IdentifierTree (com.sun.source.tree.IdentifierTree)44 MemberSelectTree (com.sun.source.tree.MemberSelectTree)40 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)38 VariableTree (com.sun.source.tree.VariableTree)32 MethodTree (com.sun.source.tree.MethodTree)30 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)28 TreePath (com.sun.source.util.TreePath)26 ExecutableElement (javax.lang.model.element.ExecutableElement)26 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)24 Type (com.sun.tools.javac.code.Type)24 AssignmentTree (com.sun.source.tree.AssignmentTree)23 ArrayList (java.util.ArrayList)23 BinaryTree (com.sun.source.tree.BinaryTree)22 JCTree (com.sun.tools.javac.tree.JCTree)22 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)20 ClassTree (com.sun.source.tree.ClassTree)18