use of com.google.errorprone.fixes.SuggestedFix in project error-prone by google.
the class TypeParameterShadowing method renameTypeVariable.
private static SuggestedFix renameTypeVariable(Tree sourceTree, List<? extends TypeParameterTree> typeParameters, Name typeVariable, String typeVarReplacement, VisitorState state) {
TypeParameterTree matchingTypeParam = typeParameters.stream().filter(t -> t.getName().contentEquals(typeVariable)).collect(MoreCollectors.onlyElement());
Symbol typeVariableSymbol = ASTHelpers.getSymbol(matchingTypeParam);
// replace only the type parameter name (and not any upper bounds)
String name = matchingTypeParam.getName().toString();
int pos = ((JCTree) matchingTypeParam).getStartPosition();
SuggestedFix.Builder fixBuilder = SuggestedFix.builder().replace(pos, pos + name.length(), typeVarReplacement);
((JCTree) sourceTree).accept(new TreeScanner() {
@Override
public void visitIdent(JCTree.JCIdent tree) {
Symbol identSym = ASTHelpers.getSymbol(tree);
if (Objects.equal(identSym, typeVariableSymbol)) {
// }
if (Objects.equal(state.getSourceForNode(tree), name)) {
fixBuilder.replace(tree, typeVarReplacement);
}
}
}
});
return fixBuilder.build();
}
use of com.google.errorprone.fixes.SuggestedFix in project error-prone by google.
the class QualifierOnMethodWithoutProvides method matchMethod.
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
MultiMatchResult<AnnotationTree> qualifierAnnotations = QUALIFIER_ANNOTATION_FINDER.multiMatchResult(tree, state);
if (qualifierAnnotations.matches() && NOT_ABSTRACT.matches(tree, state) && NOT_PROVIDES_METHOD.matches(tree, state)) {
// Otherwise, suggest removing the qualifier annotation
if (not(methodReturns(anyOf(isSameType(Suppliers.VOID_TYPE), isSameType(Suppliers.JAVA_LANG_VOID_TYPE)))).matches(tree, state)) {
if (INSIDE_GUICE_MODULE.matches(tree, state)) {
// Guice Module
SuggestedFix fix = SuggestedFix.builder().addStaticImport(GUICE_PROVIDES_ANNOTATION).prefixWith(tree, "@Provides ").build();
return describeMatch(tree, fix);
}
if (enclosingClass(IS_DAGGER_COMPONENT_OR_MODULE).matches(tree, state)) {
// Dagger component
SuggestedFix fix = SuggestedFix.builder().addStaticImport(DAGGER_PROVIDES_ANNOTATION).prefixWith(tree, "@Provides ").build();
return describeMatch(tree, fix);
}
}
List<AnnotationTree> matchingNodes = qualifierAnnotations.matchingNodes();
Builder fixBuilder = SuggestedFix.builder();
matchingNodes.forEach(fixBuilder::delete);
return describeMatch(matchingNodes.get(0), fixBuilder.build());
}
return Description.NO_MATCH;
}
use of com.google.errorprone.fixes.SuggestedFix in project error-prone by google.
the class AbstractJUnit4InitMethodNotRun method matchMethod.
/**
* Matches if all of the following conditions are true: 1) The method matches {@link
* #methodMatcher()}, (looks like setUp() or tearDown(), and none of the overrides in the
* hierarchy of the method have the appropriate @Before or @After annotations) 2) The method is
* not annotated with @Test 3) The enclosing class has an @RunWith annotation and does not extend
* TestCase. This marks that the test is intended to run with JUnit 4.
*/
@Override
public Description matchMethod(MethodTree methodTree, VisitorState state) {
boolean matches = allOf(methodMatcher(), not(hasAnnotationOnAnyOverriddenMethod(JUNIT_TEST)), enclosingClass(isJUnit4TestClass)).matches(methodTree, state);
if (!matches) {
return Description.NO_MATCH;
}
// For each annotationReplacement, replace the first annotation that matches. If any of them
// matches, don't try and do the rest of the work.
Description description;
for (AnnotationReplacements replacement : annotationReplacements()) {
description = tryToReplaceAnnotation(methodTree, state, replacement.badAnnotation, replacement.goodAnnotation);
if (description != null) {
return description;
}
}
// Search for another @Before annotation on the method and replace the import
// if we find one
String correctAnnotation = correctAnnotation();
String unqualifiedClassName = getUnqualifiedClassName(correctAnnotation);
for (AnnotationTree annotationNode : methodTree.getModifiers().getAnnotations()) {
Symbol annoSymbol = ASTHelpers.getSymbol(annotationNode);
if (annoSymbol.getSimpleName().toString().equals(unqualifiedClassName)) {
SuggestedFix.Builder suggestedFix = SuggestedFix.builder().removeImport(annoSymbol.getQualifiedName().toString()).addImport(correctAnnotation);
makeProtectedPublic(methodTree, state, suggestedFix);
return describeMatch(annotationNode, suggestedFix.build());
}
}
// Add correctAnnotation() to the unannotated method
// (and convert protected to public if it is)
SuggestedFix.Builder suggestedFix = SuggestedFix.builder().addImport(correctAnnotation);
makeProtectedPublic(methodTree, state, suggestedFix);
suggestedFix.prefixWith(methodTree, "@" + unqualifiedClassName + "\n");
return describeMatch(methodTree, suggestedFix.build());
}
use of com.google.errorprone.fixes.SuggestedFix in project error-prone by google.
the class Changes method buildCommentArgumentsFix.
SuggestedFix buildCommentArgumentsFix(InvocationInfo info) {
SuggestedFix.Builder commentArgumentsFixBuilder = SuggestedFix.builder();
for (ParameterPair change : changedPairs()) {
int index = change.formal().index();
ExpressionTree actual = info.actualParameters().get(index);
int startPosition = ((JCTree) actual).getStartPosition();
String formal = info.formalParameters().get(index).getSimpleName().toString();
commentArgumentsFixBuilder.replace(startPosition, startPosition, NamedParameterComment.toCommentText(formal));
}
return commentArgumentsFixBuilder.build();
}
use of com.google.errorprone.fixes.SuggestedFix in project error-prone by google.
the class ScopeOnModule method matchClass.
@Override
public Description matchClass(ClassTree classTree, VisitorState state) {
ClassSymbol classSymbol = getSymbol(classTree);
if (!hasAnnotation(classSymbol, "dagger.Module", state) && !hasAnnotation(classSymbol, "dagger.producers.ProducerModule", state)) {
return Description.NO_MATCH;
}
List<SuggestedFix> fixes = new ArrayList<>();
for (AnnotationTree annotation : classTree.getModifiers().getAnnotations()) {
Symbol annotationType = getSymbol(annotation.getAnnotationType());
if (hasAnnotation(annotationType, "javax.inject.Scope", state)) {
fixes.add(SuggestedFix.delete(annotation));
}
}
if (fixes.isEmpty()) {
return Description.NO_MATCH;
}
return buildDescription(classTree).addAllFixes(fixes).build();
}
Aggregations