Search in sources :

Example 21 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)

Example 22 with PsiType

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

the class ExtractUtil method createTupleDeclaration.

private static GrStatement[] createTupleDeclaration(final VariableInfo[] infos, GrMethodCallExpression callExpression, final Project project) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    StringBuilder tuple = new StringBuilder();
    tuple.append("def (");
    for (VariableInfo info : infos) {
        final PsiType type = info.getType();
        if (type != null) {
            final PsiType unboxed = TypesUtil.unboxPrimitiveTypeWrapper(type);
            tuple.append(unboxed.getCanonicalText());
            tuple.append(' ');
        }
        tuple.append(info.getName());
        tuple.append(",");
    }
    StringUtil.trimEnd(tuple, ",");
    tuple.append(")=");
    tuple.append(callExpression.getText());
    return new GrStatement[] { factory.createStatementFromText(tuple) };
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) 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)

Example 23 with PsiType

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

the class ExtractUtil method haveDifferentTypes.

private static boolean haveDifferentTypes(List<VariableInfo> varInfos) {
    if (varInfos.size() < 2)
        return true;
    Set<String> diffTypes = new HashSet<>();
    for (VariableInfo info : varInfos) {
        final PsiType t = info.getType();
        diffTypes.add(t == null ? null : TypesUtil.unboxPrimitiveTypeWrapper(t).getCanonicalText());
    }
    return diffTypes.size() > 1;
}
Also used : VariableInfo(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo) PsiType(com.intellij.psi.PsiType)

Example 24 with PsiType

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

the class ExtractUtil method getParameterString.

public static String[] getParameterString(ExtractInfoHelper helper, boolean useCanonicalText) {
    int i = 0;
    ParameterInfo[] infos = helper.getParameterInfos();
    int number = 0;
    for (ParameterInfo info : infos) {
        if (info.passAsParameter)
            number++;
    }
    ArrayList<String> params = new ArrayList<>();
    for (ParameterInfo info : infos) {
        if (info.passAsParameter) {
            PsiType paramType = info.type;
            final PsiPrimitiveType unboxed = PsiPrimitiveType.getUnboxedType(paramType);
            if (unboxed != null)
                paramType = unboxed;
            String paramTypeText;
            if (paramType == null || paramType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) || paramType.equals(PsiType.NULL)) {
                paramTypeText = "";
            } else {
                paramTypeText = (useCanonicalText ? paramType.getCanonicalText() : paramType.getPresentableText()) + " ";
            }
            params.add(paramTypeText + info.getName() + (i < number - 1 ? ", " : ""));
            i++;
        }
    }
    return ArrayUtil.toStringArray(params);
}
Also used : PsiPrimitiveType(com.intellij.psi.PsiPrimitiveType) PsiType(com.intellij.psi.PsiType)

Example 25 with PsiType

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

the class ExtractUtil method createMethod.

public static GrMethod createMethod(ExtractMethodInfoHelper helper) {
    StringBuilder buffer = new StringBuilder();
    //Add signature
    PsiType type = helper.getOutputType();
    final PsiPrimitiveType outUnboxed = PsiPrimitiveType.getUnboxedType(type);
    if (outUnboxed != null)
        type = outUnboxed;
    String modifier = getModifierString(helper);
    String typeText = getTypeString(helper, false, modifier);
    buffer.append(modifier);
    buffer.append(typeText);
    appendName(buffer, helper.getName());
    buffer.append("(");
    for (String param : getParameterString(helper, true)) {
        buffer.append(param);
    }
    buffer.append(") { \n");
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
    generateBody(helper, PsiType.VOID.equals(type), buffer, helper.isForceReturn());
    buffer.append("\n}");
    String methodText = buffer.toString();
    return factory.createMethodFromText(methodText, helper.getContext());
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) PsiPrimitiveType(com.intellij.psi.PsiPrimitiveType) PsiType(com.intellij.psi.PsiType)

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