Search in sources :

Example 11 with ExpressionTree

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

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

the class ModifyingCollectionWithItself method describe.

/**
   * We expect that the lhs is a field and the rhs is an identifier, specifically a parameter to the
   * method. We base our suggested fixes on this expectation.
   *
   * <p>Case 1: If lhs is a field and rhs is an identifier, find a method parameter of the same type
   * and similar name and suggest it as the rhs. (Guess that they have misspelled the identifier.)
   *
   * <p>Case 2: If lhs is a field and rhs is not an identifier, find a method parameter of the same
   * type and similar name and suggest it as the rhs.
   *
   * <p>Case 3: If lhs is not a field and rhs is an identifier, find a class field of the same type
   * and similar name and suggest it as the lhs.
   *
   * <p>Case 4: Otherwise replace with literal meaning of functionality
   */
private Description describe(MethodInvocationTree methodInvocationTree, VisitorState state) {
    ExpressionTree receiver = ASTHelpers.getReceiver(methodInvocationTree);
    List<? extends ExpressionTree> arguments = methodInvocationTree.getArguments();
    ExpressionTree argument;
    // .addAll(int, Collection); for the true case
    argument = arguments.size() == 2 ? arguments.get(1) : arguments.get(0);
    Description.Builder builder = buildDescription(methodInvocationTree);
    for (Fix fix : buildFixes(methodInvocationTree, state, receiver, argument)) {
        builder.addFix(fix);
    }
    return builder.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 13 with ExpressionTree

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

the class CompileTimeConstantChecker method matchArguments.

/**
   * Matches formal parameters with
   * {@link com.google.errorprone.annotations.CompileTimeConstant} annotations against
   * corresponding actual parameters.
   *
   * @param state        the visitor state
   * @param calleeSymbol the method whose formal parameters to consider
   * @param actualParams the list of actual parameters
   * @return a {@code Description} of the match <i>iff</i> for any of the actual parameters that
   * is annotated with {@link com.google.errorprone.annotations.CompileTimeConstant}, the
   * corresponding formal parameter is not a compile-time-constant expression in the sense of
   * {@link CompileTimeConstantExpressionMatcher}. Otherwise returns {@code Description.NO_MATCH}.
   */
private Description matchArguments(VisitorState state, final Symbol.MethodSymbol calleeSymbol, Iterator<? extends ExpressionTree> actualParams) {
    Symbol.VarSymbol lastFormalParam = null;
    for (Symbol.VarSymbol formalParam : calleeSymbol.getParameters()) {
        lastFormalParam = formalParam;
        // be a non-constant parameter for a @CompileTimeConstant formal.
        if (!actualParams.hasNext()) {
            return Description.NO_MATCH;
        }
        ExpressionTree actualParam = actualParams.next();
        if (hasCompileTimeConstantAnnotation(state, formalParam)) {
            if (!compileTimeConstExpressionMatcher.matches(actualParam, state)) {
                return handleMatch(actualParam, state);
            }
        }
    }
    // we need to check the remaining args as well.
    if (lastFormalParam == null || (lastFormalParam.flags() & Flags.VARARGS) == 0) {
        return Description.NO_MATCH;
    }
    if (!hasCompileTimeConstantAnnotation(state, lastFormalParam)) {
        return Description.NO_MATCH;
    }
    while (actualParams.hasNext()) {
        ExpressionTree actualParam = actualParams.next();
        if (!compileTimeConstExpressionMatcher.matches(actualParam, state)) {
            return handleMatch(actualParam, state);
        }
    }
    return Description.NO_MATCH;
}
Also used : VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 14 with ExpressionTree

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

the class DefaultCharset method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    if (state.isAndroidCompatible()) {
        return NO_MATCH;
    }
    if (STRING_GET_BYTES.matches(tree, state)) {
        Description.Builder description = buildDescription(tree);
        Tree parent = state.getPath().getParentPath().getLeaf();
        if (parent instanceof ExpressionTree && BYTESTRING_COPY_FROM.matches((ExpressionTree) parent, state)) {
            byteStringFixes(description, tree, (ExpressionTree) parent, state);
        } else {
            appendCharsets(description, tree, tree.getMethodSelect(), tree.getArguments(), state);
        }
        return description.build();
    }
    if (FILE_NEW_WRITER.matches(tree, state)) {
        Description.Builder description = buildDescription(tree);
        appendCharsets(description, tree, tree.getMethodSelect(), tree.getArguments(), state);
        return description.build();
    }
    return NO_MATCH;
}
Also used : Description(com.google.errorprone.matchers.Description) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NewClassTree(com.sun.source.tree.NewClassTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 15 with ExpressionTree

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

the class PreconditionsCheckNotNull method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree methodInvocationTree, VisitorState state) {
    if (!matcher.matches(methodInvocationTree, state)) {
        return Description.NO_MATCH;
    }
    List<? extends ExpressionTree> arguments = methodInvocationTree.getArguments();
    ExpressionTree stringLiteralValue = arguments.get(0);
    Fix fix;
    if (arguments.size() == 2) {
        fix = SuggestedFix.swap(arguments.get(0), arguments.get(1));
    } else {
        fix = SuggestedFix.delete(state.getPath().getParentPath().getLeaf());
    }
    return describeMatch(stringLiteralValue, fix);
}
Also used : SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Fix(com.google.errorprone.fixes.Fix) ExpressionTree(com.sun.source.tree.ExpressionTree)

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