Search in sources :

Example 1 with SuggestedFix

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();
}
Also used : ClassTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher) MethodTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher) MethodTree(com.sun.source.tree.MethodTree) TreeScanner(com.sun.tools.javac.tree.TreeScanner) TypeParameterTree(com.sun.source.tree.TypeParameterTree) ArrayList(java.util.ArrayList) VisitorState(com.google.errorprone.VisitorState) Matcher(java.util.regex.Matcher) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BugPattern(com.google.errorprone.BugPattern) JDK(com.google.errorprone.BugPattern.Category.JDK) Objects(com.google.common.base.Objects) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) Name(javax.lang.model.element.Name) MoreCollectors(com.google.common.collect.MoreCollectors) Symbol(com.sun.tools.javac.code.Symbol) Set(java.util.Set) Streams(com.google.common.collect.Streams) JCTree(com.sun.tools.javac.tree.JCTree) Collectors(java.util.stream.Collectors) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) List(java.util.List) Description(com.google.errorprone.matchers.Description) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Pattern(java.util.regex.Pattern) ASTHelpers(com.google.errorprone.util.ASTHelpers) TypeParameterTree(com.sun.source.tree.TypeParameterTree) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) TreeScanner(com.sun.tools.javac.tree.TreeScanner) Symbol(com.sun.tools.javac.code.Symbol) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) JCTree(com.sun.tools.javac.tree.JCTree)

Example 2 with SuggestedFix

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

Example 3 with SuggestedFix

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

Example 4 with SuggestedFix

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

Example 5 with SuggestedFix

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();
}
Also used : SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ASTHelpers.getSymbol(com.google.errorprone.util.ASTHelpers.getSymbol) Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ArrayList(java.util.ArrayList) AnnotationTree(com.sun.source.tree.AnnotationTree)

Aggregations

SuggestedFix (com.google.errorprone.fixes.SuggestedFix)16 ExpressionTree (com.sun.source.tree.ExpressionTree)5 Tree (com.sun.source.tree.Tree)5 AnnotationTree (com.sun.source.tree.AnnotationTree)4 Symbol (com.sun.tools.javac.code.Symbol)4 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)4 Description (com.google.errorprone.matchers.Description)3 Type (com.sun.tools.javac.code.Type)3 JCTree (com.sun.tools.javac.tree.JCTree)3 ArrayList (java.util.ArrayList)3 Streams (com.google.common.collect.Streams)2 BugPattern (com.google.errorprone.BugPattern)2 JDK (com.google.errorprone.BugPattern.Category.JDK)2 VisitorState (com.google.errorprone.VisitorState)2 ClassTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher)2 Builder (com.google.errorprone.fixes.SuggestedFix.Builder)2 ASTHelpers (com.google.errorprone.util.ASTHelpers)2 ClassTree (com.sun.source.tree.ClassTree)2 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)2 MethodTree (com.sun.source.tree.MethodTree)2