Search in sources :

Example 1 with PsiArrayType

use of com.intellij.psi.PsiArrayType in project kotlin by JetBrains.

the class ConstantEvaluator method isArrayLiteral.

/**
     * Returns true if the node is pointing to a an array literal
     */
public static boolean isArrayLiteral(@Nullable PsiElement node) {
    if (node instanceof PsiReference) {
        PsiElement resolved = ((PsiReference) node).resolve();
        if (resolved instanceof PsiField) {
            PsiField field = (PsiField) resolved;
            if (field.getInitializer() != null) {
                return isArrayLiteral(field.getInitializer());
            }
        } else if (resolved instanceof PsiLocalVariable) {
            PsiLocalVariable variable = (PsiLocalVariable) resolved;
            PsiStatement statement = PsiTreeUtil.getParentOfType(node, PsiStatement.class, false);
            if (statement != null) {
                PsiStatement prev = PsiTreeUtil.getPrevSiblingOfType(statement, PsiStatement.class);
                String targetName = variable.getName();
                if (targetName == null) {
                    return false;
                }
                while (prev != null) {
                    if (prev instanceof PsiDeclarationStatement) {
                        for (PsiElement element : ((PsiDeclarationStatement) prev).getDeclaredElements()) {
                            if (variable.equals(element)) {
                                return isArrayLiteral(variable.getInitializer());
                            }
                        }
                    } else if (prev instanceof PsiExpressionStatement) {
                        PsiExpression expression = ((PsiExpressionStatement) prev).getExpression();
                        if (expression instanceof PsiAssignmentExpression) {
                            PsiAssignmentExpression assign = (PsiAssignmentExpression) expression;
                            PsiExpression lhs = assign.getLExpression();
                            if (lhs instanceof PsiReferenceExpression) {
                                PsiReferenceExpression reference = (PsiReferenceExpression) lhs;
                                if (targetName.equals(reference.getReferenceName()) && reference.getQualifier() == null) {
                                    return isArrayLiteral(assign.getRExpression());
                                }
                            }
                        }
                    }
                    prev = PsiTreeUtil.getPrevSiblingOfType(prev, PsiStatement.class);
                }
            }
        }
    } else if (node instanceof PsiNewExpression) {
        PsiNewExpression creation = (PsiNewExpression) node;
        if (creation.getArrayInitializer() != null) {
            return true;
        }
        PsiType type = creation.getType();
        if (type instanceof PsiArrayType) {
            return true;
        }
    } else if (node instanceof PsiParenthesizedExpression) {
        PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression) node;
        PsiExpression expression = parenthesizedExpression.getExpression();
        if (expression != null) {
            return isArrayLiteral(expression);
        }
    } else if (node instanceof PsiTypeCastExpression) {
        PsiTypeCastExpression castExpression = (PsiTypeCastExpression) node;
        PsiExpression operand = castExpression.getOperand();
        if (operand != null) {
            return isArrayLiteral(operand);
        }
    }
    return false;
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiDeclarationStatement(com.intellij.psi.PsiDeclarationStatement) PsiExpression(com.intellij.psi.PsiExpression) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) PsiReference(com.intellij.psi.PsiReference) PsiAssignmentExpression(com.intellij.psi.PsiAssignmentExpression) PsiLocalVariable(com.intellij.psi.PsiLocalVariable) PsiNewExpression(com.intellij.psi.PsiNewExpression) PsiArrayType(com.intellij.psi.PsiArrayType) PsiTypeCastExpression(com.intellij.psi.PsiTypeCastExpression) PsiExpressionStatement(com.intellij.psi.PsiExpressionStatement) PsiField(com.intellij.psi.PsiField) PsiParenthesizedExpression(com.intellij.psi.PsiParenthesizedExpression) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Example 2 with PsiArrayType

use of com.intellij.psi.PsiArrayType in project intellij-community by JetBrains.

the class TargetType method create.

@Nullable
public static TargetType create(final PsiArrayType arrayType) {
    PsiType currentComponentType = arrayType.getComponentType();
    while (currentComponentType instanceof PsiArrayType) {
        currentComponentType = ((PsiArrayType) currentComponentType).getComponentType();
    }
    if (!(currentComponentType instanceof PsiClassType)) {
        return null;
    }
    final String targetQName = arrayType.getCanonicalText();
    return new TargetType(targetQName, true, arrayType);
}
Also used : PsiClassType(com.intellij.psi.PsiClassType) PsiArrayType(com.intellij.psi.PsiArrayType) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with PsiArrayType

use of com.intellij.psi.PsiArrayType in project smali by JesusFreke.

the class SmaliArrayTypeElement method getType.

@NotNull
@Override
public PsiType getType() {
    ASTNode token = findChildByType(SmaliTokens.ARRAY_TYPE_PREFIX);
    assert token != null;
    PsiTypeElement baseType = findChildByClass(PsiTypeElement.class);
    assert baseType != null;
    PsiArrayType arrayType = new PsiArrayType(baseType.getType());
    int dimensions = token.getTextLength() - 1;
    while (dimensions > 0) {
        arrayType = new PsiArrayType(arrayType);
        dimensions--;
    }
    return arrayType;
}
Also used : PsiTypeElement(com.intellij.psi.PsiTypeElement) ASTNode(com.intellij.lang.ASTNode) PsiArrayType(com.intellij.psi.PsiArrayType) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PsiArrayType

use of com.intellij.psi.PsiArrayType in project intellij-community by JetBrains.

the class ComponentTypeOfMacro method calculateLookupItems.

@Override
public LookupElement[] calculateLookupItems(@NotNull Expression[] params, ExpressionContext context) {
    if (params.length != 1)
        return null;
    LookupElement[] lookupItems = params[0].calculateLookupItems(context);
    if (lookupItems == null)
        return null;
    List<LookupElement> result = ContainerUtil.newArrayList();
    for (LookupElement element : lookupItems) {
        PsiTypeLookupItem lookupItem = element.as(PsiTypeLookupItem.CLASS_CONDITION_KEY);
        if (lookupItem != null) {
            PsiType psiType = lookupItem.getType();
            if (psiType instanceof PsiArrayType) {
                result.add(PsiTypeLookupItem.createLookupItem(((PsiArrayType) psiType).getComponentType(), null));
            }
        }
    }
    return lookupItems;
}
Also used : PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiArrayType(com.intellij.psi.PsiArrayType) PsiType(com.intellij.psi.PsiType)

Example 5 with PsiArrayType

use of com.intellij.psi.PsiArrayType in project intellij-community by JetBrains.

the class ComponentTypeOfMacro method calculateResult.

@Override
public Result calculateResult(@NotNull Expression[] params, final ExpressionContext context) {
    if (params.length != 1)
        return null;
    final Result result = params[0].calculateResult(context);
    if (result == null)
        return null;
    if (result instanceof PsiTypeResult) {
        PsiType type = ((PsiTypeResult) result).getType();
        if (type instanceof PsiArrayType) {
            return new PsiTypeResult(((PsiArrayType) type).getComponentType(), context.getProject());
        }
    }
    PsiExpression expr = MacroUtil.resultToPsiExpression(result, context);
    PsiType type = expr == null ? MacroUtil.resultToPsiType(result, context) : expr.getType();
    if (type instanceof PsiArrayType) {
        return new PsiTypeResult(((PsiArrayType) type).getComponentType(), context.getProject());
    }
    LookupElement[] elements = params[0].calculateLookupItems(context);
    if (elements != null) {
        for (LookupElement element : elements) {
            PsiTypeLookupItem typeLookupItem = element.as(PsiTypeLookupItem.CLASS_CONDITION_KEY);
            if (typeLookupItem != null) {
                PsiType psiType = typeLookupItem.getType();
                if (psiType instanceof PsiArrayType) {
                    return new PsiTypeResult(((PsiArrayType) psiType).getComponentType(), context.getProject());
                }
            }
        }
    }
    return new PsiElementResult(null);
}
Also used : PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) PsiExpression(com.intellij.psi.PsiExpression) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiArrayType(com.intellij.psi.PsiArrayType) PsiType(com.intellij.psi.PsiType)

Aggregations

PsiArrayType (com.intellij.psi.PsiArrayType)12 PsiType (com.intellij.psi.PsiType)10 PsiClassType (com.intellij.psi.PsiClassType)4 PsiElement (com.intellij.psi.PsiElement)3 PsiExpression (com.intellij.psi.PsiExpression)3 Nullable (com.android.annotations.Nullable)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 PsiTypeLookupItem (com.intellij.codeInsight.lookup.PsiTypeLookupItem)2 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)2 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)2 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)2 PsiField (com.intellij.psi.PsiField)2 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)2 PsiNewExpression (com.intellij.psi.PsiNewExpression)2 PsiParenthesizedExpression (com.intellij.psi.PsiParenthesizedExpression)2 PsiPrimitiveType (com.intellij.psi.PsiPrimitiveType)2 PsiReference (com.intellij.psi.PsiReference)2 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)2 PsiStatement (com.intellij.psi.PsiStatement)2 PsiTypeCastExpression (com.intellij.psi.PsiTypeCastExpression)2