Search in sources :

Example 1 with MemberSelectTree

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

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

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

the class SelfAssignment method matchVariable.

@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
    ExpressionTree initializer = stripCheckNotNull(tree.getInitializer(), state);
    Tree parent = state.getPath().getParentPath().getLeaf();
    // must be a static class variable with member select initializer
    if (initializer == null || initializer.getKind() != MEMBER_SELECT || parent.getKind() != CLASS || !tree.getModifiers().getFlags().contains(STATIC)) {
        return Description.NO_MATCH;
    }
    MemberSelectTree rhs = (MemberSelectTree) initializer;
    Symbol rhsClass = ASTHelpers.getSymbol(rhs.getExpression());
    Symbol lhsClass = ASTHelpers.getSymbol(parent);
    if (rhsClass != null && lhsClass != null && rhsClass.equals(lhsClass) && rhs.getIdentifier().contentEquals(tree.getName())) {
        return describeForVarDecl(tree, state);
    }
    return Description.NO_MATCH;
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionTree(com.sun.source.tree.ExpressionTree) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree)

Example 4 with MemberSelectTree

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

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

the class ASTHelpers method hasSimpleName.

private static boolean hasSimpleName(AnnotationTree annotation, String name) {
    Tree annotationType = annotation.getAnnotationType();
    javax.lang.model.element.Name simpleName;
    if (annotationType instanceof IdentifierTree) {
        simpleName = ((IdentifierTree) annotationType).getName();
    } else if (annotationType instanceof MemberSelectTree) {
        simpleName = ((MemberSelectTree) annotationType).getIdentifier();
    } else {
        return false;
    }
    return simpleName.contentEquals(name);
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) PackageTree(com.sun.source.tree.PackageTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) IdentifierTree(com.sun.source.tree.IdentifierTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ModifiersTree(com.sun.source.tree.ModifiersTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree) IdentifierTree(com.sun.source.tree.IdentifierTree)

Aggregations

MemberSelectTree (com.sun.source.tree.MemberSelectTree)15 IdentifierTree (com.sun.source.tree.IdentifierTree)10 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)9 ExpressionTree (com.sun.source.tree.ExpressionTree)8 JCTree (com.sun.tools.javac.tree.JCTree)8 Tree (com.sun.source.tree.Tree)7 VariableTree (com.sun.source.tree.VariableTree)5 Symbol (com.sun.tools.javac.code.Symbol)4 ClassTree (com.sun.source.tree.ClassTree)3 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)3 NewClassTree (com.sun.source.tree.NewClassTree)3 Element (javax.lang.model.element.Element)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 TypeElement (javax.lang.model.element.TypeElement)3 VariableElement (javax.lang.model.element.VariableElement)3 ImmutableList (com.google.common.collect.ImmutableList)2 AssignmentTree (com.sun.source.tree.AssignmentTree)2 BinaryTree (com.sun.source.tree.BinaryTree)2 BlockTree (com.sun.source.tree.BlockTree)2 ImportTree (com.sun.source.tree.ImportTree)2