Search in sources :

Example 1 with JavaEvaluator

use of com.android.tools.klint.client.api.JavaEvaluator in project kotlin by JetBrains.

the class AlarmDetector method visitMethod.

@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression node, @NonNull UMethod method) {
    JavaEvaluator evaluator = context.getEvaluator();
    if (evaluator.isMemberInClass(method, "android.app.AlarmManager") && evaluator.getParameterCount(method) == 4) {
        ensureAtLeast(context, node, 1, 5000L);
        ensureAtLeast(context, node, 2, 60000L);
    }
}
Also used : JavaEvaluator(com.android.tools.klint.client.api.JavaEvaluator)

Example 2 with JavaEvaluator

use of com.android.tools.klint.client.api.JavaEvaluator in project kotlin by JetBrains.

the class AllowAllHostnameVerifierDetector method visitMethod.

@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression node, @NonNull UMethod method) {
    JavaEvaluator evaluator = context.getEvaluator();
    if (evaluator.methodMatches(method, null, false, "javax.net.ssl.HostnameVerifier")) {
        UExpression argument = node.getValueArguments().get(0);
        PsiElement resolvedArgument = UastUtils.tryResolve(argument);
        if (resolvedArgument instanceof PsiField) {
            PsiField field = (PsiField) resolvedArgument;
            if ("ALLOW_ALL_HOSTNAME_VERIFIER".equals(field.getName())) {
                Location location = context.getUastLocation(argument);
                String message = "Using the ALLOW_ALL_HOSTNAME_VERIFIER HostnameVerifier " + "is unsafe because it always returns true, which could cause " + "insecure network traffic due to trusting TLS/SSL server " + "certificates for wrong hostnames";
                context.report(ISSUE, argument, location, message);
            }
        }
    }
}
Also used : UExpression(org.jetbrains.uast.UExpression) PsiField(com.intellij.psi.PsiField) JavaEvaluator(com.android.tools.klint.client.api.JavaEvaluator) PsiElement(com.intellij.psi.PsiElement) Location(com.android.tools.klint.detector.api.Location)

Example 3 with JavaEvaluator

use of com.android.tools.klint.client.api.JavaEvaluator in project kotlin by JetBrains.

the class ViewConstructorDetector method checkClass.

@Override
public void checkClass(@NonNull JavaContext context, @NonNull UClass declaration) {
    // Only applies to concrete classes
    JavaEvaluator evaluator = context.getEvaluator();
    if (evaluator.isAbstract(declaration) || declaration instanceof PsiAnonymousClass) {
        // Ignore abstract classes
        return;
    }
    if (declaration.getContainingClass() != null && !evaluator.isStatic(declaration)) {
        // anyway since we'd need the outer instance
        return;
    }
    boolean found = false;
    for (PsiMethod constructor : declaration.getConstructors()) {
        if (isXmlConstructor(evaluator, constructor)) {
            found = true;
            break;
        }
    }
    if (!found) {
        String message = String.format("Custom view `%1$s` is missing constructor used by tools: " + "`(Context)` or `(Context,AttributeSet)` " + "or `(Context,AttributeSet,int)`", declaration.getName());
        Location location = context.getUastNameLocation(declaration);
        context.reportUast(ISSUE, declaration, location, message);
    }
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiAnonymousClass(com.intellij.psi.PsiAnonymousClass) JavaEvaluator(com.android.tools.klint.client.api.JavaEvaluator) Location(com.android.tools.klint.detector.api.Location)

Example 4 with JavaEvaluator

use of com.android.tools.klint.client.api.JavaEvaluator in project kotlin by JetBrains.

the class ViewTagDetector method visitMethod.

@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression call, @NonNull UMethod method) {
    // http://code.google.com/p/android/issues/detail?id=18273
    if (context.getMainProject().getMinSdk() >= 14) {
        return;
    }
    JavaEvaluator evaluator = context.getEvaluator();
    if (!evaluator.isMemberInSubClassOf(method, CLASS_VIEW, false)) {
        return;
    }
    List<UExpression> arguments = call.getValueArguments();
    if (arguments.size() != 2) {
        return;
    }
    UExpression tagArgument = arguments.get(1);
    if (tagArgument == null) {
        return;
    }
    PsiType type = TypeEvaluator.evaluate(context, tagArgument);
    if ((!(type instanceof PsiClassType))) {
        return;
    }
    PsiClass typeClass = ((PsiClassType) type).resolve();
    if (typeClass == null) {
        return;
    }
    String objectType;
    if (InheritanceUtil.isInheritor(typeClass, false, CLASS_VIEW)) {
        objectType = "views";
    } else if (InheritanceUtil.isInheritor(typeClass, false, CURSOR_CLS)) {
        objectType = "cursors";
    } else if (typeClass.getName() != null && typeClass.getName().endsWith("ViewHolder")) {
        objectType = "view holders";
    } else {
        return;
    }
    String message = String.format("Avoid setting %1$s as values for `setTag`: " + "Can lead to memory leaks in versions older than Android 4.0", objectType);
    context.report(ISSUE, call, context.getUastLocation(tagArgument), 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)

Example 5 with JavaEvaluator

use of com.android.tools.klint.client.api.JavaEvaluator in project kotlin by JetBrains.

the class StringFormatDetector method visitMethod.

@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression node, @NonNull UMethod method) {
    if (mFormatStrings == null && !context.getClient().supportsProjectResources()) {
        return;
    }
    JavaEvaluator evaluator = context.getEvaluator();
    String methodName = method.getName();
    if (methodName.equals(FORMAT_METHOD)) {
        if (JavaEvaluator.isMemberInClass(method, TYPE_STRING)) {
            // Check formatting parameters for
            //   java.lang.String#format(String format, Object... formatArgs)
            //   java.lang.String#format(Locale locale, String format, Object... formatArgs)
            checkStringFormatCall(context, method, node, method.getParameterList().getParametersCount() == 3);
        // TODO: Consider also enforcing
        // java.util.Formatter#format(String string, Object... formatArgs)
        }
    } else {
        // an error.
        if (method.getParameterList().getParametersCount() < 2) {
            return;
        }
        if (evaluator.isMemberInSubClassOf(method, CLASS_RESOURCES, false) || evaluator.isMemberInSubClassOf(method, CLASS_CONTEXT, false) || evaluator.isMemberInSubClassOf(method, CLASS_FRAGMENT, false) || evaluator.isMemberInSubClassOf(method, CLASS_V4_FRAGMENT, false)) {
            checkStringFormatCall(context, method, node, false);
        }
    // TODO: Consider also looking up
    // android.content.res.Resources#getQuantityString(@PluralsRes int id, int quantity,
    //              Object... formatArgs)
    // though this will require being smarter about cross referencing formatting
    // strings since we'll need to go via the quantity string definitions
    }
}
Also used : JavaEvaluator(com.android.tools.klint.client.api.JavaEvaluator)

Aggregations

JavaEvaluator (com.android.tools.klint.client.api.JavaEvaluator)26 PsiMethod (com.intellij.psi.PsiMethod)9 Location (com.android.tools.klint.detector.api.Location)7 UExpression (org.jetbrains.uast.UExpression)7 PsiClass (com.intellij.psi.PsiClass)3 PsiClassType (com.intellij.psi.PsiClassType)2 PsiElement (com.intellij.psi.PsiElement)2 PsiType (com.intellij.psi.PsiType)2 Nullable (com.android.annotations.Nullable)1 PsiAnnotation (com.intellij.psi.PsiAnnotation)1 PsiAnonymousClass (com.intellij.psi.PsiAnonymousClass)1 PsiField (com.intellij.psi.PsiField)1 UAnonymousClass (org.jetbrains.uast.UAnonymousClass)1 UElement (org.jetbrains.uast.UElement)1