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;
}
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;
}
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;
}
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()]);
}
}
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 + " ";
}
}
Aggregations