本文整理汇总了Java中com.android.tools.lint.detector.api.LintFix类的典型用法代码示例。如果您正苦于以下问题:Java LintFix类的具体用法?Java LintFix怎么用?Java LintFix使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LintFix类属于com.android.tools.lint.detector.api包,在下文中一共展示了LintFix类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkNestedStringFormat
import com.android.tools.lint.detector.api.LintFix; //导入依赖的package包/类
private static void checkNestedStringFormat(JavaContext context, UCallExpression call) {
UElement current = call;
while (true) {
current = LintUtils.skipParentheses(current.getUastParent());
if (current == null || current instanceof UMethod) {
// Reached AST root or code block node; String.format not inside Timber.X(..).
return;
}
if (current instanceof UCallExpression) {
UCallExpression maybeTimberLogCall = (UCallExpression) current;
JavaEvaluator evaluator = context.getEvaluator();
PsiMethod psiMethod = maybeTimberLogCall.resolve();
if (Pattern.matches(TIMBER_TREE_LOG_METHOD_REGEXP, psiMethod.getName())
&& evaluator.isMemberInClass(psiMethod, "timber.log.Timber")) {
LintFix fix = quickFixIssueFormat(call);
context.report(ISSUE_FORMAT, call, context.getLocation(call),
"Using 'String#format' inside of 'Timber'", fix);
return;
}
}
}
}
示例2: checkElement
import com.android.tools.lint.detector.api.LintFix; //导入依赖的package包/类
private static boolean checkElement(JavaContext context, UCallExpression call, UElement element) {
if (element instanceof UBinaryExpression) {
UBinaryExpression binaryExpression = (UBinaryExpression) element;
UastBinaryOperator operator = binaryExpression.getOperator();
if (operator == PLUS || operator == PLUS_ASSIGN) {
Class argumentType = getType(binaryExpression);
if (argumentType == String.class) {
LintFix fix = quickFixIssueBinary(binaryExpression);
context.report(ISSUE_BINARY, call, context.getLocation(element),
"Replace String concatenation with Timber's string formatting", fix);
return true;
}
}
} else if (element instanceof UIfExpression) {
return checkConditionalUsage(context, call, element);
}
return false;
}
示例3: quickFixIssueBinary
import com.android.tools.lint.detector.api.LintFix; //导入依赖的package包/类
private static LintFix quickFixIssueBinary(UBinaryExpression binaryExpression) {
UExpression leftOperand = binaryExpression.getLeftOperand();
UExpression rightOperand = binaryExpression.getRightOperand();
boolean isLeftLiteral = isStringLiteral(leftOperand);
boolean isRightLiteral = isStringLiteral(rightOperand);
// "a" + "b" => "ab"
if (isLeftLiteral && isRightLiteral) {
return fix().replace() //
.text(binaryExpression.asSourceString())
.with("\"" + evaluateString(binaryExpression) + "\"")
.build();
}
String args;
if (isLeftLiteral) {
args = "\"" + evaluateString(leftOperand) + "%s\", " + rightOperand.asSourceString();
} else if (isRightLiteral) {
args = "\"%s" + evaluateString(rightOperand) + "\", " + leftOperand.asSourceString();
} else {
args = "\"%s%s\", " + leftOperand.asSourceString() + ", " + rightOperand.asSourceString();
}
return fix().replace().text(binaryExpression.asSourceString()).with(args).build();
}
示例4: checkNestedStringFormat
import com.android.tools.lint.detector.api.LintFix; //导入依赖的package包/类
private static void checkNestedStringFormat(JavaContext context, UCallExpression call) {
UElement current = call;
while (true) {
current = LintUtils.skipParentheses(current.getUastParent());
if (current == null || current instanceof UMethod) {
// Reached AST root or code block node; String.format not inside Timber.X(..).
return;
}
if (UastExpressionUtils.isMethodCall(current)) {
UCallExpression maybeTimberLogCall = (UCallExpression) current;
JavaEvaluator evaluator = context.getEvaluator();
PsiMethod psiMethod = maybeTimberLogCall.resolve();
if (Pattern.matches(TIMBER_TREE_LOG_METHOD_REGEXP, psiMethod.getName())
&& evaluator.isMemberInClass(psiMethod, "timber.log.Timber")) {
LintFix fix = quickFixIssueFormat(call);
context.report(ISSUE_FORMAT, call, context.getLocation(call),
"Using 'String#format' inside of 'Timber'", fix);
return;
}
}
}
}
示例5: visitMethod
import com.android.tools.lint.detector.api.LintFix; //导入依赖的package包/类
@Override public void visitMethod(JavaContext context, UCallExpression call, PsiMethod method) {
String methodName = call.getMethodName();
JavaEvaluator evaluator = context.getEvaluator();
if ("format".equals(methodName) && evaluator.isMemberInClass(method, "java.lang.String")) {
checkNestedStringFormat(context, call);
return;
}
// As of API 24, Log tags are no longer limited to 23 chars.
if ("tag".equals(methodName)
&& evaluator.isMemberInClass(method, "timber.log.Timber")
&& context.getMainProject().getMinSdk() <= 23) {
checkTagLength(context, call);
return;
}
if (evaluator.isMemberInClass(method, "android.util.Log")) {
LintFix fix = quickFixIssueLog(call);
context.report(ISSUE_LOG, call, context.getLocation(call), "Using 'Log' instead of 'Timber'",
fix);
return;
}
// Handles Timber.X(..) and Timber.tag(..).X(..) where X in (v|d|i|w|e|wtf).
if (evaluator.isMemberInClass(method, "timber.log.Timber") //
|| evaluator.isMemberInClass(method, "timber.log.Timber.Tree")) {
checkMethodArguments(context, call);
checkFormatArguments(context, call);
checkExceptionLogging(context, call);
}
}
示例6: checkTagLength
import com.android.tools.lint.detector.api.LintFix; //导入依赖的package包/类
private static void checkTagLength(JavaContext context, UCallExpression call) {
List<UExpression> arguments = call.getValueArguments();
UExpression argument = arguments.get(0);
String tag = evaluateString(context, argument, true);
if (tag != null && tag.length() > 23) {
String message =
String.format("The logging tag can be at most 23 characters, was %1$d (%2$s)",
tag.length(), tag);
LintFix fix = quickFixIssueTagLength(argument, tag);
context.report(ISSUE_TAG_LENGTH, argument, context.getLocation(argument), message, fix);
}
}
示例7: checkMethodArguments
import com.android.tools.lint.detector.api.LintFix; //导入依赖的package包/类
private static void checkMethodArguments(JavaContext context, UCallExpression call) {
List<UExpression> arguments = call.getValueArguments();
int numArguments = arguments.size();
for (int i = 0; i < numArguments; i++) {
UExpression argument = arguments.get(i);
if (checkElement(context, call, argument)) {
break;
}
if (i > 0 && isSubclassOf(context, argument, Throwable.class)) {
LintFix fix = quickFixIssueThrowable(call, arguments, argument);
context.report(ISSUE_THROWABLE, call, context.getLocation(call),
"Throwable should be first argument", fix);
}
}
}
示例8: quickFixIssueLog
import com.android.tools.lint.detector.api.LintFix; //导入依赖的package包/类
private static LintFix quickFixIssueLog(UCallExpression logCall) {
List<UExpression> arguments = logCall.getValueArguments();
String methodName = logCall.getMethodName();
UExpression tag = arguments.get(0);
// 1st suggestion respects author's tag preference.
// 2nd suggestion drops it (Timber defaults to calling class name).
String fixSource1 = "Timber.tag(" + tag.asSourceString() + ").";
String fixSource2 = "Timber.";
int numArguments = arguments.size();
if (numArguments == 2) {
UExpression msgOrThrowable = arguments.get(1);
fixSource1 += methodName + "(" + msgOrThrowable.asSourceString() + ")";
fixSource2 += methodName + "(" + msgOrThrowable.asSourceString() + ")";
} else if (numArguments == 3) {
UExpression msg = arguments.get(1);
UExpression throwable = arguments.get(2);
fixSource1 +=
methodName + "(" + throwable.asSourceString() + ", " + msg.asSourceString() + ")";
fixSource2 +=
methodName + "(" + throwable.asSourceString() + ", " + msg.asSourceString() + ")";
} else {
throw new IllegalStateException("android.util.Log overloads should have 2 or 3 arguments");
}
String logCallSource = logCall.asSourceString();
LintFix.GroupBuilder fixGrouper = fix().group();
fixGrouper.add(
fix().replace().text(logCallSource).shortenNames().reformat(true).with(fixSource1).build());
fixGrouper.add(
fix().replace().text(logCallSource).shortenNames().reformat(true).with(fixSource2).build());
return fixGrouper.build();
}
示例9: quickFixIssueFormat
import com.android.tools.lint.detector.api.LintFix; //导入依赖的package包/类
private static LintFix quickFixIssueFormat(UCallExpression stringFormatCall) {
// Handles:
// 1) String.format(..)
// 2) format(...) [static import]
UExpression callReceiver = stringFormatCall.getReceiver();
String callSourceString = callReceiver == null ? "" : callReceiver.asSourceString() + ".";
callSourceString += stringFormatCall.getMethodName();
return fix().name("Remove String.format(...)").composite() //
// Delete closing parenthesis of String.format(...)
.add(fix().replace().pattern(callSourceString + "\\(.*(\\))").with("").build())
// Delete "String.format("
.add(fix().replace().text(callSourceString + "(").with("").build()).build();
}
示例10: quickFixIssueThrowable
import com.android.tools.lint.detector.api.LintFix; //导入依赖的package包/类
private static LintFix quickFixIssueThrowable(UCallExpression call, List<UExpression> arguments,
UExpression throwable) {
String rearrangedArgs = throwable.asSourceString();
for (UExpression arg : arguments) {
if (arg != throwable) {
rearrangedArgs += (", " + arg.asSourceString());
}
}
return fix().replace() //
.pattern("\\." + call.getMethodName() + "\\((.*)\\)").with(rearrangedArgs).build();
}
示例11: quickFixIssueTagLength
import com.android.tools.lint.detector.api.LintFix; //导入依赖的package包/类
private static LintFix quickFixIssueTagLength(UExpression argument, String tag) {
int numCharsToTrim = tag.length() - 23;
return fix().replace()
.name("Strip last " + (numCharsToTrim == 1 ? "char" : numCharsToTrim + " chars"))
.text(argument.asSourceString())
.with("\"" + tag.substring(0, 23) + "\"")
.build();
}
示例12: quickFixIssueExceptionLogging
import com.android.tools.lint.detector.api.LintFix; //导入依赖的package包/类
private static LintFix quickFixIssueExceptionLogging(UExpression arg2) {
return fix().replace()
.name("Remove redundant argument")
.text(", " + arg2.asSourceString())
.with("")
.build();
}