Search in sources :

Example 26 with Location

use of com.android.tools.klint.detector.api.Location in project kotlin by JetBrains.

the class MathDetector method visitMethod.

@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression call, @NonNull UMethod method) {
    if (context.getEvaluator().isMemberInClass(method, "android.util.FloatMath") && context.getProject().getMinSdk() >= 8) {
        String message = String.format("Use `java.lang.Math#%1$s` instead of `android.util.FloatMath#%1$s()` " + "since it is faster as of API 8", method.getName());
        Location location = context.getUastLocation(call.getMethodIdentifier());
        context.report(ISSUE, call, location, message);
    }
}
Also used : Location(com.android.tools.klint.detector.api.Location)

Example 27 with Location

use of com.android.tools.klint.detector.api.Location in project kotlin by JetBrains.

the class OverdrawDetector method afterCheckProject.

@Override
public void afterCheckProject(@NonNull Context context) {
    if (mRootAttributes != null) {
        for (Pair<Location, String> pair : mRootAttributes) {
            Location location = pair.getFirst();
            Object clientData = location.getClientData();
            if (clientData instanceof Node) {
                if (context.getDriver().isSuppressed(null, ISSUE, (Node) clientData)) {
                    return;
                }
            }
            String layoutName = location.getFile().getName();
            if (endsWith(layoutName, DOT_XML)) {
                layoutName = layoutName.substring(0, layoutName.length() - DOT_XML.length());
            }
            String theme = getTheme(context, layoutName);
            if (theme == null || !isBlankTheme(theme)) {
                String drawable = pair.getSecond();
                String message = String.format("Possible overdraw: Root element paints background `%1$s` with " + "a theme that also paints a background (inferred theme is `%2$s`)", drawable, theme);
                // TODO: Compute applicable scope node
                context.report(ISSUE, location, message);
            }
        }
    }
}
Also used : Node(org.w3c.dom.Node) Location(com.android.tools.klint.detector.api.Location)

Example 28 with Location

use of com.android.tools.klint.detector.api.Location in project kotlin by JetBrains.

the class OverdrawDetector method visitAttribute.

// ---- Implements XmlScanner ----
@Override
public void visitAttribute(@NonNull XmlContext context, @NonNull Attr attribute) {
    // android View background attribute
    if (!ANDROID_URI.equals(attribute.getNamespaceURI())) {
        return;
    }
    // Only consider the root element's background
    Element documentElement = attribute.getOwnerDocument().getDocumentElement();
    if (documentElement == attribute.getOwnerElement()) {
        // If the drawable is a non-repeated pattern then the overdraw might be
        // intentional since the image isn't covering the whole screen
        String background = attribute.getValue();
        if (mValidDrawables != null && mValidDrawables.contains(background)) {
            return;
        }
        if (background.equals(TRANSPARENT_COLOR) || background.equals(NULL_RESOURCE)) {
            return;
        }
        if (background.startsWith("@android:drawable/")) {
            // but many of these are not bitmaps, so ignore these
            return;
        }
        String name = context.file.getName();
        if (name.contains("list_") || name.contains("_item")) {
            // pretty common to want to paint custom list item backgrounds
            return;
        }
        if (!context.getProject().getReportIssues()) {
            // If this is a library project not being analyzed, ignore it
            return;
        }
        Location location = context.getLocation(attribute);
        location.setClientData(attribute);
        if (mRootAttributes == null) {
            mRootAttributes = new ArrayList<Pair<Location, String>>();
        }
        mRootAttributes.add(Pair.of(location, attribute.getValue()));
        String activity = documentElement.getAttributeNS(TOOLS_URI, ATTR_CONTEXT);
        if (activity != null && !activity.isEmpty()) {
            if (activity.startsWith(".")) {
                //$NON-NLS-1$
                activity = context.getProject().getPackage() + activity;
            }
            registerLayoutActivity(LintUtils.getLayoutName(context.file), activity);
        }
    }
}
Also used : UElement(org.jetbrains.uast.UElement) PsiElement(com.intellij.psi.PsiElement) Element(org.w3c.dom.Element) Location(com.android.tools.klint.detector.api.Location) Pair(com.android.utils.Pair)

Example 29 with Location

use of com.android.tools.klint.detector.api.Location in project kotlin by JetBrains.

the class ParcelDetector method checkClass.

@Override
public void checkClass(@NonNull JavaContext context, @NonNull UClass declaration) {
    if (declaration instanceof UAnonymousClass) {
        // Anonymous classes aren't parcelable
        return;
    }
    // Only applies to concrete classes
    if (declaration.isInterface()) {
        return;
    }
    if (declaration.hasModifierProperty(PsiModifier.ABSTRACT)) {
        return;
    }
    // Parceling spans is handled in TextUtils#CHAR_SEQUENCE_CREATOR
    if (InheritanceUtil.isInheritor(declaration, false, "android.text.ParcelableSpan")) {
        return;
    }
    PsiField field = declaration.findFieldByName("CREATOR", false);
    if (field == null) {
        Location location = context.getUastNameLocation(declaration);
        context.reportUast(ISSUE, declaration, location, "This class implements `Parcelable` but does not " + "provide a `CREATOR` field");
    }
}
Also used : PsiField(com.intellij.psi.PsiField) UAnonymousClass(org.jetbrains.uast.UAnonymousClass) Location(com.android.tools.klint.detector.api.Location)

Example 30 with Location

use of com.android.tools.klint.detector.api.Location in project kotlin by JetBrains.

the class PreferenceActivityDetector method checkClass.

@Override
public void checkClass(@NonNull JavaContext context, @NonNull UClass declaration) {
    if (!context.getProject().getReportIssues()) {
        return;
    }
    JavaEvaluator evaluator = context.getEvaluator();
    String className = declaration.getQualifiedName();
    if (InheritanceUtil.isInheritor(declaration, false, PREFERENCE_ACTIVITY) && mExportedActivities.containsKey(className)) {
        // valid fragments.
        if (context.getMainProject().getTargetSdk() >= 19 && overridesIsValidFragment(evaluator, declaration)) {
            return;
        }
        String message = String.format("`PreferenceActivity` subclass `%1$s` should not be exported", className);
        Location location = mExportedActivities.get(className).resolve();
        context.reportUast(ISSUE, declaration, location, message);
    }
}
Also used : JavaEvaluator(com.android.tools.klint.client.api.JavaEvaluator) Location(com.android.tools.klint.detector.api.Location)

Aggregations

Location (com.android.tools.klint.detector.api.Location)38 UExpression (org.jetbrains.uast.UExpression)8 JavaEvaluator (com.android.tools.klint.client.api.JavaEvaluator)7 File (java.io.File)7 PsiElement (com.intellij.psi.PsiElement)4 PsiMethod (com.intellij.psi.PsiMethod)4 ArrayList (java.util.ArrayList)4 Attr (org.w3c.dom.Attr)4 Node (org.w3c.dom.Node)4 Handle (com.android.tools.klint.detector.api.Location.Handle)3 XmlContext (com.android.tools.klint.detector.api.XmlContext)3 PsiClassType (com.intellij.psi.PsiClassType)3 PsiType (com.intellij.psi.PsiType)3 List (java.util.List)3 UAnonymousClass (org.jetbrains.uast.UAnonymousClass)3 NodeList (org.w3c.dom.NodeList)3 NonNull (com.android.annotations.NonNull)2 ResourceType (com.android.resources.ResourceType)2 ClassContext (com.android.tools.klint.detector.api.ClassContext)2 Context (com.android.tools.klint.detector.api.Context)2