Search in sources :

Example 6 with JavaEvaluator

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

the class GetSignaturesDetector 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, PACKAGE_MANAGER_CLASS, true, JavaParser.TYPE_STRING, JavaParser.TYPE_INT)) {
        return;
    }
    List<UExpression> arguments = node.getValueArguments();
    UExpression second = arguments.get(1);
    Object number = ConstantEvaluator.evaluate(context, second);
    if (number instanceof Number) {
        int flagValue = ((Number) number).intValue();
        maybeReportIssue(flagValue, context, node, second);
    }
}
Also used : UExpression(org.jetbrains.uast.UExpression) JavaEvaluator(com.android.tools.klint.client.api.JavaEvaluator)

Example 7 with JavaEvaluator

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

the class TrustAllX509TrustManagerDetector method checkMethod.

private static void checkMethod(@NonNull JavaContext context, @NonNull UClass cls, @NonNull String methodName) {
    JavaEvaluator evaluator = context.getEvaluator();
    for (PsiMethod method : cls.findMethodsByName(methodName, true)) {
        if (evaluator.isAbstract(method)) {
            continue;
        }
        // For now very simple; only checks if nothing is done.
        // Future work: Improve this check to be less sensitive to irrelevant
        // instructions/statements/invocations (e.g. System.out.println) by
        // looking for calls that could lead to a CertificateException being
        // thrown, e.g. throw statement within the method itself or invocation
        // of another method that may throw a CertificateException, and only
        // reporting an issue if none of these calls are found. ControlFlowGraph
        // may be useful here.
        UExpression body = context.getUastContext().getMethodBody(method);
        ComplexBodyVisitor visitor = new ComplexBodyVisitor();
        body.accept(visitor);
        if (!visitor.isComplex()) {
            Location location = context.getNameLocation(method);
            String message = getErrorMessage(methodName);
            context.report(ISSUE, method, location, message);
        }
    }
}
Also used : UExpression(org.jetbrains.uast.UExpression) PsiMethod(com.intellij.psi.PsiMethod) JavaEvaluator(com.android.tools.klint.client.api.JavaEvaluator) Location(com.android.tools.klint.detector.api.Location)

Example 8 with JavaEvaluator

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

the class UnsafeBroadcastReceiverDetector method checkClass.

@Override
public void checkClass(@NonNull JavaContext context, @NonNull UClass declaration) {
    String name = declaration.getName();
    if (name == null) {
        // anonymous classes can't be the ones referenced in the manifest
        return;
    }
    String qualifiedName = declaration.getQualifiedName();
    if (qualifiedName == null) {
        return;
    }
    if (!mReceiversWithProtectedBroadcastIntentFilter.contains(qualifiedName)) {
        return;
    }
    JavaEvaluator evaluator = context.getEvaluator();
    for (PsiMethod method : declaration.findMethodsByName("onReceive", false)) {
        if (evaluator.parametersMatch(method, CLASS_CONTEXT, CLASS_INTENT)) {
            checkOnReceive(context, method);
        }
    }
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) JavaEvaluator(com.android.tools.klint.client.api.JavaEvaluator)

Example 9 with JavaEvaluator

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

the class LogDetector method visitMethod.

@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression node, @NonNull UMethod method) {
    JavaEvaluator evaluator = context.getEvaluator();
    if (!JavaEvaluator.isMemberInClass(method, LOG_CLS)) {
        return;
    }
    String name = method.getName();
    boolean withinConditional = IS_LOGGABLE.equals(name) || checkWithinConditional(context, node.getUastParent(), node);
    // log methods (info, verbose, etc))
    if (("i".equals(name) || "d".equals(name) || "v".equals(name) || PRINTLN.equals(name)) && !withinConditional && performsWork(node) && context.isEnabled(CONDITIONAL)) {
        String message = String.format("The log call Log.%1$s(...) should be " + "conditional: surround with `if (Log.isLoggable(...))` or " + "`if (BuildConfig.DEBUG) { ... }`", name);
        context.report(CONDITIONAL, node, context.getUastLocation(node), message);
    }
    // Check tag length
    if (context.isEnabled(LONG_TAG)) {
        int tagArgumentIndex = PRINTLN.equals(name) ? 1 : 0;
        PsiParameterList parameterList = method.getParameterList();
        List<UExpression> argumentList = node.getValueArguments();
        if (evaluator.parameterHasType(method, tagArgumentIndex, TYPE_STRING) && parameterList.getParametersCount() == argumentList.size()) {
            UExpression argument = argumentList.get(tagArgumentIndex);
            String tag = ConstantEvaluator.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);
                context.report(LONG_TAG, node, context.getUastLocation(node), message);
            }
        }
    }
}
Also used : JavaEvaluator(com.android.tools.klint.client.api.JavaEvaluator)

Example 10 with JavaEvaluator

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

the class RecyclerViewDetector method checkClass.

@Override
public void checkClass(@NonNull JavaContext context, @NonNull UClass declaration) {
    JavaEvaluator evaluator = context.getEvaluator();
    for (PsiMethod method : declaration.findMethodsByName(ON_BIND_VIEW_HOLDER, false)) {
        int size = evaluator.getParameterCount(method);
        if (size == 2 || size == 3) {
            checkMethod(context, method, declaration);
        }
    }
    super.checkClass(context, declaration);
}
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