Search in sources :

Example 21 with UExpression

use of org.jetbrains.uast.UExpression in project kotlin by JetBrains.

the class JavaScriptInterfaceDetector method visitMethod.

@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression call, @NonNull UMethod method) {
    if (context.getMainProject().getTargetSdk() < 17) {
        return;
    }
    List<UExpression> arguments = call.getValueArguments();
    if (arguments.size() != 2) {
        return;
    }
    JavaEvaluator evaluator = context.getEvaluator();
    if (!JavaEvaluator.isMemberInClass(method, WEB_VIEW_CLS)) {
        return;
    }
    UExpression first = arguments.get(0);
    PsiType evaluated = TypeEvaluator.evaluate(context, first);
    if (evaluated instanceof PsiClassType) {
        PsiClassType classType = (PsiClassType) evaluated;
        PsiClass cls = classType.resolve();
        if (cls == null) {
            return;
        }
        if (isJavaScriptAnnotated(cls)) {
            return;
        }
        Location location = context.getUastNameLocation(call);
        String message = String.format("None of the methods in the added interface (%1$s) have been annotated " + "with `@android.webkit.JavascriptInterface`; they will not " + "be visible in API 17", cls.getName());
        context.report(ISSUE, call, location, message);
    }
}
Also used : UExpression(org.jetbrains.uast.UExpression) PsiClassType(com.intellij.psi.PsiClassType) PsiClass(com.intellij.psi.PsiClass) JavaEvaluator(com.android.tools.klint.client.api.JavaEvaluator) PsiType(com.intellij.psi.PsiType) Location(com.android.tools.klint.detector.api.Location)

Example 22 with UExpression

use of org.jetbrains.uast.UExpression in project kotlin by JetBrains.

the class LayoutInflationDetector method visitMethod.

@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression call, @NonNull UMethod method) {
    assert method.getName().equals(INFLATE);
    if (call.getReceiver() == null) {
        return;
    }
    List<UExpression> arguments = call.getValueArguments();
    if (arguments.size() < 2) {
        return;
    }
    UExpression second = arguments.get(1);
    if (!UastLiteralUtils.isNullLiteral(second)) {
        return;
    }
    UExpression first = arguments.get(0);
    AndroidReference androidReference = UastLintUtils.toAndroidReferenceViaResolve(first);
    if (androidReference == null) {
        return;
    }
    String layoutName = androidReference.getName();
    if (context.getScope().contains(Scope.RESOURCE_FILE)) {
        // incrementally
        if (!context.getDriver().isSuppressed(context, ISSUE, call)) {
            if (mPendingErrors == null) {
                mPendingErrors = Lists.newArrayList();
            }
            Location location = context.getUastLocation(second);
            mPendingErrors.add(Pair.of(layoutName, location));
        }
    } else if (hasLayoutParams(context, layoutName)) {
        context.report(ISSUE, call, context.getUastLocation(second), ERROR_MESSAGE);
    }
}
Also used : UExpression(org.jetbrains.uast.UExpression) AndroidReference(com.android.tools.klint.client.api.AndroidReference) Location(com.android.tools.klint.detector.api.Location)

Example 23 with UExpression

use of org.jetbrains.uast.UExpression in project kotlin by JetBrains.

the class NonInternationalizedSmsDetector method visitMethod.

@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression call, @NonNull UMethod method) {
    if (call.getReceiver() == null) {
        // "sendTextMessage"/"sendMultipartTextMessage" in the code with no operand
        return;
    }
    List<UExpression> args = call.getValueArguments();
    if (args.size() != 5) {
        return;
    }
    UExpression destinationAddress = args.get(0);
    if (!(destinationAddress instanceof ULiteralExpression)) {
        return;
    }
    Object literal = ((ULiteralExpression) destinationAddress).getValue();
    if (!(literal instanceof String)) {
        return;
    }
    String number = (String) literal;
    if (number.startsWith("+")) {
        //$NON-NLS-1$
        return;
    }
    context.report(ISSUE, call, context.getUastLocation(destinationAddress), "To make sure the SMS can be sent by all users, please start the SMS number " + "with a + and a country code or restrict the code invocation to people in the " + "country you are targeting.");
}
Also used : UExpression(org.jetbrains.uast.UExpression) ULiteralExpression(org.jetbrains.uast.ULiteralExpression)

Example 24 with UExpression

use of org.jetbrains.uast.UExpression in project kotlin by JetBrains.

the class ReadParcelableDetector method visitMethod.

@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression node, @NonNull UMethod method) {
    PsiClass containingClass = method.getContainingClass();
    if (containingClass == null) {
        return;
    }
    if (!(CLASS_PARCEL.equals(containingClass.getQualifiedName()))) {
        return;
    }
    List<UExpression> expressions = node.getValueArguments();
    int argumentCount = expressions.size();
    if (argumentCount == 0) {
        String message = String.format("Using the default class loader " + "will not work if you are restoring your own classes. Consider " + "using for example `%1$s(getClass().getClassLoader())` instead.", node.getMethodName());
        Location location = context.getUastLocation(node);
        context.report(ISSUE, node, location, message);
    } else if (argumentCount == 1) {
        UExpression parameter = expressions.get(0);
        if (UastLiteralUtils.isNullLiteral(parameter)) {
            String message = "Passing null here (to use the default class loader) " + "will not work if you are restoring your own classes. Consider " + "using for example `getClass().getClassLoader()` instead.";
            Location location = context.getUastLocation(node);
            context.report(ISSUE, node, location, message);
        }
    }
}
Also used : UExpression(org.jetbrains.uast.UExpression) PsiClass(com.intellij.psi.PsiClass) Location(com.android.tools.klint.detector.api.Location)

Example 25 with UExpression

use of org.jetbrains.uast.UExpression 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

UExpression (org.jetbrains.uast.UExpression)25 Location (com.android.tools.klint.detector.api.Location)8 JavaEvaluator (com.android.tools.klint.client.api.JavaEvaluator)7 PsiElement (com.intellij.psi.PsiElement)6 PsiMethod (com.intellij.psi.PsiMethod)6 PsiClass (com.intellij.psi.PsiClass)5 UCallExpression (org.jetbrains.uast.UCallExpression)5 ResourceUrl (com.android.ide.common.resources.ResourceUrl)4 PsiClassType (com.intellij.psi.PsiClassType)4 PsiType (com.intellij.psi.PsiType)4 UReferenceExpression (org.jetbrains.uast.UReferenceExpression)4 Nullable (com.android.annotations.Nullable)3 LintFix (com.android.tools.lint.detector.api.LintFix)3 PsiVariable (com.intellij.psi.PsiVariable)3 UElement (org.jetbrains.uast.UElement)3 ULiteralExpression (org.jetbrains.uast.ULiteralExpression)3 PsiField (com.intellij.psi.PsiField)2 UIfExpression (org.jetbrains.uast.UIfExpression)2 UParenthesizedExpression (org.jetbrains.uast.UParenthesizedExpression)2 UQualifiedReferenceExpression (org.jetbrains.uast.UQualifiedReferenceExpression)2