Search in sources :

Example 16 with ExpressionTree

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

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

the class SelfEquals method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    if (ASSERTION.matches(state.getPath().getParentPath().getLeaf(), state)) {
        return NO_MATCH;
    }
    List<? extends ExpressionTree> args = tree.getArguments();
    ExpressionTree toReplace;
    if (INSTANCE_MATCHER.matches(tree, state)) {
        toReplace = args.get(0);
    } else if (STATIC_MATCHER.matches(tree, state)) {
        if (args.get(0).getKind() == Kind.IDENTIFIER && args.get(1).getKind() != Kind.IDENTIFIER) {
            toReplace = args.get(0);
        } else {
            toReplace = args.get(1);
        }
    } else {
        return NO_MATCH;
    }
    Description.Builder description = buildDescription(tree);
    Fix fix = fieldFix(toReplace, state);
    if (fix != null) {
        description.addFix(fix);
    }
    return description.build();
}
Also used : Description(com.google.errorprone.matchers.Description) Fix(com.google.errorprone.fixes.Fix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 18 with ExpressionTree

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

the class JUnit3FloatingPointComparisonWithoutDelta method getArgumentTypesWithoutMessage.

/**
   * Gets the argument types, excluding the message argument if present.
   */
private List<Type> getArgumentTypesWithoutMessage(MethodInvocationTree methodInvocationTree, VisitorState state) {
    List<Type> argumentTypes = new ArrayList<>();
    for (ExpressionTree argument : methodInvocationTree.getArguments()) {
        JCTree tree = (JCTree) argument;
        argumentTypes.add(tree.type);
    }
    removeMessageArgumentIfPresent(state, argumentTypes);
    return argumentTypes;
}
Also used : Type(com.sun.tools.javac.code.Type) ArrayList(java.util.ArrayList) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree)

Example 19 with ExpressionTree

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

the class ProtoFieldNullComparison method isGetListMethodInvocation.

private static boolean isGetListMethodInvocation(ExpressionTree tree, VisitorState state) {
    if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) {
        MethodInvocationTree method = (MethodInvocationTree) tree;
        if (!method.getArguments().isEmpty()) {
            return false;
        }
        if (!returnsListMatcher.matches(method, state)) {
            return false;
        }
        ExpressionTree expressionTree = method.getMethodSelect();
        if (expressionTree instanceof JCFieldAccess) {
            JCFieldAccess access = (JCFieldAccess) expressionTree;
            String methodName = access.sym.getQualifiedName().toString();
            return isFieldGetMethod(methodName);
        }
        return true;
    }
    return false;
}
Also used : JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 20 with ExpressionTree

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

the class ProtoFieldNullComparison method createReplacement.

/**
   * Creates replacements for the following comparisons:
   * <pre>
   * proto.getField() == null --> !proto.hasField()
   * proto.getField() != null --> proto.hasField()
   * proto.getList() == null  --> proto.getList().isEmpty()
   * proto.getList() != null  --> !proto.getList().isEmpty()
   * <pre>
   * Also creates replacements for the Yoda style version of them.
   */
@Nullable
private static String createReplacement(BinaryTree tree, VisitorState state) {
    ExpressionTree leftOperand = tree.getLeftOperand();
    ExpressionTree rightOperand = tree.getRightOperand();
    ExpressionTree methodInvocation;
    if (isNull(leftOperand)) {
        methodInvocation = rightOperand;
    } else {
        methodInvocation = leftOperand;
    }
    if (isGetMethodInvocation(methodInvocation, state)) {
        String methodName = getMethodName(methodInvocation);
        String hasMethod = methodName.replaceFirst("get", "has");
        // proto3 does not generate has methods for scalar types, e.g. ByteString and String.
        // Do not provide a replacement in these cases.
        Set<MethodSymbol> hasMethods = ASTHelpers.findMatchingMethods(state.getName(hasMethod), NO_ARGS, ASTHelpers.getType(ASTHelpers.getReceiver(methodInvocation)), state.getTypes());
        if (hasMethods.isEmpty()) {
            return null;
        }
        String replacement = replaceLast(methodInvocation.toString(), methodName, hasMethod);
        replacement = tree.getKind() == Kind.EQUAL_TO ? "!" + replacement : replacement;
        return replacement;
    } else {
        String replacement = methodInvocation + ".isEmpty()";
        return tree.getKind() == Kind.EQUAL_TO ? replacement : "!" + replacement;
    }
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ExpressionTree(com.sun.source.tree.ExpressionTree) Nullable(javax.annotation.Nullable)

Aggregations

ExpressionTree (com.sun.source.tree.ExpressionTree)78 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)27 Tree (com.sun.source.tree.Tree)18 Type (com.sun.tools.javac.code.Type)18 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)16 Symbol (com.sun.tools.javac.code.Symbol)14 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)14 JCTree (com.sun.tools.javac.tree.JCTree)14 Fix (com.google.errorprone.fixes.Fix)13 VariableTree (com.sun.source.tree.VariableTree)13 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)13 IdentifierTree (com.sun.source.tree.IdentifierTree)12 MemberSelectTree (com.sun.source.tree.MemberSelectTree)12 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)11 VisitorState (com.google.errorprone.VisitorState)9 AssignmentTree (com.sun.source.tree.AssignmentTree)9 BinaryTree (com.sun.source.tree.BinaryTree)9 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)8 MethodTree (com.sun.source.tree.MethodTree)8 TreePath (com.sun.source.util.TreePath)8