Search in sources :

Example 6 with SuggestedFix

use of com.google.errorprone.fixes.SuggestedFix in project error-prone by google.

the class MutableConstantField method matchVariable.

@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
    if (!isConstantField(ASTHelpers.getSymbol(tree))) {
        return Description.NO_MATCH;
    }
    Tree lhsTree = tree.getType();
    Symbol lhsSymbol = ASTHelpers.getSymbol(lhsTree);
    if (lhsSymbol == null) {
        return Description.NO_MATCH;
    }
    String lhsTypeQualifiedName = lhsSymbol.getQualifiedName().toString();
    if (!MUTABLE_TO_IMMUTABLE_CLASS_NAME_MAP.containsKey(lhsTypeQualifiedName)) {
        return Description.NO_MATCH;
    }
    String immutableClassName = MUTABLE_TO_IMMUTABLE_CLASS_NAME_MAP.get(lhsTypeQualifiedName);
    Type immutableType = state.getTypeFromString(immutableClassName);
    Tree rhsTree = tree.getInitializer();
    Type rhsType = ASTHelpers.getType(rhsTree);
    if (!ASTHelpers.isSameType(rhsType, immutableType, state)) {
        return Description.NO_MATCH;
    }
    SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
    fixBuilder.replace(getTypeTree(lhsTree), SuggestedFixes.qualifyType(state, fixBuilder, immutableType.asElement()));
    SuggestedFix fix = fixBuilder.build();
    return describeMatch(lhsTree, fix);
}
Also used : Type(com.sun.tools.javac.code.Type) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Symbol(com.sun.tools.javac.code.Symbol) VariableTree(com.sun.source.tree.VariableTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree)

Example 7 with SuggestedFix

use of com.google.errorprone.fixes.SuggestedFix in project error-prone by google.

the class ExpressionTemplate method replace.

/**
   * Generates a {@link SuggestedFix} replacing the specified match (usually of another template)
   * with this template.
   */
@Override
public Fix replace(ExpressionTemplateMatch match) {
    Inliner inliner = match.createInliner();
    Context context = inliner.getContext();
    if (annotations().containsKey(UseImportPolicy.class)) {
        ImportPolicy.bind(context, annotations().getInstance(UseImportPolicy.class).value());
    } else {
        ImportPolicy.bind(context, ImportPolicy.IMPORT_TOP_LEVEL);
    }
    int prec = getPrecedence(match.getLocation(), context);
    SuggestedFix.Builder fix = SuggestedFix.builder();
    try {
        StringWriter writer = new StringWriter();
        pretty(inliner.getContext(), writer).printExpr(expression().inline(inliner), prec);
        fix.replace(match.getLocation(), writer.toString());
    } catch (CouldNotResolveImportException e) {
        logger.log(SEVERE, "Failure to resolve in replacement", e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return addImports(inliner, fix);
}
Also used : Context(com.sun.tools.javac.util.Context) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) StringWriter(java.io.StringWriter) IOException(java.io.IOException)

Aggregations

SuggestedFix (com.google.errorprone.fixes.SuggestedFix)7 Description (com.google.errorprone.matchers.Description)2 AnnotationTree (com.sun.source.tree.AnnotationTree)2 Tree (com.sun.source.tree.Tree)2 Symbol (com.sun.tools.javac.code.Symbol)2 Objects (com.google.common.base.Objects)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 MoreCollectors (com.google.common.collect.MoreCollectors)1 Streams (com.google.common.collect.Streams)1 BugPattern (com.google.errorprone.BugPattern)1 JDK (com.google.errorprone.BugPattern.Category.JDK)1 WARNING (com.google.errorprone.BugPattern.SeverityLevel.WARNING)1 VisitorState (com.google.errorprone.VisitorState)1 ClassTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher)1 MethodTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher)1 Builder (com.google.errorprone.fixes.SuggestedFix.Builder)1 ASTHelpers (com.google.errorprone.util.ASTHelpers)1 ClassTree (com.sun.source.tree.ClassTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 IdentifierTree (com.sun.source.tree.IdentifierTree)1