Search in sources :

Example 1 with Fix

use of com.google.errorprone.fixes.Fix in project error-prone by google.

the class WildcardImport method matchCompilationUnit.

@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
    ImmutableList<ImportTree> wildcardImports = getWildcardImports(tree.getImports());
    if (wildcardImports.isEmpty()) {
        return NO_MATCH;
    }
    // Find all of the types that need to be imported.
    Set<TypeToImport> typesToImport = ImportCollector.collect((JCCompilationUnit) tree);
    Fix fix = createFix(wildcardImports, typesToImport, state);
    if (fix.isEmpty()) {
        return NO_MATCH;
    }
    return describeMatch(wildcardImports.get(0), fix);
}
Also used : Fix(com.google.errorprone.fixes.Fix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ImportTree(com.sun.source.tree.ImportTree)

Example 2 with Fix

use of com.google.errorprone.fixes.Fix 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 3 with Fix

use of com.google.errorprone.fixes.Fix in project error-prone by google.

the class BadShiftAmount method matchBinary.

@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
    if (!BINARY_TREE_MATCHER.matches(tree, state)) {
        return Description.NO_MATCH;
    }
    /*
     * For shift amounts in [32, 63], cast the left operand to long.  Otherwise change the shift
     * amount to whatever would actually be used.
     */
    int intValue = ((Number) ((LiteralTree) tree.getRightOperand()).getValue()).intValue();
    Fix fix;
    if (intValue >= 32 && intValue <= 63) {
        if (tree.getLeftOperand().getKind() == Kind.INT_LITERAL) {
            fix = SuggestedFix.postfixWith(tree.getLeftOperand(), "L");
        } else {
            fix = SuggestedFix.prefixWith(tree, "(long) ");
        }
    } else {
        // This is the equivalent shift distance according to JLS 15.19.
        String actualShiftDistance = Integer.toString(intValue & 0x1f);
        fix = SuggestedFix.replace(tree.getRightOperand(), actualShiftDistance);
    }
    return describeMatch(tree, fix);
}
Also used : ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Fix(com.google.errorprone.fixes.Fix)

Example 4 with Fix

use of com.google.errorprone.fixes.Fix in project error-prone by google.

the class ArrayEquals method matchMethodInvocation.

/**
 * Suggests replacing with Arrays.equals(a, b). Also adds the necessary import statement for
 * java.util.Arrays.
 */
@Override
public Description matchMethodInvocation(MethodInvocationTree t, VisitorState state) {
    String arg1;
    String arg2;
    if (instanceEqualsMatcher.matches(t, state)) {
        arg1 = ((JCFieldAccess) t.getMethodSelect()).getExpression().toString();
        arg2 = t.getArguments().get(0).toString();
    } else if (staticEqualsMatcher.matches(t, state)) {
        arg1 = t.getArguments().get(0).toString();
        arg2 = t.getArguments().get(1).toString();
    } else {
        return NO_MATCH;
    }
    Fix fix = SuggestedFix.builder().replace(t, "Arrays.equals(" + arg1 + ", " + arg2 + ")").addImport("java.util.Arrays").build();
    return describeMatch(t, fix);
}
Also used : ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Fix(com.google.errorprone.fixes.Fix) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess)

Example 5 with Fix

use of com.google.errorprone.fixes.Fix in project error-prone by google.

the class HashtableContains method replaceMethodName.

private Fix replaceMethodName(MethodInvocationTree tree, VisitorState state, String newName) {
    String source = state.getSourceForNode((JCTree) tree.getMethodSelect());
    int idx = source.lastIndexOf("contains");
    String replacement = source.substring(0, idx) + newName + source.substring(idx + "contains".length());
    Fix fix = SuggestedFix.replace(tree.getMethodSelect(), replacement);
    return fix;
}
Also used : ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Fix(com.google.errorprone.fixes.Fix)

Aggregations

Fix (com.google.errorprone.fixes.Fix)29 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)28 ProvidesFix (com.google.errorprone.BugPattern.ProvidesFix)21 ExpressionTree (com.sun.source.tree.ExpressionTree)13 Description (com.google.errorprone.matchers.Description)7 Tree (com.sun.source.tree.Tree)7 Type (com.sun.tools.javac.code.Type)7 IdentifierTree (com.sun.source.tree.IdentifierTree)5 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)5 JCTree (com.sun.tools.javac.tree.JCTree)5 VisitorState (com.google.errorprone.VisitorState)4 MethodTree (com.sun.source.tree.MethodTree)4 StatementTree (com.sun.source.tree.StatementTree)4 BugPattern (com.google.errorprone.BugPattern)3 ASTHelpers (com.google.errorprone.util.ASTHelpers)3 ClassTree (com.sun.source.tree.ClassTree)3 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)3 ImmutableList (com.google.common.collect.ImmutableList)2 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)2 JDK (com.google.errorprone.BugPattern.Category.JDK)2