Search in sources :

Example 1 with JavaUDeclarationsExpression

use of org.jetbrains.uast.java.JavaUDeclarationsExpression in project kotlin by JetBrains.

the class UastLintUtils method toAndroidReferenceViaResolve.

@Nullable
public static AndroidReference toAndroidReferenceViaResolve(UElement element) {
    if (element instanceof UQualifiedReferenceExpression && element instanceof JavaAbstractUExpression) {
        AndroidReference ref = toAndroidReference((UQualifiedReferenceExpression) element);
        if (ref != null) {
            return ref;
        }
    }
    PsiElement declaration;
    if (element instanceof UVariable) {
        declaration = ((UVariable) element).getPsi();
    } else if (element instanceof UResolvable) {
        declaration = ((UResolvable) element).resolve();
    } else {
        return null;
    }
    if (declaration == null && element instanceof USimpleNameReferenceExpression && element instanceof JavaAbstractUExpression) {
        // R class can't be resolved in tests so we need to use heuristics to calc the reference 
        UExpression maybeQualified = UastUtils.getQualifiedParentOrThis((UExpression) element);
        if (maybeQualified instanceof UQualifiedReferenceExpression) {
            AndroidReference ref = toAndroidReference((UQualifiedReferenceExpression) maybeQualified);
            if (ref != null) {
                return ref;
            }
        }
    }
    if (!(declaration instanceof PsiVariable)) {
        return null;
    }
    PsiVariable variable = (PsiVariable) declaration;
    if (!(variable instanceof PsiField) || variable.getType() != PsiType.INT || !variable.hasModifierProperty(PsiModifier.STATIC) || !variable.hasModifierProperty(PsiModifier.FINAL)) {
        return null;
    }
    PsiClass resTypeClass = ((PsiField) variable).getContainingClass();
    if (resTypeClass == null || !resTypeClass.hasModifierProperty(PsiModifier.STATIC)) {
        return null;
    }
    PsiClass rClass = resTypeClass.getContainingClass();
    if (rClass == null || rClass.getContainingClass() != null || !"R".equals(rClass.getName())) {
        return null;
    }
    String packageName = ((PsiJavaFile) rClass.getContainingFile()).getPackageName();
    if (packageName.isEmpty()) {
        return null;
    }
    String resourceTypeName = resTypeClass.getName();
    ResourceType resourceType = null;
    for (ResourceType value : ResourceType.values()) {
        if (value.getName().equals(resourceTypeName)) {
            resourceType = value;
            break;
        }
    }
    if (resourceType == null) {
        return null;
    }
    String resourceName = variable.getName();
    UExpression node;
    if (element instanceof UExpression) {
        node = (UExpression) element;
    } else if (element instanceof UVariable) {
        node = new JavaUDeclarationsExpression(null, Collections.singletonList(((UVariable) element)));
    } else {
        throw new IllegalArgumentException("element must be an expression or an UVariable");
    }
    return new AndroidReference(node, packageName, resourceType, resourceName);
}
Also used : ResourceType(com.android.resources.ResourceType) JavaAbstractUExpression(org.jetbrains.uast.java.JavaAbstractUExpression) JavaAbstractUExpression(org.jetbrains.uast.java.JavaAbstractUExpression) JavaUDeclarationsExpression(org.jetbrains.uast.java.JavaUDeclarationsExpression) Nullable(com.android.annotations.Nullable)

Aggregations

Nullable (com.android.annotations.Nullable)1 ResourceType (com.android.resources.ResourceType)1 JavaAbstractUExpression (org.jetbrains.uast.java.JavaAbstractUExpression)1 JavaUDeclarationsExpression (org.jetbrains.uast.java.JavaUDeclarationsExpression)1