Search in sources :

Example 16 with PsiType

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

the class InitialInfo method inferOutputType.

@Nullable
private PsiType inferOutputType(VariableInfo[] outputInfos, GrStatement[] statements, ArrayList<GrStatement> returnStatements, boolean hasReturnValue, StringPartInfo stringPartInfo) {
    if (stringPartInfo != null) {
        return stringPartInfo.getLiteral().getType();
    }
    PsiType outputType = PsiType.VOID;
    if (outputInfos.length > 0) {
        if (outputInfos.length == 1) {
            outputType = outputInfos[0].getType();
        } else {
            outputType = JavaPsiFacade.getElementFactory(myProject).createTypeFromText(CommonClassNames.JAVA_UTIL_LIST, getContext());
        }
    } else if (ExtractUtil.isSingleExpression(statements)) {
        outputType = ((GrExpression) statements[0]).getType();
    } else if (hasReturnValue) {
        assert !returnStatements.isEmpty();
        List<PsiType> types = new ArrayList<>(returnStatements.size());
        for (GrStatement statement : returnStatements) {
            if (statement instanceof GrReturnStatement) {
                GrExpression returnValue = ((GrReturnStatement) statement).getReturnValue();
                if (returnValue != null) {
                    types.add(returnValue.getType());
                }
            } else if (statement instanceof GrExpression) {
                types.add(((GrExpression) statement).getType());
            }
        }
        outputType = TypesUtil.getLeastUpperBoundNullable(types, getContext().getManager());
    }
    return outputType;
}
Also used : ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiType(com.intellij.psi.PsiType) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with PsiType

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

the class ExtractClosureHelperImpl method getSelectedType.

@Override
public PsiType getSelectedType() {
    if (myForceDef)
        return null;
    if (myType == null) {
        final GrClosableBlock closure = ExtractClosureProcessorBase.generateClosure(this);
        PsiType type = closure.getType();
        if (type instanceof PsiClassType) {
            final PsiType[] parameters = ((PsiClassType) type).getParameters();
            if (parameters.length == 1 && parameters[0] != null) {
                if (parameters[0].equalsToText(PsiType.VOID.getBoxedTypeName())) {
                    type = ((PsiClassType) type).rawType();
                }
            }
        }
        myType = type;
    }
    return myType;
}
Also used : PsiClassType(com.intellij.psi.PsiClassType) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) PsiType(com.intellij.psi.PsiType)

Example 18 with PsiType

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

the class SwitchStatementGenerator method generateConditionVar.

private static String generateConditionVar(@NotNull StringBuilder builder, @NotNull ExpressionContext context, @NotNull GrExpression condition) {
    StringBuilder conditionBuilder = new StringBuilder();
    final PsiType type = condition.getType();
    final String varName = GenerationUtil.validateName("switchArg", condition, context);
    conditionBuilder.append("final ");
    TypeWriter.writeType(conditionBuilder, type, condition);
    conditionBuilder.append(' ').append(varName).append(" = ");
    condition.accept(new ExpressionGenerator(conditionBuilder, context));
    conditionBuilder.append(";\n");
    GenerationUtil.insertStatementFromContextBefore(builder, context);
    builder.append(conditionBuilder);
    return varName;
}
Also used : PsiType(com.intellij.psi.PsiType)

Example 19 with PsiType

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

the class ExtractUtil method createResultStatement.

@NotNull
private static GrStatement[] createResultStatement(ExtractInfoHelper helper) {
    VariableInfo[] outputVars = helper.getOutputVariableInfos();
    PsiType type = helper.getOutputType();
    GrStatement[] statements = helper.getStatements();
    GrMethodCallExpression callExpression = createMethodCall(helper);
    if ((outputVars.length == 0 || PsiType.VOID.equals(type)) && !helper.hasReturnValue())
        return new GrStatement[] { callExpression };
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
    if (helper.hasReturnValue()) {
        return new GrStatement[] { factory.createStatementFromText("return " + callExpression.getText()) };
    }
    LOG.assertTrue(outputVars.length > 0);
    final List<VariableInfo> mustAdd = mustAddVariableDeclaration(statements, outputVars);
    if (mustAdd.isEmpty()) {
        return new GrStatement[] { createAssignment(outputVars, callExpression, helper.getProject()) };
    } else if (mustAdd.size() == outputVars.length && outputVars.length == 1) {
        return new GrVariableDeclaration[] { factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, callExpression, outputVars[0].getType(), outputVars[0].getName()) };
    } else if (varsAreEqual(mustAdd, outputVars)) {
        return createTupleDeclaration(outputVars, callExpression, helper.getProject());
    } else {
        final List<GrStatement> result = generateVarDeclarations(mustAdd, helper.getProject(), null);
        result.add(createAssignment(outputVars, callExpression, helper.getProject()));
        return result.toArray(new GrStatement[result.size()]);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) VariableInfo(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo) PsiType(com.intellij.psi.PsiType) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with PsiType

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

the class ExtractUtil method getTypeString.

@NotNull
public static String getTypeString(@NotNull ExtractMethodInfoHelper helper, boolean forPresentation, @NotNull String modifier) {
    if (!helper.specifyType()) {
        return modifier.isEmpty() ? "def " : "";
    }
    PsiType type = helper.getOutputType();
    final PsiPrimitiveType unboxed = PsiPrimitiveType.getUnboxedType(type);
    if (unboxed != null)
        type = unboxed;
    final String returnType = StringUtil.notNullize(forPresentation ? type.getPresentableText() : type.getCanonicalText());
    if (StringUtil.isEmptyOrSpaces(returnType) || "null".equals(returnType)) {
        return modifier.isEmpty() ? "def " : "";
    } else {
        return returnType + " ";
    }
}
Also used : PsiPrimitiveType(com.intellij.psi.PsiPrimitiveType) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiType (com.intellij.psi.PsiType)157 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)24 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)24 NotNull (org.jetbrains.annotations.NotNull)21 PsiClassType (com.intellij.psi.PsiClassType)20 PsiClass (com.intellij.psi.PsiClass)14 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 PsiPrimitiveType (com.intellij.psi.PsiPrimitiveType)12 ArrayList (java.util.ArrayList)9 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)9 PsiArrayType (com.intellij.psi.PsiArrayType)7 PsiExpression (com.intellij.psi.PsiExpression)7 PsiField (com.intellij.psi.PsiField)7 NonNls (org.jetbrains.annotations.NonNls)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 PsiLiteralExpression (com.intellij.psi.PsiLiteralExpression)6 PsiMethod (com.intellij.psi.PsiMethod)6 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)6 GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)6