Search in sources :

Example 1 with LintFix

use of com.android.tools.lint.detector.api.LintFix in project Signal-Android by WhisperSystems.

the class AlertDialogBuilderDetector method visitConstructor.

@Override
public void visitConstructor(JavaContext context, @NotNull UCallExpression call, @NotNull PsiMethod method) {
    JavaEvaluator evaluator = context.getEvaluator();
    if (evaluator.isMemberInClass(method, "android.app.AlertDialog.Builder")) {
        LintFix fix = quickFixIssueAlertDialogBuilder(call);
        context.report(ALERT_DIALOG_BUILDER_USAGE, call, context.getLocation(call), "Using 'android.app.AlertDialog.Builder' instead of com.google.android.material.dialog.MaterialAlertDialogBuilder", fix);
    }
    if (evaluator.isMemberInClass(method, "androidx.appcompat.app.AlertDialog.Builder")) {
        LintFix fix = quickFixIssueAlertDialogBuilder(call);
        context.report(ALERT_DIALOG_BUILDER_USAGE, call, context.getLocation(call), "Using 'androidx.appcompat.app.AlertDialog.Builder' instead of com.google.android.material.dialog.MaterialAlertDialogBuilder", fix);
    }
}
Also used : LintFix(com.android.tools.lint.detector.api.LintFix) JavaEvaluator(com.android.tools.lint.client.api.JavaEvaluator)

Example 2 with LintFix

use of com.android.tools.lint.detector.api.LintFix in project Signal-Android by WhisperSystems.

the class AlertDialogBuilderDetector method quickFixIssueAlertDialogBuilder.

private LintFix quickFixIssueAlertDialogBuilder(@NotNull UCallExpression alertBuilderCall) {
    List<UExpression> arguments = alertBuilderCall.getValueArguments();
    UExpression context = arguments.get(0);
    String fixSource = "new com.google.android.material.dialog.MaterialAlertDialogBuilder";
    switch(arguments.size()) {
        case 1:
            fixSource += String.format("(%s)", context);
            break;
        case 2:
            UExpression themeOverride = arguments.get(1);
            fixSource += String.format("(%s, %s)", context, themeOverride);
            break;
        default:
            throw new IllegalStateException("MaterialAlertDialogBuilder overloads should have 1 or 2 arguments");
    }
    String builderCallSource = alertBuilderCall.asSourceString();
    LintFix.GroupBuilder fixGrouper = fix().group();
    fixGrouper.add(fix().replace().text(builderCallSource).shortenNames().reformat(true).with(fixSource).build());
    return fixGrouper.build();
}
Also used : UExpression(org.jetbrains.uast.UExpression) LintFix(com.android.tools.lint.detector.api.LintFix)

Example 3 with LintFix

use of com.android.tools.lint.detector.api.LintFix in project Signal-Android by WhisperSystems.

the class SignalLogDetector method visitMethodCall.

@Override
public void visitMethodCall(JavaContext context, @NotNull UCallExpression call, @NotNull PsiMethod method) {
    JavaEvaluator evaluator = context.getEvaluator();
    if (evaluator.isMemberInClass(method, "android.util.Log")) {
        LintFix fix = quickFixIssueLog(call);
        context.report(LOG_NOT_SIGNAL, call, context.getLocation(call), "Using 'android.util.Log' instead of a Signal Logger", fix);
    }
    if (evaluator.isMemberInClass(method, "org.signal.glide.Log")) {
        LintFix fix = quickFixIssueLog(call);
        context.report(LOG_NOT_SIGNAL, call, context.getLocation(call), "Using 'org.signal.glide.Log' instead of a Signal Logger", fix);
    }
    if (evaluator.isMemberInClass(method, "org.whispersystems.libsignal.logging.Log")) {
        LintFix fix = quickFixIssueLog(call);
        context.report(LOG_NOT_APP, call, context.getLocation(call), "Using Signal server logger instead of app level Logger", fix);
    }
    if (evaluator.isMemberInClass(method, "org.signal.core.util.logging.Log")) {
        List<UExpression> arguments = call.getValueArguments();
        UExpression tag = arguments.get(0);
        if (!(tag instanceof JavaUSimpleNameReferenceExpression || tag instanceof KotlinUSimpleReferenceExpression || tag instanceof KotlinUQualifiedReferenceExpression)) {
            context.report(INLINE_TAG, call, context.getLocation(call), "Not using a tag constant");
        }
    }
}
Also used : UExpression(org.jetbrains.uast.UExpression) KotlinUQualifiedReferenceExpression(org.jetbrains.uast.kotlin.KotlinUQualifiedReferenceExpression) LintFix(com.android.tools.lint.detector.api.LintFix) KotlinUSimpleReferenceExpression(org.jetbrains.uast.kotlin.KotlinUSimpleReferenceExpression) JavaUSimpleNameReferenceExpression(org.jetbrains.uast.java.JavaUSimpleNameReferenceExpression) JavaEvaluator(com.android.tools.lint.client.api.JavaEvaluator)

Example 4 with LintFix

use of com.android.tools.lint.detector.api.LintFix in project Signal-Android by WhisperSystems.

the class SignalLogDetector method quickFixIssueLog.

private LintFix quickFixIssueLog(@NotNull UCallExpression logCall) {
    List<UExpression> arguments = logCall.getValueArguments();
    String methodName = logCall.getMethodName();
    UExpression tag = arguments.get(0);
    String fixSource = "org.signal.core.util.logging.Log.";
    switch(arguments.size()) {
        case 2:
            UExpression msgOrThrowable = arguments.get(1);
            fixSource += String.format("%s(%s, %s)", methodName, tag, msgOrThrowable.asSourceString());
            break;
        case 3:
            UExpression msg = arguments.get(1);
            UExpression throwable = arguments.get(2);
            fixSource += String.format("%s(%s, %s, %s)", methodName, tag, msg.asSourceString(), throwable.asSourceString());
            break;
        default:
            throw new IllegalStateException("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(fixSource).build());
    return fixGrouper.build();
}
Also used : UExpression(org.jetbrains.uast.UExpression) LintFix(com.android.tools.lint.detector.api.LintFix)

Aggregations

LintFix (com.android.tools.lint.detector.api.LintFix)4 UExpression (org.jetbrains.uast.UExpression)3 JavaEvaluator (com.android.tools.lint.client.api.JavaEvaluator)2 JavaUSimpleNameReferenceExpression (org.jetbrains.uast.java.JavaUSimpleNameReferenceExpression)1 KotlinUQualifiedReferenceExpression (org.jetbrains.uast.kotlin.KotlinUQualifiedReferenceExpression)1 KotlinUSimpleReferenceExpression (org.jetbrains.uast.kotlin.KotlinUSimpleReferenceExpression)1