Search in sources :

Example 16 with Nullable

use of com.android.annotations.Nullable in project kotlin by JetBrains.

the class LintUtils method getStyleAttributes.

/**
     * Looks up the resource values for the given attribute given a style. Note that
     * this only looks project-level style values, it does not resume into the framework
     * styles.
     */
@Nullable
public static List<ResourceValue> getStyleAttributes(@NonNull Project project, @NonNull LintClient client, @NonNull String styleUrl, @NonNull String namespace, @NonNull String attribute) {
    if (!client.supportsProjectResources()) {
        return null;
    }
    AbstractResourceRepository resources = client.getProjectResources(project, true);
    if (resources == null) {
        return null;
    }
    ResourceUrl style = ResourceUrl.parse(styleUrl);
    if (style == null || style.framework) {
        return null;
    }
    List<ResourceValue> result = null;
    Queue<ResourceValue> queue = new ArrayDeque<ResourceValue>();
    queue.add(new ResourceValue(style.type, style.name, false));
    Set<String> seen = Sets.newHashSet();
    int count = 0;
    boolean isFrameworkAttribute = ANDROID_URI.equals(namespace);
    while (count < 30 && !queue.isEmpty()) {
        ResourceValue front = queue.remove();
        String name = front.getName();
        seen.add(name);
        List<ResourceItem> items = resources.getResourceItem(front.getResourceType(), name);
        if (items != null) {
            for (ResourceItem item : items) {
                ResourceValue rv = item.getResourceValue(false);
                if (rv instanceof StyleResourceValue) {
                    StyleResourceValue srv = (StyleResourceValue) rv;
                    ItemResourceValue value = srv.getItem(attribute, isFrameworkAttribute);
                    if (value != null) {
                        if (result == null) {
                            result = Lists.newArrayList();
                        }
                        if (!result.contains(value)) {
                            result.add(value);
                        }
                    }
                    String parent = srv.getParentStyle();
                    if (parent != null && !parent.startsWith(ANDROID_PREFIX)) {
                        ResourceUrl p = ResourceUrl.parse(parent);
                        if (p != null && !p.framework && !seen.contains(p.name)) {
                            seen.add(p.name);
                            queue.add(new ResourceValue(ResourceType.STYLE, p.name, false));
                        }
                    }
                    int index = name.lastIndexOf('.');
                    if (index > 0) {
                        String parentName = name.substring(0, index);
                        if (!seen.contains(parentName)) {
                            seen.add(parentName);
                            queue.add(new ResourceValue(ResourceType.STYLE, parentName, false));
                        }
                    }
                }
            }
        }
        count++;
    }
    return result;
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) AbstractResourceRepository(com.android.ide.common.res2.AbstractResourceRepository) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceItem(com.android.ide.common.res2.ResourceItem) ResourceUrl(com.android.ide.common.resources.ResourceUrl) Nullable(com.android.annotations.Nullable)

Example 17 with Nullable

use of com.android.annotations.Nullable in project kotlin by JetBrains.

the class LintUtils method getCommonParent.

/**
     * Computes the closest common parent path between two files.
     *
     * @param file1 the first file to be compared
     * @param file2 the second file to be compared
     * @return the closest common ancestor file, or null if the two files have
     *         no common parent
     */
@Nullable
public static File getCommonParent(@NonNull File file1, @NonNull File file2) {
    if (file1.equals(file2)) {
        return file1;
    } else if (file1.getPath().startsWith(file2.getPath())) {
        return file2;
    } else if (file2.getPath().startsWith(file1.getPath())) {
        return file1;
    } else {
        // Dumb and simple implementation
        File first = file1.getParentFile();
        while (first != null) {
            File second = file2.getParentFile();
            while (second != null) {
                if (first.equals(second)) {
                    return first;
                }
                second = second.getParentFile();
            }
            first = first.getParentFile();
        }
    }
    return null;
}
Also used : File(java.io.File) Nullable(com.android.annotations.Nullable)

Example 18 with Nullable

use of com.android.annotations.Nullable in project kotlin by JetBrains.

the class JavaPsiVisitor method getInterfaceNames.

@Nullable
private static Set<String> getInterfaceNames(@Nullable Set<String> addTo, @NonNull PsiClass cls) {
    for (PsiClass resolvedInterface : cls.getInterfaces()) {
        String name = resolvedInterface.getQualifiedName();
        if (addTo == null) {
            addTo = Sets.newHashSet();
        } else if (addTo.contains(name)) {
            // more than once.
            continue;
        }
        addTo.add(name);
        getInterfaceNames(addTo, resolvedInterface);
    }
    return addTo;
}
Also used : PsiClass(com.intellij.psi.PsiClass) Nullable(com.android.annotations.Nullable)

Example 19 with Nullable

use of com.android.annotations.Nullable in project kotlin by JetBrains.

the class JavaVisitor method getInterfaceNames.

@Nullable
private static Set<String> getInterfaceNames(@Nullable Set<String> addTo, @NonNull ResolvedClass cls) {
    Iterable<ResolvedClass> interfaces = cls.getInterfaces();
    for (ResolvedClass resolvedInterface : interfaces) {
        String name = resolvedInterface.getName();
        if (addTo == null) {
            addTo = Sets.newHashSet();
        } else if (addTo.contains(name)) {
            // more than once.
            continue;
        }
        addTo.add(name);
        getInterfaceNames(addTo, resolvedInterface);
    }
    return addTo;
}
Also used : ResolvedClass(com.android.tools.klint.client.api.JavaParser.ResolvedClass) Nullable(com.android.annotations.Nullable)

Example 20 with Nullable

use of com.android.annotations.Nullable in project kotlin by JetBrains.

the class UastLintUtils method findLastValue.

@Nullable
public static Object findLastValue(@NonNull PsiVariable variable, @NonNull UElement call, @NonNull JavaContext context, @NonNull ConstantEvaluator evaluator) {
    Object value = null;
    if (!variable.hasModifierProperty(PsiModifier.FINAL) && (variable instanceof PsiLocalVariable || variable instanceof PsiParameter)) {
        UMethod containingFunction = UastUtils.getContainingUMethod(call);
        if (containingFunction != null) {
            ConstantEvaluator.LastAssignmentFinder finder = new ConstantEvaluator.LastAssignmentFinder(variable, call, context, evaluator, 1);
            containingFunction.getUastBody().accept(finder);
            value = finder.getCurrentValue();
        }
    } else {
        UExpression initializer = context.getUastContext().getInitializerBody(variable);
        if (initializer != null) {
            value = initializer.evaluate();
        }
    }
    return value;
}
Also used : ConstantEvaluator(com.android.tools.klint.detector.api.ConstantEvaluator) JavaAbstractUExpression(org.jetbrains.uast.java.JavaAbstractUExpression) Nullable(com.android.annotations.Nullable)

Aggregations

Nullable (com.android.annotations.Nullable)83 File (java.io.File)21 IOException (java.io.IOException)9 ResourceType (com.android.resources.ResourceType)8 PsiClass (com.intellij.psi.PsiClass)7 PsiElement (com.intellij.psi.PsiElement)7 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)7 ResourceUrl (com.android.ide.common.resources.ResourceUrl)6 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)6 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)6 PsiExpression (com.intellij.psi.PsiExpression)6 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)6 PsiMethod (com.intellij.psi.PsiMethod)6 PsiStatement (com.intellij.psi.PsiStatement)6 ArrayList (java.util.ArrayList)6 PsiField (com.intellij.psi.PsiField)5 PsiFile (com.intellij.psi.PsiFile)4 Node (lombok.ast.Node)4 UReferenceExpression (org.jetbrains.uast.UReferenceExpression)4 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)3