本文整理汇总了Java中com.google.errorprone.fixes.SuggestedFix.prefixWith方法的典型用法代码示例。如果您正苦于以下问题:Java SuggestedFix.prefixWith方法的具体用法?Java SuggestedFix.prefixWith怎么用?Java SuggestedFix.prefixWith使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.errorprone.fixes.SuggestedFix
的用法示例。
在下文中一共展示了SuggestedFix.prefixWith方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: matchNewClass
import com.google.errorprone.fixes.SuggestedFix; //导入方法依赖的package包/类
@Override
public Description matchNewClass(NewClassTree newClassTree, VisitorState state) {
if (!MATCHER.matches(newClassTree, state)) {
return Description.NO_MATCH;
}
StatementTree parent = (StatementTree) state.getPath().getParentPath().getLeaf();
boolean isLastStatement =
anyOf(
new ChildOfBlockOrCase<>(
ChildMultiMatcher.MatchType.LAST, Matchers.<StatementTree>isSame(parent)),
// it could also be a bare if statement with no braces
parentNode(parentNode(kindIs(IF))))
.matches(newClassTree, state);
Fix fix;
if (isLastStatement) {
fix = SuggestedFix.prefixWith(newClassTree, "throw ");
} else {
fix = SuggestedFix.delete(parent);
}
return describeMatch(newClassTree, fix);
}
示例2: matchBinary
import com.google.errorprone.fixes.SuggestedFix; //导入方法依赖的package包/类
@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
if (!BINARY_TREE_MATCHER.matches(tree, state)) {
return Description.NO_MATCH;
}
/*
* For shift amounts in [32, 63], cast the left operand to long. Otherwise change the shift
* amount to whatever would actually be used.
*/
int intValue = ((Number) ((LiteralTree) tree.getRightOperand()).getValue()).intValue();
Fix fix;
if (intValue >= 32 && intValue <= 63) {
if (tree.getLeftOperand().getKind() == Kind.INT_LITERAL) {
fix = SuggestedFix.postfixWith(tree.getLeftOperand(), "L");
} else {
fix = SuggestedFix.prefixWith(tree, "(long) ");
}
} else {
// This is the equivalent shift distance according to JLS 15.19.
String actualShiftDistance = Integer.toString(intValue & 0x1f);
fix = SuggestedFix.replace(tree.getRightOperand(), actualShiftDistance);
}
return describeMatch(tree, fix);
}
示例3: matchNewClass
import com.google.errorprone.fixes.SuggestedFix; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public Description matchNewClass(NewClassTree newClassTree, VisitorState state) {
if (!MATCHER.matches(newClassTree, state)) {
return Description.NO_MATCH;
}
StatementTree parent = (StatementTree) state.getPath().getParentPath().getLeaf();
boolean isLastStatement = anyOf(
new Enclosing.BlockOrCase(lastStatement(Matchers.<StatementTree>isSame(parent))),
// it could also be a bare if statement with no braces
parentNode(parentNode(kindIs(IF))))
.matches(newClassTree, state);
SuggestedFix suggestedFix = new SuggestedFix();
if (isLastStatement) {
suggestedFix.prefixWith(newClassTree, "throw ");
} else {
suggestedFix.delete(parent);
}
return describeMatch(newClassTree, suggestedFix);
}
示例4: addSuppressWarningsFix
import com.google.errorprone.fixes.SuggestedFix; //导入方法依赖的package包/类
private Description.Builder addSuppressWarningsFix(
Tree suggestTree, Description.Builder builder, String checkerName) {
SuppressWarnings extantSuppressWarnings =
ASTHelpers.getAnnotation(suggestTree, SuppressWarnings.class);
SuggestedFix fix;
if (extantSuppressWarnings == null) {
fix = SuggestedFix.prefixWith(suggestTree, "@SuppressWarnings(\"" + checkerName + "\") ");
} else {
// need to update the existing list of warnings
List<String> suppressions = Lists.newArrayList(extantSuppressWarnings.value());
suppressions.add(checkerName);
// find the existing annotation, so we can replace it
ModifiersTree modifiers =
(suggestTree instanceof MethodTree)
? ((MethodTree) suggestTree).getModifiers()
: ((VariableTree) suggestTree).getModifiers();
List<? extends AnnotationTree> annotations = modifiers.getAnnotations();
// noinspection ConstantConditions
com.google.common.base.Optional<? extends AnnotationTree> suppressWarningsAnnot =
Iterables.tryFind(
annotations,
annot -> annot.getAnnotationType().toString().endsWith("SuppressWarnings"));
if (!suppressWarningsAnnot.isPresent()) {
throw new AssertionError("something went horribly wrong");
}
String replacement =
"@SuppressWarnings({"
+ Joiner.on(',').join(Iterables.transform(suppressions, s -> '"' + s + '"'))
+ "}) ";
fix = SuggestedFix.replace(suppressWarningsAnnot.get(), replacement);
}
return builder.addFix(fix);
}
示例5: fieldFix
import com.google.errorprone.fixes.SuggestedFix; //导入方法依赖的package包/类
@Nullable
protected static Fix fieldFix(Tree toReplace, VisitorState state) {
TreePath path = state.getPath();
while (path != null
&& path.getLeaf().getKind() != Kind.CLASS
&& path.getLeaf().getKind() != Kind.BLOCK) {
path = path.getParentPath();
}
if (path == null) {
return null;
}
List<? extends JCTree> members;
// Must be block or class
if (path.getLeaf().getKind() == Kind.CLASS) {
members = ((JCClassDecl) path.getLeaf()).getMembers();
} else {
members = ((JCBlock) path.getLeaf()).getStatements();
}
for (JCTree jcTree : members) {
if (jcTree.getKind() == Kind.VARIABLE) {
JCVariableDecl declaration = (JCVariableDecl) jcTree;
TypeSymbol variableTypeSymbol =
state.getTypes().erasure(ASTHelpers.getType(declaration)).tsym;
if (ASTHelpers.getSymbol(toReplace).isMemberOf(variableTypeSymbol, state.getTypes())) {
if (toReplace.getKind() == Kind.IDENTIFIER) {
return SuggestedFix.prefixWith(toReplace, declaration.getName() + ".");
} else {
return SuggestedFix.replace(
((JCFieldAccess) toReplace).getExpression(), declaration.getName().toString());
}
}
}
}
return null;
}
示例6: describe
import com.google.errorprone.fixes.SuggestedFix; //导入方法依赖的package包/类
/**
* Fixes the error by assigning the result of the call to the receiver reference, or deleting the
* method call.
*/
public Description describe(MethodInvocationTree methodInvocationTree, VisitorState state) {
// Find the root of the field access chain, i.e. a.intern().trim() ==> a.
ExpressionTree identifierExpr = ASTHelpers.getRootAssignable(methodInvocationTree);
String identifierStr = null;
Type identifierType = null;
if (identifierExpr != null) {
identifierStr = identifierExpr.toString();
if (identifierExpr instanceof JCIdent) {
identifierType = ((JCIdent) identifierExpr).sym.type;
} else if (identifierExpr instanceof JCFieldAccess) {
identifierType = ((JCFieldAccess) identifierExpr).sym.type;
} else {
throw new IllegalStateException("Expected a JCIdent or a JCFieldAccess");
}
}
Type returnType =
ASTHelpers.getReturnType(((JCMethodInvocation) methodInvocationTree).getMethodSelect());
Fix fix;
if (identifierStr != null
&& !"this".equals(identifierStr)
&& returnType != null
&& state.getTypes().isAssignable(returnType, identifierType)) {
// Fix by assigning the assigning the result of the call to the root receiver reference.
fix = SuggestedFix.prefixWith(methodInvocationTree, identifierStr + " = ");
} else {
// Unclear what the programmer intended. Delete since we don't know what else to do.
Tree parent = state.getPath().getParentPath().getLeaf();
fix = SuggestedFix.delete(parent);
}
return describeMatch(methodInvocationTree, fix);
}
示例7: matchBinary
import com.google.errorprone.fixes.SuggestedFix; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
if (!BINARY_TREE_MATCHER.matches(tree, state)) {
return Description.NO_MATCH;
}
/*
* For shift amounts in [32, 63], cast the left operand to long. Otherwise change the shift
* amount to whatever would actually be used.
*/
int intValue = ((Number) ((LiteralTree) tree.getRightOperand()).getValue()).intValue();
SuggestedFix fix = new SuggestedFix();
if (intValue >= 32 && intValue <= 63) {
if (tree.getLeftOperand().getKind() == Kind.INT_LITERAL) {
fix = fix.postfixWith(tree.getLeftOperand(), "L");
} else {
fix = fix.prefixWith(tree, "(long) ");
}
} else {
JCLiteral jcLiteral = (JCLiteral) tree.getRightOperand();
// This is the equivalent shift distance according to JLS 15.19.
String actualShiftDistance = Integer.toString(intValue & 0x1f);
int actualStart = ASTHelpers.getActualStartPosition(jcLiteral, state.getSourceCode());
if (actualStart != jcLiteral.getStartPosition()) {
fix = fix.replace(tree.getRightOperand(), actualShiftDistance,
actualStart - jcLiteral.getStartPosition(), 0);
} else {
fix = fix.replace(tree.getRightOperand(), actualShiftDistance);
}
}
return describeMatch(tree, fix);
}
示例8: matchClass
import com.google.errorprone.fixes.SuggestedFix; //导入方法依赖的package包/类
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
if (!classTreeMatcher.matches(tree, state)) {
return Description.NO_MATCH;
}
// figure out where to insert the static modifier
// if there is other modifier, prepend 'static ' in front of class
// else insert 'static ' AFTER public/private/protected and BEFORE final
SuggestedFix fix = new SuggestedFix();
ModifiersTree mods = tree.getModifiers();
if (mods.getFlags().isEmpty()) {
fix.prefixWith(tree, "static ");
} else {
// Note that the use of .toString() here effectively destroys any special
// formatting, eg if the modifiers previously had multiple spaces or a
// comment between them, after this fix they will all have exactly one
// space between each modifier.
String newmods = mods.toString();
int ind = newmods.indexOf("final");
if (ind < 0) {
// append if 'final' not found
newmods += "static";
fix.replace(mods, newmods);
} else {
// insert at ind, just before 'final'
newmods = newmods.substring(0, ind) + "static "
+ newmods.substring(ind, newmods.length() - 1);
fix.replace(mods, newmods);
}
fix.replace(mods, newmods);
}
return describeMatch(tree, fix);
}
示例9: matchMethodInvocation
import com.google.errorprone.fixes.SuggestedFix; //导入方法依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!waitMatcher.matches(tree, state)) {
return Description.NO_MATCH;
}
if (!SUPPLY_FIX) {
return describeMatch(tree, Fix.NO_FIX);
}
SuggestedFix fix = new SuggestedFix();
// if -> while case
JCIf enclosingIf =
ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), JCIf.class);
if (enclosingIf != null && enclosingIf.getElseStatement() == null) {
// Assume first 2 characters of the IfTree are "if", replace with while.
fix.replace(enclosingIf.getStartPosition(), enclosingIf.getStartPosition() + 2, "while");
return describeMatch(tree, fix);
}
// loop outside synchronized block -> move synchronized outside
@SuppressWarnings("unchecked")
List<Class<? extends StatementTree>> loopClasses = Arrays.asList(WhileLoopTree.class, ForLoopTree.class,
EnhancedForLoopTree.class, DoWhileLoopTree.class);
StatementTree enclosingLoop = null;
for (Class<? extends StatementTree> loopClass : loopClasses) {
enclosingLoop = ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), loopClass);
if (enclosingLoop != null) {
break;
}
}
if (enclosingLoop != null) {
SynchronizedTree enclosingSynchronized = ASTHelpers.findEnclosingNode(
state.getPath().getParentPath(), SynchronizedTree.class);
if (enclosingSynchronized != null) {
String blockStatements = enclosingSynchronized.getBlock().toString();
int openBracketIndex = blockStatements.indexOf('{');
int closeBracketIndex = blockStatements.lastIndexOf('}');
blockStatements = blockStatements.substring(openBracketIndex + 1, closeBracketIndex).trim();
fix.replace(enclosingSynchronized, blockStatements);
fix.prefixWith(enclosingLoop, "synchronized " + enclosingSynchronized.getExpression() + " {\n");
fix.postfixWith(enclosingLoop, "\n}");
return describeMatch(tree, fix);
}
}
// Intent is to wait forever -> wrap in while (true)
// Heuristic: this is the last statement in a method called main, inside a synchronized block.
/*
if (enclosingIf == null
&& (ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), WhileLoopTree.class) == null)
&& (ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), ForLoopTree.class) == null)
&& (ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), EnhancedForLoopTree.class) == null)
&& (ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), DoWhileLoopTree.class) == null)) {
TreeMaker treeMaker = TreeMaker.instance(state.context);
JCLiteral trueLiteral = treeMaker.Literal(true);
treeMaker.WhileLoop(trueLiteral,
}
*/
return describeMatch(tree, fix);
}
示例10: matchMethod
import com.google.errorprone.fixes.SuggestedFix; //导入方法依赖的package包/类
/**
* Matches any method definitions that fit the following:
* 1) Defined method is named "equals."
* 2) Defined method returns a boolean.
* 3) Defined method takes a single parameter of the same type as the enclosing class.
* 4) The enclosing class does not have a method defined that really overrides Object.equals().
*/
@Override
@SuppressWarnings("unchecked") // matchers + varargs cause this
public Description matchMethod(MethodTree methodTree, VisitorState state) {
if (!MATCHER.matches(methodTree, state)) {
return Description.NO_MATCH;
}
SuggestedFix fix = new SuggestedFix();
JCClassDecl cls = (JCClassDecl) state.findEnclosing(ClassTree.class);
if ((cls.getModifiers().flags & ENUM) != 0) {
/* If the enclosing class is an enum, then just delete the equals method since enums
* should always be compared for reference equality. Enum defines a final equals method for
* just this reason. */
fix.delete(methodTree);
} else {
/* Otherwise, change the covariant equals method to override Object.equals. */
JCTree parameterType = (JCTree) methodTree.getParameters().get(0).getType();
Name parameterName = ((JCVariableDecl) methodTree.getParameters().get(0)).getName();
// Add @Override annotation if not present.
boolean hasOverrideAnnotation = false;
List<JCAnnotation> annotations = ((JCMethodDecl) methodTree).getModifiers().getAnnotations();
for (JCAnnotation annotation : annotations) {
if (annotation.annotationType.type.tsym == state.getSymtab().overrideType.tsym) {
hasOverrideAnnotation = true;
}
}
if (!hasOverrideAnnotation) {
fix.prefixWith(methodTree, "@Override\n");
}
// Change method signature, substituting Object for parameter type.
fix.replace(parameterType, "Object");
// If there is a method body...
if (methodTree.getBody() != null) {
// Add type check at start
String typeCheckStmt = "if (!(" + parameterName + " instanceof " + parameterType + ")) {\n"
+ " return false;\n"
+ "}\n";
fix.prefixWith(methodTree.getBody().getStatements().get(0), typeCheckStmt);
// Cast all uses of the parameter name using a recursive TreeScanner.
new CastScanner().scan(methodTree.getBody(), new CastState(parameterName,
parameterType.toString(), fix));
}
}
return describeMatch(methodTree, fix);
}