本文整理汇总了Java中com.google.errorprone.fixes.SuggestedFix类的典型用法代码示例。如果您正苦于以下问题:Java SuggestedFix类的具体用法?Java SuggestedFix怎么用?Java SuggestedFix使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SuggestedFix类属于com.google.errorprone.fixes包,在下文中一共展示了SuggestedFix类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: scan
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
@Override
public Void scan(Tree tree, Context context) {
if (tree == null) {
return null;
}
JCCompilationUnit compilationUnit = context.get(JCCompilationUnit.class);
for (T beforeTemplate : rule().beforeTemplates()) {
for (M match : beforeTemplate.match((JCTree) tree, context)) {
if (rule().rejectMatchesWithComments()) {
String matchContents = match.getRange(compilationUnit);
if (matchContents.contains("//") || matchContents.contains("/*")) {
continue;
}
}
Fix fix;
if (rule().afterTemplate() == null) {
fix = SuggestedFix.delete(match.getLocation());
} else {
fix = rule().afterTemplate().replace(match);
}
listener().onDescribed(new Description(
match.getLocation(), rule().qualifiedTemplateClass(), fix, SeverityLevel.WARNING));
}
}
return super.scan(tree, context);
}
开发者ID:sivakumar-kailasam,项目名称:refactor-faster,代码行数:27,代码来源:RefasterScanner.java
示例2: twoDiffs
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
@Test
public void twoDiffs() {
DescriptionBasedDiff diff = DescriptionBasedDiff.create(compilationUnit);
diff.onDescribed(new Description(null, "message",
SuggestedFix.builder()
.replace(83, 86, "longer")
.replace(96, 99, "bar")
.build(),
SeverityLevel.NOT_A_PROBLEM));
diff.applyDifferences(sourceFile);
assertThat(sourceFile.getLines()).iteratesAs(
"package foo.bar;",
"class Foo {",
" public static void main(String[] args) {",
" System.longer.println(\"bar\");",
" }",
"}");
}
开发者ID:sivakumar-kailasam,项目名称:refactor-faster,代码行数:19,代码来源:DescriptionBasedDiffTest.java
示例3: addImport
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
@Test
public void addImport() {
DescriptionBasedDiff diff = DescriptionBasedDiff.create(compilationUnit);
diff.onDescribed(new Description(null, "message",
SuggestedFix.builder().addImport("com.google.foo.Bar").build(),
SeverityLevel.NOT_A_PROBLEM));
diff.applyDifferences(sourceFile);
assertThat(sourceFile.getLines()).iteratesAs(
"package foo.bar;",
"",
"import com.google.foo.Bar;",
"class Foo {",
" public static void main(String[] args) {",
" System.out.println(\"foo\");",
" }",
"}");
}
开发者ID:sivakumar-kailasam,项目名称:refactor-faster,代码行数:18,代码来源:DescriptionBasedDiffTest.java
示例4: twoDiffsWithImport
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
@Test
public void twoDiffsWithImport() {
DescriptionBasedDiff diff = DescriptionBasedDiff.create(compilationUnit);
diff.onDescribed(new Description(null, "message",
SuggestedFix.builder()
.replace(83, 86, "longer")
.replace(96, 99, "bar")
.addImport("com.google.foo.Bar")
.build(),
SeverityLevel.NOT_A_PROBLEM));
diff.applyDifferences(sourceFile);
assertThat(sourceFile.getLines()).iteratesAs(
"package foo.bar;",
"",
"import com.google.foo.Bar;",
"class Foo {",
" public static void main(String[] args) {",
" System.longer.println(\"bar\");",
" }",
"}");
}
开发者ID:sivakumar-kailasam,项目名称:refactor-faster,代码行数:22,代码来源:DescriptionBasedDiffTest.java
示例5: matchTry
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
@Override
public Description matchTry(TryTree tree, VisitorState state) {
if (tryTreeMatches(tree, state)) {
List<? extends StatementTree> tryStatements = tree.getBlock().getStatements();
StatementTree lastTryStatement = tryStatements.get(tryStatements.size() - 1);
String failCall = String.format("\nfail(\"Expected %s\");", exceptionToString(tree));
SuggestedFix.Builder fixBuilder =
SuggestedFix.builder().postfixWith(lastTryStatement, failCall);
// Make sure that when the fail import is added it doesn't conflict with existing ones.
fixBuilder.removeStaticImport("junit.framework.Assert.fail");
fixBuilder.removeStaticImport("junit.framework.TestCase.fail");
fixBuilder.addStaticImport("org.junit.Assert.fail");
return describeMatch(lastTryStatement, fixBuilder.build());
} else {
return Description.NO_MATCH;
}
}
开发者ID:google,项目名称:error-prone,代码行数:21,代码来源:MissingFail.java
示例6: matchBinary
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
if (!(tree.getLeftOperand() instanceof JCLiteral)) {
return Description.NO_MATCH;
}
if (!(tree.getRightOperand() instanceof JCLiteral)) {
return Description.NO_MATCH;
}
Boolean constValue = ASTHelpers.constValue(tree, Boolean.class);
if (constValue == null) {
return Description.NO_MATCH;
}
return buildDescription(tree)
.addFix(SuggestedFix.replace(tree, constValue.toString()))
.setMessage(
String.format(
"This expression always evalutes to `%s`, prefer a boolean literal for clarity.",
constValue))
.build();
}
开发者ID:google,项目名称:error-prone,代码行数:21,代码来源:ComplexBooleanConstant.java
示例7: describe
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
public Description describe(MethodInvocationTree t, VisitorState state) {
LiteralTree formatTree = (LiteralTree) t.getArguments().get(1);
String fixedFormatString = state.getSourceForNode((JCTree) formatTree).toString()
.replaceAll(BAD_PLACEHOLDER_REGEX, "%s");
SuggestedFix fix = new SuggestedFix();
if (expectedArguments(fixedFormatString) == t.getArguments().size() - 2) {
fix.replace(formatTree, fixedFormatString);
return describeMatch(formatTree, fix);
} else {
int missing = t.getArguments().size() - 2 - expectedArguments(fixedFormatString);
StringBuilder builder = new StringBuilder(fixedFormatString);
builder.deleteCharAt(builder.length() - 1);
builder.append(" [%s");
for (int i = 1; i < missing; i++) {
builder.append(", %s");
}
builder.append("]\"");
fix.replace(formatTree, builder.toString());
return describeMatch(t, fix);
}
}
开发者ID:diy1,项目名称:error-prone-aspirator,代码行数:23,代码来源:PreconditionsTooManyArgs.java
示例8: handleLocalOrParam
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
private Description handleLocalOrParam(VariableTree tree, VisitorState state, Symbol sym) {
if (sym.getModifiers().contains(Modifier.FINAL)) {
if (Source.instance(state.context).allowEffectivelyFinalInInnerClasses()) {
// In Java 8, the final modifier is never necessary on locals/parameters because
// effectively final variables can be used anywhere a final variable is required.
Optional<SuggestedFix> fix = SuggestedFixes.removeModifiers(tree, state, Modifier.FINAL);
// The fix may not be present for TWR variables that were not explicitly final
if (fix.isPresent()) {
return buildDescription(tree).setMessage(UNNECESSARY_FINAL).addFix(fix.get()).build();
}
}
return Description.NO_MATCH;
}
if (!Collections.disjoint(
sym.owner.getModifiers(), EnumSet.of(Modifier.ABSTRACT, Modifier.NATIVE))) {
// flow information isn't collected for body-less methods
return Description.NO_MATCH;
}
if ((sym.flags() & (Flags.EFFECTIVELY_FINAL | Flags.FINAL)) != 0) {
return Description.NO_MATCH;
}
return describeMatch(tree, addVarAnnotation(tree));
}
开发者ID:google,项目名称:error-prone,代码行数:24,代码来源:VarChecker.java
示例9: matchMethodInvocation
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!NEW_INSTANCE.matches(tree, state)) {
return Description.NO_MATCH;
}
SuggestedFix.Builder fix = SuggestedFix.builder();
fix.replace(
state.getEndPosition(ASTHelpers.getReceiver(tree)),
state.getEndPosition(tree),
".getDeclaredConstructor().newInstance()");
boolean fixedExceptions = fixExceptions(state, fix);
if (!fixedExceptions) {
fixThrows(state, fix);
}
return describeMatch(tree, fix.build());
}
开发者ID:google,项目名称:error-prone,代码行数:17,代码来源:ClassNewInstance.java
示例10: fixThrows
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
private void fixThrows(VisitorState state, SuggestedFix.Builder fix) {
MethodTree methodTree = state.findEnclosing(MethodTree.class);
if (methodTree == null || methodTree.getThrows().isEmpty()) {
return;
}
ImmutableMap.Builder<Type, ExpressionTree> thrown = ImmutableMap.builder();
for (ExpressionTree e : methodTree.getThrows()) {
thrown.put(ASTHelpers.getType(e), e);
}
UnhandledResult<ExpressionTree> result = unhandled(thrown.build(), state);
if (result.unhandled.isEmpty()) {
return;
}
List<String> newThrows = new ArrayList<>();
for (Type handle : result.unhandled) {
newThrows.add(handle.tsym.getSimpleName().toString());
}
Collections.sort(newThrows);
fix.postfixWith(
Iterables.getLast(methodTree.getThrows()), ", " + Joiner.on(", ").join(newThrows));
// the other exceptions are in java.lang
fix.addImport("java.lang.reflect.InvocationTargetException");
}
开发者ID:google,项目名称:error-prone,代码行数:24,代码来源:ClassNewInstance.java
示例11: matchReturn
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
@Override
public final Description matchReturn(ReturnTree tree, VisitorState state) {
if (tree.getExpression() == null || tree.getExpression().getKind() != NULL_LITERAL) {
return NO_MATCH;
}
TreePath path = state.getPath();
while (path != null && path.getLeaf() instanceof StatementTree) {
path = path.getParentPath();
}
if (path == null || !(path.getLeaf() instanceof MethodTree)) {
return NO_MATCH;
}
if (!implementsAsyncTypeMethod.matches((MethodTree) path.getLeaf(), state)) {
return NO_MATCH;
}
return describeMatch(
tree,
SuggestedFix.builder()
.replace(tree.getExpression(), "immediateFuture(null)")
.addStaticImport(Futures.class.getName() + ".immediateFuture")
.build());
}
开发者ID:google,项目名称:error-prone,代码行数:23,代码来源:AbstractAsyncTypeReturnsNull.java
示例12: matchMethodInvocation
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, final VisitorState state) {
if (!METHOD_MATCHER.matches(tree, state)) {
return Description.NO_MATCH;
}
String value = ASTHelpers.constValue(tree.getArguments().get(0), String.class);
if (value == null) {
// Value isn't a compile-time constant, so we can't know if it's unsafe.
return Description.NO_MATCH;
}
Replacement replacement = getReplacement(value, isInJodaTimeContext(state));
if (replacement.replacements.isEmpty()) {
return Description.NO_MATCH;
}
Description.Builder builder = buildDescription(tree).setMessage(replacement.message);
for (String r : replacement.replacements) {
builder.addFix(
SuggestedFix.replace(
tree.getArguments().get(0), state.getTreeMaker().Literal(r).toString()));
}
return builder.build();
}
开发者ID:google,项目名称:error-prone,代码行数:24,代码来源:ThreeLetterTimeZoneID.java
示例13: matchMethodInvocation
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!ARRAYS_AS_LIST_SINGLE_ARRAY.matches(tree, state)) {
return NO_MATCH;
}
ExpressionTree array = Iterables.getOnlyElement(tree.getArguments());
Type componentType = ((ArrayType) ASTHelpers.getType(array)).getComponentType();
if (!componentType.isPrimitive()) {
return NO_MATCH;
}
String guavaUtils = GUAVA_UTILS.get(componentType.getKind());
if (guavaUtils == null) {
return NO_MATCH;
}
Fix fix =
SuggestedFix.builder()
.addImport("com.google.common.primitives." + guavaUtils)
.replace(tree.getMethodSelect(), guavaUtils + ".asList")
.build();
return describeMatch(tree, fix);
}
开发者ID:google,项目名称:error-prone,代码行数:22,代码来源:ArraysAsListPrimitiveArray.java
示例14: matchDivZero
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private Description matchDivZero(Tree tree, ExpressionTree operand, VisitorState state) {
if (!anyOf(kindIs(Kind.DIVIDE), kindIs(Kind.DIVIDE_ASSIGNMENT)).matches(tree, state)) {
return Description.NO_MATCH;
}
if (!kindIs(Kind.INT_LITERAL).matches(operand, state)) {
return Description.NO_MATCH;
}
LiteralTree rightOperand = (LiteralTree) operand;
if (((Integer) rightOperand.getValue()) != 0) {
return Description.NO_MATCH;
}
// Find and replace enclosing Statement.
StatementTree enclosingStmt =
ASTHelpers.findEnclosingNode(state.getPath(), StatementTree.class);
SuggestedFix fix = new SuggestedFix();
if (enclosingStmt != null) {
fix = fix.replace(enclosingStmt, "throw new ArithmeticException(\"/ by zero\");");
}
return describeMatch(tree, fix);
}
开发者ID:diy1,项目名称:error-prone-aspirator,代码行数:26,代码来源:DivZero.java
示例15: fixesFromFields
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
private List<Fix> fixesFromFields(VisitorState state, final ExpressionTree receiver) {
FluentIterable<JCVariableDecl> collectionFields =
FluentIterable.from(
ASTHelpers.findEnclosingNode(state.getPath(), JCClassDecl.class).getMembers())
.filter(JCVariableDecl.class)
.filter(isCollectionVariable(state));
Multimap<Integer, JCVariableDecl> potentialReplacements =
partitionByEditDistance(simpleNameOfIdentifierOrMemberAccess(receiver), collectionFields);
return buildValidReplacements(
potentialReplacements,
new Function<JCVariableDecl, Fix>() {
@Override
public Fix apply(JCVariableDecl var) {
return SuggestedFix.replace(receiver, "this." + var.sym.toString());
}
});
}
开发者ID:google,项目名称:error-prone,代码行数:20,代码来源:ModifyingCollectionWithItself.java
示例16: fixesFromMethodParameters
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
private List<Fix> fixesFromMethodParameters(VisitorState state, final ExpressionTree argument) {
// find a method parameter of the same type and similar name and suggest it
// as the new argument
assert (argument.getKind() == IDENTIFIER || argument.getKind() == MEMBER_SELECT);
FluentIterable<JCVariableDecl> collectionParams =
FluentIterable.from(
ASTHelpers.findEnclosingNode(state.getPath(), JCMethodDecl.class).getParameters())
.filter(isCollectionVariable(state));
Multimap<Integer, JCVariableDecl> potentialReplacements =
partitionByEditDistance(simpleNameOfIdentifierOrMemberAccess(argument), collectionParams);
return buildValidReplacements(
potentialReplacements,
new Function<JCVariableDecl, Fix>() {
@Override
public Fix apply(JCVariableDecl var) {
return SuggestedFix.replace(argument, var.sym.toString());
}
});
}
开发者ID:google,项目名称:error-prone,代码行数:24,代码来源:ModifyingCollectionWithItself.java
示例17: prefixDiff
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
@Test
public void prefixDiff() {
DescriptionBasedDiff diff = createDescriptionBasedDiff();
diff.onDescribed(
new Description(
null, "message", SuggestedFix.replace(140, 140, "bar"), SeverityLevel.SUGGESTION));
diff.applyDifferences(sourceFile);
assertThat(sourceFile.getLines())
.containsExactly(
"package foo.bar;",
"import org.bar.Baz;",
"import com.foo.Bar;",
"",
"class Foo {",
" public static void main(String[] args) {",
" System.out.println(\"foobar\");",
" }",
"}")
.inOrder();
}
开发者ID:google,项目名称:error-prone,代码行数:21,代码来源:DescriptionBasedDiffTest.java
示例18: getQualifiedName
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
private static String getQualifiedName(VisitorState state, SuggestedFix.Builder builder) {
// TODO(cpovirk): Suggest @NullableDecl if the code uses that.
Symbol sym = FindIdentifiers.findIdent("Nullable", state, KindSelector.VAL_TYP);
String defaultType =
state.isAndroidCompatible()
? "android.support.annotation.Nullable"
: "javax.annotation.Nullable";
if (sym != null) {
ClassSymbol classSym = (ClassSymbol) sym;
if (classSym.isAnnotationType()) {
// We've got an existing annotation called Nullable. We can use this.
return "Nullable";
} else {
// It's not an annotation type. We have to fully-qualify the import.
return defaultType;
}
}
// There is no symbol already. Import and use.
builder.addImport(defaultType);
return "Nullable";
}
开发者ID:google,项目名称:error-prone,代码行数:22,代码来源:NullnessFixes.java
示例19: matchNewClassOrMethodInvocation
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
private Description matchNewClassOrMethodInvocation(
MethodSymbol symbol, ImmutableList<Commented<ExpressionTree>> arguments, Tree tree) {
if (symbol.getParameters().isEmpty()) {
return NO_MATCH;
}
SuggestedFix.Builder fix = SuggestedFix.builder();
forEachPair(
arguments.stream(),
Stream.concat(
symbol.getParameters().stream(),
Stream.iterate(getLast(symbol.getParameters()), x -> x)),
(commented, param) ->
commented
.afterComments()
.stream()
.filter(c -> matchingParamComment(c, param))
.findFirst()
.ifPresent(c -> fixParamComment(fix, commented, param, c)));
return fix.isEmpty() ? NO_MATCH : describeMatch(tree, fix.build());
}
开发者ID:google,项目名称:error-prone,代码行数:21,代码来源:ParameterComment.java
示例20: matchNewClass
import com.google.errorprone.fixes.SuggestedFix; //导入依赖的package包/类
@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
if (ASTHelpers.isSameType(
state.getSymtab().stringBuilderType, ASTHelpers.getType(tree.getIdentifier()), state)
&& tree.getArguments().size() == 1) {
ExpressionTree argument = tree.getArguments().get(0);
Type type = ((JCTree) argument).type;
if (type.getKind() == TypeKind.CHAR) {
if (argument.getKind() == Kind.CHAR_LITERAL) {
char ch = (Character) ((LiteralTree) argument).getValue();
return describeMatch(
tree,
SuggestedFix.replace(argument, "\"" + Convert.quote(Character.toString(ch)) + "\""));
} else {
return describeMatch(
tree,
SuggestedFix.replace(
tree,
"new StringBuilder().append(" + state.getSourceForNode((JCTree) argument) + ")"));
}
}
}
return Description.NO_MATCH;
}
开发者ID:google,项目名称:error-prone,代码行数:25,代码来源:StringBuilderInitWithChar.java
注:本文中的com.google.errorprone.fixes.SuggestedFix类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论