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);
}
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);
}
Aggregations