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;
}
Aggregations