Search in sources :

Example 1 with GrLiteralClassType

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

the class DefaultCallExpressionTypeCalculator method calculateReturnType.

@Override
public PsiType calculateReturnType(@NotNull GrMethodCall callExpression, GroovyResolveResult[] resolveResults) {
    GrExpression invoked = callExpression.getInvokedExpression();
    if (invoked instanceof GrReferenceExpression) {
        GrReferenceExpression refExpr = (GrReferenceExpression) invoked;
        PsiManager manager = callExpression.getManager();
        PsiType result = null;
        for (GroovyResolveResult resolveResult : resolveResults) {
            PsiType returnType = calculateReturnTypeInner(callExpression, refExpr, resolveResult);
            if (returnType == null)
                return null;
            PsiType nonVoid = PsiType.VOID.equals(returnType) && !isCompileStatic(callExpression) ? PsiType.NULL : returnType;
            PsiType normalized = nonVoid instanceof GrLiteralClassType ? nonVoid : TypesUtil.substituteAndNormalizeType(nonVoid, resolveResult.getSubstitutor(), resolveResult.getSpreadState(), callExpression);
            LOG.assertTrue(normalized != null, "return type: " + returnType + "; substitutor: " + resolveResult.getSubstitutor());
            if (result == null || normalized.isAssignableFrom(result)) {
                result = normalized;
            } else if (!result.isAssignableFrom(normalized)) {
                result = TypesUtil.getLeastUpperBound(result, normalized, manager);
            }
        }
        return result;
    } else {
        return extractReturnTypeFromType(invoked.getType(), false, callExpression);
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrLiteralClassType(org.jetbrains.plugins.groovy.lang.psi.impl.GrLiteralClassType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 2 with GrLiteralClassType

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

the class ExpressionGenerator method visitListOrMap.

@Override
public void visitListOrMap(@NotNull GrListOrMap listOrMap) {
    final PsiType type = listOrMap.getType();
    //can be PsiArrayType or GrLiteralClassType
    LOG.assertTrue(type instanceof GrLiteralClassType || type instanceof PsiArrayType || type instanceof PsiClassType);
    if (listOrMap.isMap()) {
        if (listOrMap.getNamedArguments().length == 0) {
            builder.append("new ");
            TypeWriter.writeTypeForNew(builder, type, listOrMap);
            builder.append("()");
        } else {
            String varName = generateMapVariableDeclaration(listOrMap, type);
            generateMapElementInsertions(listOrMap, varName);
            builder.append(varName);
        }
    } else {
        builder.append("new ");
        PsiType typeToUse = getTypeToUseByList(listOrMap, type);
        TypeWriter.writeTypeForNew(builder, typeToUse, listOrMap);
        if (typeToUse instanceof PsiArrayType) {
            if (listOrMap.getInitializers().length == 0) {
                builder.replace(builder.length() - 2, builder.length(), "[0]");
            } else {
                builder.append('{');
                genInitializers(listOrMap);
                builder.append('}');
            }
        } else if (listOrMap.getInitializers().length == 0) {
            builder.append("()");
        } else {
            builder.append("(java.util.Arrays.asList(");
            genInitializers(listOrMap);
            builder.append("))");
        }
    }
}
Also used : GrLiteralClassType(org.jetbrains.plugins.groovy.lang.psi.impl.GrLiteralClassType) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)

Aggregations

GrLiteralClassType (org.jetbrains.plugins.groovy.lang.psi.impl.GrLiteralClassType)2 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)1