Search in sources :

Example 66 with Nullable

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

the class DomPsiParser method parse.

@Nullable
private Document parse(XmlContext context) {
    // Should only be called from read thread
    assert ApplicationManager.getApplication().isReadAccessAllowed();
    final PsiFile psiFile = IntellijLintUtils.getPsiFile(context);
    if (!(psiFile instanceof XmlFile)) {
        return null;
    }
    XmlFile xmlFile = (XmlFile) psiFile;
    try {
        return DomPsiConverter.convert(xmlFile);
    } catch (Throwable t) {
        myClient.log(t, "Failed converting PSI parse tree to DOM for file %1$s", context.file.getPath());
        return null;
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) Nullable(com.android.annotations.Nullable)

Example 67 with Nullable

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

the class DomPsiParser method parseXml.

@Nullable
@Override
public Document parseXml(@NonNull final XmlContext context) {
    assert myReadLock == null;
    myReadLock = ApplicationManager.getApplication().acquireReadActionLock();
    Document document = parse(context);
    if (document == null) {
        myReadLock.finish();
        myReadLock = null;
    }
    return document;
}
Also used : Document(org.w3c.dom.Document) Nullable(com.android.annotations.Nullable)

Example 68 with Nullable

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

the class ResourceEvaluator method getTypesFromAnnotations.

@Nullable
private EnumSet<ResourceType> getTypesFromAnnotations(PsiModifierListOwner owner) {
    if (mEvaluator == null) {
        return null;
    }
    for (PsiAnnotation annotation : mEvaluator.getAllAnnotations(owner)) {
        String signature = annotation.getQualifiedName();
        if (signature == null) {
            continue;
        }
        if (signature.equals(COLOR_INT_ANNOTATION)) {
            return EnumSet.of(COLOR_INT_MARKER_TYPE);
        }
        if (signature.equals(PX_ANNOTATION)) {
            return EnumSet.of(PX_MARKER_TYPE);
        }
        if (signature.endsWith(RES_SUFFIX) && signature.startsWith(SUPPORT_ANNOTATIONS_PREFIX)) {
            String typeString = signature.substring(SUPPORT_ANNOTATIONS_PREFIX.length(), signature.length() - RES_SUFFIX.length()).toLowerCase(Locale.US);
            ResourceType type = ResourceType.getEnum(typeString);
            if (type != null) {
                return EnumSet.of(type);
            } else if (typeString.equals("any")) {
                // @AnyRes
                return getAnyRes();
            }
        }
    }
    return null;
}
Also used : ResourceType(com.android.resources.ResourceType) PsiAnnotation(com.intellij.psi.PsiAnnotation) Nullable(com.android.annotations.Nullable)

Example 69 with Nullable

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

the class ResourceEvaluator method getResourceConstant.

/** Returns a resource URL based on the field reference in the code */
@Nullable
public static ResourceUrl getResourceConstant(@NonNull UElement node) {
    AndroidReference androidReference = toAndroidReferenceViaResolve(node);
    if (androidReference == null) {
        return null;
    }
    String name = androidReference.getName();
    ResourceType type = androidReference.getType();
    boolean isFramework = androidReference.getPackage().equals("android");
    return ResourceUrl.create(type, name, isFramework, false);
}
Also used : AndroidReference(com.android.tools.klint.client.api.AndroidReference) ResourceType(com.android.resources.ResourceType) Nullable(com.android.annotations.Nullable)

Example 70 with Nullable

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

the class ResourceEvaluator method getResourceTypes.

/**
     * Evaluates the given node and returns the resource types applicable to the
     * node, if any.
     *
     * @param element the element to compute the types for
     * @return the corresponding resource types
     */
@Nullable
public EnumSet<ResourceType> getResourceTypes(@Nullable UElement element) {
    if (element == null) {
        return null;
    }
    if (element instanceof UIfExpression) {
        UIfExpression expression = (UIfExpression) element;
        Object known = ConstantEvaluator.evaluate(null, expression.getCondition());
        if (known == Boolean.TRUE && expression.getThenExpression() != null) {
            return getResourceTypes(expression.getThenExpression());
        } else if (known == Boolean.FALSE && expression.getElseExpression() != null) {
            return getResourceTypes(expression.getElseExpression());
        } else {
            EnumSet<ResourceType> left = getResourceTypes(expression.getThenExpression());
            EnumSet<ResourceType> right = getResourceTypes(expression.getElseExpression());
            if (left == null) {
                return right;
            } else if (right == null) {
                return left;
            } else {
                EnumSet<ResourceType> copy = EnumSet.copyOf(left);
                copy.addAll(right);
                return copy;
            }
        }
    } else if (element instanceof UParenthesizedExpression) {
        UParenthesizedExpression parenthesizedExpression = (UParenthesizedExpression) element;
        return getResourceTypes(parenthesizedExpression.getExpression());
    } else if ((element instanceof UQualifiedReferenceExpression && mAllowDereference) || element instanceof UCallExpression) {
        UElement probablyCallExpression = element;
        if (element instanceof UQualifiedReferenceExpression) {
            UQualifiedReferenceExpression qualifiedExpression = (UQualifiedReferenceExpression) element;
            probablyCallExpression = qualifiedExpression.getSelector();
        }
        if ((probablyCallExpression instanceof UCallExpression)) {
            UCallExpression call = (UCallExpression) probablyCallExpression;
            PsiMethod method = call.resolve();
            PsiClass containingClass = UastUtils.getContainingClass(method);
            if (method != null && containingClass != null) {
                EnumSet<ResourceType> types = getTypesFromAnnotations(method);
                if (types != null) {
                    return types;
                }
                String qualifiedName = containingClass.getQualifiedName();
                String name = call.getMethodName();
                if ((CLASS_RESOURCES.equals(qualifiedName) || CLASS_CONTEXT.equals(qualifiedName) || CLASS_FRAGMENT.equals(qualifiedName) || CLASS_V4_FRAGMENT.equals(qualifiedName) || CLS_TYPED_ARRAY.equals(qualifiedName)) && name != null && name.startsWith("get")) {
                    List<UExpression> args = call.getValueArguments();
                    if (!args.isEmpty()) {
                        types = getResourceTypes(args.get(0));
                        if (types != null) {
                            return types;
                        }
                    }
                }
            }
        }
    }
    if (element instanceof UReferenceExpression) {
        ResourceUrl url = getResourceConstant(element);
        if (url != null) {
            return EnumSet.of(url.type);
        }
        PsiElement resolved = ((UReferenceExpression) element).resolve();
        if (resolved instanceof PsiVariable) {
            PsiVariable variable = (PsiVariable) resolved;
            UElement lastAssignment = UastLintUtils.findLastAssignment(variable, element, mContext);
            if (lastAssignment != null) {
                return getResourceTypes(lastAssignment);
            }
            return null;
        }
    }
    return null;
}
Also used : PsiVariable(com.intellij.psi.PsiVariable) PsiMethod(com.intellij.psi.PsiMethod) UParenthesizedExpression(org.jetbrains.uast.UParenthesizedExpression) UQualifiedReferenceExpression(org.jetbrains.uast.UQualifiedReferenceExpression) EnumSet(java.util.EnumSet) PsiClass(com.intellij.psi.PsiClass) ResourceType(com.android.resources.ResourceType) UCallExpression(org.jetbrains.uast.UCallExpression) UExpression(org.jetbrains.uast.UExpression) UElement(org.jetbrains.uast.UElement) UReferenceExpression(org.jetbrains.uast.UReferenceExpression) ResourceUrl(com.android.ide.common.resources.ResourceUrl) PsiElement(com.intellij.psi.PsiElement) UIfExpression(org.jetbrains.uast.UIfExpression) 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