Search in sources :

Example 1 with Builder

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;
}
Also used : SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Builder(com.google.errorprone.fixes.SuggestedFix.Builder) AnnotationTree(com.sun.source.tree.AnnotationTree)

Example 2 with Builder

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();
}
Also used : Builder(com.google.errorprone.fixes.SuggestedFix.Builder) Nullable(javax.annotation.Nullable)

Example 3 with Builder

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);
}
Also used : Symtab(com.sun.tools.javac.code.Symtab) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) Builder(com.google.errorprone.fixes.SuggestedFix.Builder) ArrayList(java.util.ArrayList) MethodTree(com.sun.source.tree.MethodTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) StatementTree(com.sun.source.tree.StatementTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

Builder (com.google.errorprone.fixes.SuggestedFix.Builder)3 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)1 AnnotationTree (com.sun.source.tree.AnnotationTree)1 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)1 MethodTree (com.sun.source.tree.MethodTree)1 StatementTree (com.sun.source.tree.StatementTree)1 Tree (com.sun.source.tree.Tree)1 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)1 Symtab (com.sun.tools.javac.code.Symtab)1 JCTree (com.sun.tools.javac.tree.JCTree)1 ArrayList (java.util.ArrayList)1 Nullable (javax.annotation.Nullable)1