Search in sources :

Example 11 with GrIndexProperty

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty in project intellij-community by JetBrains.

the class GenerationUtil method getDeclaredType.

@Nullable
public static PsiType getDeclaredType(@Nullable GrExpression expression, ExpressionContext context) {
    if (expression instanceof GrReferenceExpression) {
        final GroovyResolveResult resolveResult = ((GrReferenceExpression) expression).advancedResolve();
        final PsiSubstitutor substitutor = resolveResult.getSubstitutor();
        PsiElement resolved = resolveResult.getElement();
        if (resolved instanceof PsiVariable) {
            return substitutor.substitute(context.typeProvider.getVarType((PsiVariable) resolved));
        } else if (resolved instanceof PsiMethod) {
            return getDeclaredType((PsiMethod) resolved, substitutor, context);
        }
    } else if (expression instanceof GrMethodCall) {
        final GrExpression invokedExpression = ((GrMethodCall) expression).getInvokedExpression();
        return getDeclaredType(invokedExpression, context);
    } else if (expression instanceof GrBinaryExpression) {
        final GroovyResolveResult result = PsiImplUtil.extractUniqueResult(((GrBinaryExpression) expression).multiResolve(false));
        if (result.getElement() instanceof PsiMethod) {
            return getDeclaredType((PsiMethod) result.getElement(), result.getSubstitutor(), context);
        }
    } else if (expression instanceof GrIndexProperty) {
        final GroovyResolveResult result = ((GrIndexProperty) expression).advancedResolve();
        if (result.getElement() instanceof PsiMethod) {
            return getDeclaredType((PsiMethod) result.getElement(), result.getSubstitutor(), context);
        }
    } else if (expression instanceof GrAssignmentExpression) {
        return getDeclaredType(((GrAssignmentExpression) expression).getRValue(), context);
    } else if (expression instanceof GrConditionalExpression) {
        return TypesUtil.getLeastUpperBoundNullable(getDeclaredType(((GrConditionalExpression) expression).getThenBranch(), context), getDeclaredType(((GrConditionalExpression) expression).getElseBranch(), context), expression.getManager());
    } else if (expression instanceof GrParenthesizedExpression) {
        return getDeclaredType(((GrParenthesizedExpression) expression).getOperand(), context);
    } else if (expression == null) {
        return null;
    }
    return expression.getType();
}
Also used : GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrIndexProperty (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty)11 IElementType (com.intellij.psi.tree.IElementType)5 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)5 PsiElement (com.intellij.psi.PsiElement)4 Nullable (org.jetbrains.annotations.Nullable)4 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)3 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)2 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)2 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)2 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)2 GrBindingVariable (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable)2 ASTNode (com.intellij.lang.ASTNode)1 PsiType (com.intellij.psi.PsiType)1 LightElement (com.intellij.psi.impl.light.LightElement)1 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)1 GrSpreadArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrSpreadArgument)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1