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