use of com.google.errorprone.fixes.SuggestedFix.Builder 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.Builder in project error-prone by google.
the class SuggestedFixes method addSuppressWarnings.
/**
* Returns a fix that adds a {@code @SuppressWarnings(warningToSuppress)} to the closest
* suppressible element to the node pointed at by {@code state.getPath()}.
*
* <p>If the closest suppressible element already has a @SuppressWarning annotation,
* warningToSuppress will be added to the value in {@code @SuppressWarnings} instead.
*
* <p>In the event that a suppressible element couldn't be found (e.g.: the state is pointing at a
* CompilationUnit, or some other internal inconsistency has occurred), or the enclosing
* suppressible element already has a {@code @SuppressWarnings} annotation with {@code
* warningToSuppress}, this method will return null.
*/
@Nullable
public static Fix addSuppressWarnings(VisitorState state, String warningToSuppress) {
Builder fixBuilder = SuggestedFix.builder();
addSuppressWarnings(fixBuilder, state, warningToSuppress);
return fixBuilder.isEmpty() ? null : fixBuilder.build();
}
use of com.google.errorprone.fixes.SuggestedFix.Builder in project error-prone by google.
the class AbstractExpectedExceptionChecker method buildBaseFix.
protected BaseFix buildBaseFix(VisitorState state, List<Tree> expectations) {
String exceptionClass = "Throwable";
// additional assertions to perform on the captured exception (if any)
List<String> newAsserts = new ArrayList<>();
Builder fix = SuggestedFix.builder();
for (Tree expectation : expectations) {
MethodInvocationTree invocation = (MethodInvocationTree) ((ExpressionStatementTree) expectation).getExpression();
MethodSymbol symbol = ASTHelpers.getSymbol(invocation);
Symtab symtab = state.getSymtab();
List<? extends ExpressionTree> args = invocation.getArguments();
switch(symbol.getSimpleName().toString()) {
case "expect":
if (isSubtype(getOnlyElement(symbol.getParameters()).asType(), symtab.classType, state)) {
// expect(Class<?>)
exceptionClass = state.getSourceForNode(getReceiver(getOnlyElement(args)));
} else {
// expect(Matcher)
fix.addStaticImport("org.hamcrest.MatcherAssert.assertThat");
newAsserts.add(String.format("assertThat(thrown, %s);", state.getSourceForNode(getOnlyElement(args))));
}
break;
case "expectCause":
ExpressionTree matcher = getOnlyElement(invocation.getArguments());
if (IS_A.matches(matcher, state)) {
fix.addStaticImport("com.google.common.truth.Truth.assertThat");
newAsserts.add(String.format("assertThat(thrown).hasCauseThat().isInstanceOf(%s);", state.getSourceForNode(getOnlyElement(((MethodInvocationTree) matcher).getArguments()))));
} else {
fix.addStaticImport("org.hamcrest.MatcherAssert.assertThat");
newAsserts.add(String.format("assertThat(thrown.getCause(), %s);", state.getSourceForNode(getOnlyElement(args))));
}
break;
case "expectMessage":
if (isSubtype(getOnlyElement(symbol.getParameters()).asType(), symtab.stringType, state)) {
// expectedMessage(String)
fix.addStaticImport("com.google.common.truth.Truth.assertThat");
newAsserts.add(String.format("assertThat(thrown).hasMessageThat().contains(%s);", state.getSourceForNode(getOnlyElement(args))));
} else {
// expectedMessage(Matcher)
fix.addStaticImport("org.hamcrest.MatcherAssert.assertThat");
newAsserts.add(String.format("assertThat(thrown.getMessage(), %s);", state.getSourceForNode(getOnlyElement(args))));
}
break;
default:
throw new AssertionError("unknown expect method: " + symbol.getSimpleName());
}
}
// remove all interactions with the ExpectedException rule
fix.replace(((JCTree) expectations.get(0)).getStartPosition(), state.getEndPosition(getLast(expectations)), "");
return new BaseFix(fix.build(), exceptionClass, newAsserts);
}
Aggregations