Search in sources :

Example 6 with Location

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

the class DateFormatDetector method visitConstructor.

@Override
public void visitConstructor(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression node, @NonNull UMethod constructor) {
    if (!specifiesLocale(constructor)) {
        Location location = context.getUastLocation(node);
        String message = "To get local formatting use `getDateInstance()`, `getDateTimeInstance()`, " + "or `getTimeInstance()`, or use `new SimpleDateFormat(String template, " + "Locale locale)` with for example `Locale.US` for ASCII dates.";
        context.report(DATE_FORMAT, node, location, message);
    }
}
Also used : Location(com.android.tools.klint.detector.api.Location)

Example 7 with Location

use of com.android.tools.klint.detector.api.Location 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 Location

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

the class PrivateResourceDetector method visitElement.

@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
    if (TAG_RESOURCES.equals(element.getTagName())) {
        for (Element item : LintUtils.getChildren(element)) {
            Attr nameAttribute = item.getAttributeNode(ATTR_NAME);
            if (nameAttribute != null) {
                String name = getResourceFieldName(nameAttribute.getValue());
                String type = item.getTagName();
                if (type.equals(TAG_ITEM)) {
                    type = item.getAttribute(ATTR_TYPE);
                    if (type == null || type.isEmpty()) {
                        type = RESOURCE_CLZ_ID;
                    }
                } else if (type.equals("declare-styleable")) {
                    //$NON-NLS-1$
                    type = RESOURCE_CLR_STYLEABLE;
                } else if (type.contains("array")) {
                    //$NON-NLS-1$
                    // <string-array> etc
                    type = RESOURCE_CLZ_ARRAY;
                }
                ResourceType t = ResourceType.getEnum(type);
                if (t != null && isPrivate(context, t, name) && !VALUE_TRUE.equals(item.getAttributeNS(TOOLS_URI, ATTR_OVERRIDE))) {
                    String message = createOverrideErrorMessage(context, t, name);
                    Location location = context.getValueLocation(nameAttribute);
                    context.report(ISSUE, nameAttribute, location, message);
                }
            }
        }
    } else {
        assert TAG_STYLE.equals(element.getTagName()) || TAG_ARRAY.equals(element.getTagName()) || TAG_PLURALS.equals(element.getTagName()) || TAG_INTEGER_ARRAY.equals(element.getTagName()) || TAG_STRING_ARRAY.equals(element.getTagName());
        for (Element item : LintUtils.getChildren(element)) {
            checkChildRefs(context, item);
        }
    }
}
Also used : UElement(org.jetbrains.uast.UElement) Element(org.w3c.dom.Element) ResourceType(com.android.resources.ResourceType) Attr(org.w3c.dom.Attr) Location(com.android.tools.klint.detector.api.Location)

Example 9 with Location

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

the class PrivateResourceDetector method beforeCheckFile.

@Override
public void beforeCheckFile(@NonNull Context context) {
    File file = context.file;
    boolean isXmlFile = LintUtils.isXmlFile(file);
    if (!isXmlFile && !LintUtils.isBitmapFile(file)) {
        return;
    }
    String parentName = file.getParentFile().getName();
    int dash = parentName.indexOf('-');
    if (dash != -1 || FD_RES_VALUES.equals(parentName)) {
        return;
    }
    ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
    if (folderType == null) {
        return;
    }
    List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folderType);
    if (types.isEmpty()) {
        return;
    }
    ResourceType type = types.get(0);
    String resourceName = getResourceFieldName(getBaseName(file.getName()));
    if (isPrivate(context, type, resourceName)) {
        String message = createOverrideErrorMessage(context, type, resourceName);
        Location location = Location.create(file);
        context.report(ISSUE, location, message);
    }
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) ResourceType(com.android.resources.ResourceType) File(java.io.File) Location(com.android.tools.klint.detector.api.Location)

Example 10 with Location

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

the class RegistrationDetector method reportWrongTag.

private static void reportWrongTag(@NonNull JavaContext context, @NonNull PsiClass node, @NonNull String rightTag, @NonNull String className, @NonNull String framework) {
    String wrongTag = classToTag(framework);
    if (wrongTag == null) {
        return;
    }
    Location location = context.getNameLocation(node);
    String message = String.format("`%1$s` is %2$s but is registered " + "in the manifest as %3$s", className, describeTag(rightTag), describeTag(wrongTag));
    context.report(ISSUE, node, location, message);
}
Also used : 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