Search in sources :

Example 1 with GrExpressionTypeCalculator

use of org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.GrExpressionTypeCalculator in project intellij-community by JetBrains.

the class GrReferenceExpressionImpl method calculateType.

@Nullable
private static PsiType calculateType(@NotNull GrReferenceExpressionImpl refExpr, boolean forceRValue) {
    final GroovyResolveResult[] results = refExpr.multiResolve(false, forceRValue);
    final GroovyResolveResult result = PsiImplUtil.extractUniqueResult(results);
    final PsiElement resolved = result.getElement();
    for (GrExpressionTypeCalculator calculator : GrExpressionTypeCalculator.EP_NAME.getExtensions()) {
        PsiType type = calculator.calculateType(refExpr, resolved);
        if (type != null)
            return type;
    }
    if (ResolveUtil.isClassReference(refExpr)) {
        GrExpression qualifier = refExpr.getQualifier();
        LOG.assertTrue(qualifier != null);
        return qualifier.getType();
    }
    if (PsiUtil.isCompileStatic(refExpr)) {
        final PsiType type;
        if (resolved instanceof GrField) {
            type = ((GrField) resolved).getType();
        } else if (resolved instanceof GrVariable) {
            type = ((GrVariable) resolved).getDeclaredType();
        } else if (resolved instanceof GrAccessorMethod) {
            type = ((GrAccessorMethod) resolved).getProperty().getType();
        } else {
            type = null;
        }
        if (type != null) {
            return result.getSubstitutor().substitute(type);
        }
    }
    final PsiType nominal = refExpr.getNominalType(forceRValue);
    Boolean reassigned = GrReassignedLocalVarsChecker.isReassignedVar(refExpr);
    if (reassigned != null && reassigned.booleanValue()) {
        return GrReassignedLocalVarsChecker.getReassignedVarType(refExpr, true);
    }
    final PsiType inferred = getInferredTypes(refExpr, resolved);
    if (inferred == null) {
        if (nominal == null) {
            //inside nested closure we could still try to infer from variable initializer. Not sound, but makes sense
            if (resolved instanceof GrVariable) {
                LOG.assertTrue(resolved.isValid());
                return ((GrVariable) resolved).getTypeGroovy();
            }
        }
        return nominal;
    }
    if (nominal == null)
        return inferred;
    if (!TypeConversionUtil.isAssignable(TypeConversionUtil.erasure(nominal), inferred, false)) {
        if (resolved instanceof GrVariable && ((GrVariable) resolved).getTypeElementGroovy() != null) {
            return nominal;
        }
    }
    return inferred;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrExpressionTypeCalculator(org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.GrExpressionTypeCalculator) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Nullable (org.jetbrains.annotations.Nullable)1 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)1 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrAccessorMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod)1 GrExpressionTypeCalculator (org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.GrExpressionTypeCalculator)1