Search in sources :

Example 11 with Location

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

the class LayoutConsistencyDetector method chainLocations.

@NonNull
private static Location chainLocations(@NonNull List<Location> locations) {
    assert !locations.isEmpty();
    // Sort locations by the file parent folders
    if (locations.size() > 1) {
        Collections.sort(locations, new Comparator<Location>() {

            @Override
            public int compare(Location location1, Location location2) {
                File file1 = location1.getFile();
                File file2 = location2.getFile();
                String folder1 = file1.getParentFile().getName();
                String folder2 = file2.getParentFile().getName();
                return folder1.compareTo(folder2);
            }
        });
        // Chain locations together
        Iterator<Location> iterator = locations.iterator();
        assert iterator.hasNext();
        Location prev = iterator.next();
        while (iterator.hasNext()) {
            Location next = iterator.next();
            prev.setSecondary(next);
            prev = next;
        }
    }
    return locations.get(0);
}
Also used : File(java.io.File) Location(com.android.tools.klint.detector.api.Location) NonNull(com.android.annotations.NonNull)

Example 12 with Location

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

the class LayoutConsistencyDetector method lookupLocations.

private static void lookupLocations(@NonNull XmlContext context, @NonNull Element element, @NonNull Map<String, List<Location>> map) {
    String id = getId(element);
    if (id != null) {
        if (map.containsKey(id)) {
            if (context.getDriver().isSuppressed(context, INCONSISTENT_IDS, element)) {
                map.remove(id);
                return;
            }
            List<Location> locations = map.get(id);
            if (locations == null) {
                locations = Lists.newArrayList();
                map.put(id, locations);
            }
            Attr attr = element.getAttributeNodeNS(ANDROID_URI, ATTR_ID);
            assert attr != null;
            Location location = context.getLocation(attr);
            String folder = context.file.getParentFile().getName();
            location.setMessage(String.format("Occurrence in %1$s", folder));
            locations.add(location);
        }
    }
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            lookupLocations(context, (Element) child, map);
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Attr(org.w3c.dom.Attr) Location(com.android.tools.klint.detector.api.Location)

Example 13 with Location

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

the class LayoutInflationDetector method afterCheckProject.

@Override
public void afterCheckProject(@NonNull Context context) {
    if (mPendingErrors != null) {
        for (Pair<String, Location> pair : mPendingErrors) {
            String inflatedLayout = pair.getFirst();
            if (mLayoutsWithRootLayoutParams == null || !mLayoutsWithRootLayoutParams.contains(inflatedLayout)) {
                // No root layout parameters on the inflated layout: no need to complain
                continue;
            }
            Location location = pair.getSecond();
            context.report(ISSUE, location, ERROR_MESSAGE);
        }
    }
}
Also used : Location(com.android.tools.klint.detector.api.Location)

Example 14 with Location

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

the class LocaleDetector method checkFormat.

private static void checkFormat(@NonNull JavaContext context, @NonNull PsiMethod method, @NonNull UCallExpression call) {
    // Only check the non-locale version of String.format
    if (method.getParameterList().getParametersCount() == 0 || !context.getEvaluator().parameterHasType(method, 0, TYPE_STRING)) {
        return;
    }
    List<UExpression> expressions = call.getValueArguments();
    if (expressions.isEmpty()) {
        return;
    }
    // Find the formatting string
    UExpression first = expressions.get(0);
    Object value = ConstantEvaluator.evaluate(context, first);
    if (!(value instanceof String)) {
        return;
    }
    String format = (String) value;
    if (StringFormatDetector.isLocaleSpecific(format)) {
        if (isLoggingParameter(call)) {
            return;
        }
        Location location = context.getUastLocation(call);
        String message = "Implicitly using the default locale is a common source of bugs: " + "Use `String.format(Locale, ...)` instead";
        context.report(STRING_LOCALE, call, location, message);
    }
}
Also used : UExpression(org.jetbrains.uast.UExpression) Location(com.android.tools.klint.detector.api.Location)

Example 15 with Location

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

the class BadHostnameVerifierDetector method checkClass.

@Override
public void checkClass(@NonNull JavaContext context, @NonNull UClass declaration) {
    JavaEvaluator evaluator = context.getEvaluator();
    for (PsiMethod method : declaration.findMethodsByName("verify", false)) {
        if (evaluator.methodMatches(method, null, false, TYPE_STRING, "javax.net.ssl.SSLSession")) {
            ComplexVisitor visitor = new ComplexVisitor(context);
            declaration.accept(visitor);
            if (visitor.isComplex()) {
                return;
            }
            Location location = context.getNameLocation(method);
            String message = String.format("`%1$s` always returns `true`, which " + "could cause insecure network traffic due to trusting " + "TLS/SSL server certificates for wrong hostnames", method.getName());
            context.report(ISSUE, location, message);
            break;
        }
    }
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) 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