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);
}
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;
}
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);
}
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);
}
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;
}
Aggregations