本文整理汇总了Java中com.google.errorprone.matchers.Description.Builder方法的典型用法代码示例。如果您正苦于以下问题:Java Description.Builder方法的具体用法?Java Description.Builder怎么用?Java Description.Builder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.errorprone.matchers.Description
的用法示例。
在下文中一共展示了Description.Builder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: changeReturnNullabilityFix
import com.google.errorprone.matchers.Description; //导入方法依赖的package包/类
private Description.Builder changeReturnNullabilityFix(
Tree suggestTree, Description.Builder builder) {
if (suggestTree.getKind() != Tree.Kind.METHOD) {
throw new RuntimeException("This should be a MethodTree");
}
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
MethodTree methodTree = (MethodTree) suggestTree;
int countNullableAnnotations = 0;
for (AnnotationTree annotationTree : methodTree.getModifiers().getAnnotations()) {
if (annotationTree.getAnnotationType().toString().endsWith("Nullable")) {
fixBuilder.delete(annotationTree);
countNullableAnnotations += 1;
}
}
assert countNullableAnnotations > 1;
return builder.addFix(fixBuilder.build());
}
示例2: matchMethodInvocation
import com.google.errorprone.matchers.Description; //导入方法依赖的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();
}
示例3: handleFileWriter
import com.google.errorprone.matchers.Description; //导入方法依赖的package包/类
private Description handleFileWriter(NewClassTree tree, VisitorState state) {
Iterator<? extends ExpressionTree> it = tree.getArguments().iterator();
Tree fileArg = it.next();
Tree appendMode = it.hasNext() ? it.next() : null;
Tree parent = state.getPath().getParentPath().getLeaf();
Tree toReplace = BUFFERED_WRITER.matches(parent, state) ? parent : tree;
Description.Builder description = buildDescription(tree);
boolean useGuava = shouldUseGuava(state);
for (CharsetFix charset : CharsetFix.values()) {
if (appendMode == null && useGuava) {
description.addFix(guavaFileWriterFix(state, fileArg, toReplace, charset));
} else {
description.addFix(
nioFileWriterFix(state, appendMode, fileArg, toReplace, charset, useGuava));
}
}
return description.build();
}
示例4: matchMethodInvocation
import com.google.errorprone.matchers.Description; //导入方法依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (ASSERTION.matches(state.getPath().getParentPath().getLeaf(), state)) {
return NO_MATCH;
}
List<? extends ExpressionTree> args = tree.getArguments();
ExpressionTree toReplace;
if (INSTANCE_MATCHER.matches(tree, state)) {
toReplace = args.get(0);
} else if (STATIC_MATCHER.matches(tree, state)) {
if (args.get(0).getKind() == Kind.IDENTIFIER && args.get(1).getKind() != Kind.IDENTIFIER) {
toReplace = args.get(0);
} else {
toReplace = args.get(1);
}
} else {
return NO_MATCH;
}
Description.Builder description = buildDescription(tree);
Fix fix = fieldFix(toReplace, state);
if (fix != null) {
description.addFix(fix);
}
return description.build();
}
示例5: createErrorDescription
import com.google.errorprone.matchers.Description; //导入方法依赖的package包/类
/**
* create an error description for a nullability warning
*
* @param errorType the type of error encountered.
* @param errorLocTree the location of the error
* @param message the error message
* @param suggestTree the location at which a fix suggestion should be made
* @return the error description
*/
private Description createErrorDescription(
MessageTypes errorType, Tree errorLocTree, String message, @Nullable Tree suggestTree) {
Description.Builder builder = buildDescription(errorLocTree).setMessage(message);
if (config.suggestSuppressions() && suggestTree != null) {
switch (errorType) {
case DEREFERENCE_NULLABLE:
case RETURN_NULLABLE:
case PASS_NULLABLE:
case ASSIGN_FIELD_NULLABLE:
if (config.getCastToNonNullMethod() != null) {
builder = addCastToNonNullFix(suggestTree, builder);
} else {
builder = addSuppressWarningsFix(suggestTree, builder, canonicalName());
}
break;
case WRONG_OVERRIDE_RETURN:
builder = changeReturnNullabilityFix(suggestTree, builder);
break;
case WRONG_OVERRIDE_PARAM:
builder = changeParamNullabilityFix(suggestTree, builder);
break;
case METHOD_NO_INIT:
case FIELD_NO_INIT:
builder = addSuppressWarningsFix(suggestTree, builder, INITIALIZATION_CHECK_NAME);
break;
default:
builder = addSuppressWarningsFix(suggestTree, builder, canonicalName());
}
}
// #letbuildersbuild
return builder.build();
}
示例6: addCastToNonNullFix
import com.google.errorprone.matchers.Description; //导入方法依赖的package包/类
private Description.Builder addCastToNonNullFix(Tree suggestTree, Description.Builder builder) {
String fullMethodName = config.getCastToNonNullMethod();
assert fullMethodName != null;
// Add a call to castToNonNull around suggestTree:
String[] parts = fullMethodName.split("\\.");
String shortMethodName = parts[parts.length - 1];
String replacement = shortMethodName + "(" + suggestTree.toString() + ")";
SuggestedFix fix =
SuggestedFix.builder()
.replace(suggestTree, replacement)
.addStaticImport(fullMethodName) // ensure castToNonNull static import
.build();
return builder.addFix(fix);
}
示例7: addSuppressWarningsFix
import com.google.errorprone.matchers.Description; //导入方法依赖的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);
}
示例8: buildDescriptionFromChecker
import com.google.errorprone.matchers.Description; //导入方法依赖的package包/类
/**
* Returns a new builder for {@link Description}s.
*
* @param node the node where the error is
* @param checker the {@code BugChecker} instance that is producing this {@code Description}
*/
@CheckReturnValue
public static Description.Builder buildDescriptionFromChecker(Tree node, BugChecker checker) {
return Description.builder(
Preconditions.checkNotNull(node),
checker.canonicalName(),
checker.linkUrl(),
checker.defaultSeverity(),
checker.message());
}
示例9: handlePrintWriterOutputStream
import com.google.errorprone.matchers.Description; //导入方法依赖的package包/类
private Description handlePrintWriterOutputStream(NewClassTree tree, VisitorState state) {
Tree outputStream = tree.getArguments().get(0);
Description.Builder description = buildDescription(tree);
for (CharsetFix charsetFix : CharsetFix.values()) {
SuggestedFix.Builder fix =
SuggestedFix.builder()
.prefixWith(outputStream, "new BufferedWriter(new OutputStreamWriter(")
.postfixWith(outputStream, String.format(", %s))", charsetFix.replacement()));
charsetFix.addImport(fix, state);
fix.addImport("java.io.BufferedWriter");
fix.addImport("java.io.OutputStreamWriter");
description.addFix(fix.build());
}
return description.build();
}
示例10: appendCharsets
import com.google.errorprone.matchers.Description; //导入方法依赖的package包/类
private void appendCharsets(
Description.Builder description,
Tree tree,
Tree select,
List<? extends ExpressionTree> arguments,
VisitorState state) {
description.addFix(appendCharset(tree, select, arguments, state, CharsetFix.UTF_8_FIX));
description.addFix(
appendCharset(tree, select, arguments, state, CharsetFix.DEFAULT_CHARSET_FIX));
}
示例11: describeAnonymous
import com.google.errorprone.matchers.Description; //导入方法依赖的package包/类
private Description.Builder describeAnonymous(Tree tree, Type superType, Violation info) {
String message =
String.format(
"Class extends @Immutable type %s, but is not immutable: %s",
superType, info.message());
return buildDescription(tree).setMessage(message);
}
示例12: matchNewClass
import com.google.errorprone.matchers.Description; //导入方法依赖的package包/类
@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
if (state.isAndroidCompatible()) {
return NO_MATCH;
}
if (CTOR.matches(tree, state)) {
Description.Builder description = buildDescription(tree);
appendCharsets(description, tree, tree.getIdentifier(), tree.getArguments(), state);
return description.build();
}
if (FILE_READER.matches(tree, state)) {
return handleFileReader(tree, state);
}
if (FILE_WRITER.matches(tree, state)) {
return handleFileWriter(tree, state);
}
if (PRINT_WRITER.matches(tree, state)) {
return handlePrintWriter(tree, state);
}
if (PRINT_WRITER_OUTPUTSTREAM.matches(tree, state)) {
return handlePrintWriterOutputStream(tree, state);
}
if (SCANNER_MATCHER.matches(tree, state)) {
return handleScanner(tree, state);
}
return NO_MATCH;
}
示例13: matchVariable
import com.google.errorprone.matchers.Description; //导入方法依赖的package包/类
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
Type type = ASTHelpers.getType(tree);
VarSymbol symbol = ASTHelpers.getSymbol(tree);
if (type == null || symbol == null) {
return NO_MATCH;
}
if (symbol.getKind() != ElementKind.PARAMETER) {
return NO_MATCH;
}
if (!isSameType(type, state.getSymtab().iterableType, state)) {
return NO_MATCH;
}
if (type.getTypeArguments().isEmpty()) {
return NO_MATCH;
}
if (!isSameType(
wildBound(getOnlyElement(type.getTypeArguments())),
state.getTypeFromString(Path.class.getName()),
state)) {
return NO_MATCH;
}
Description.Builder description = buildDescription(tree);
if (tree.getType() instanceof ParameterizedTypeTree) {
description.addFix(
SuggestedFix.builder()
.addImport("java.util.Collection")
.replace(((ParameterizedTypeTree) tree.getType()).getType(), "Collection")
.build());
}
return description.build();
}
示例14: describeClass
import com.google.errorprone.matchers.Description; //导入方法依赖的package包/类
private Description.Builder describeClass(
Tree tree, ClassSymbol sym, AnnotationInfo annotation, Violation info) {
String message;
if (sym.getQualifiedName().contentEquals(annotation.typeName())) {
message = "type annotated with @Immutable could not be proven immutable: " + info.message();
} else {
message =
String.format(
"Class extends @Immutable type %s, but is not immutable: %s",
annotation.typeName(), info.message());
}
return buildDescription(tree).setMessage(message);
}
示例15: changeParamNullabilityFix
import com.google.errorprone.matchers.Description; //导入方法依赖的package包/类
private Description.Builder changeParamNullabilityFix(
Tree suggestTree, Description.Builder builder) {
return builder.addFix(SuggestedFix.prefixWith(suggestTree, "@Nullable "));
}