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); buffer.append(helper.getName()); buffer.append("("); for (String param : getParameterString(helper, true)) { buffer.append(param); } buffer.append(") { \n"); GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject()); generateBody(helper, type == PsiType.VOID, buffer, helper.isForceReturn()); buffer.append("\n}"); String methodText = buffer.toString(); GrMethod method = factory.createMethodFromText(methodText); LOG.assertTrue(method != null); return method; }
public static String getModifierString(ExtractMethodInfoHelper helper) { String visibility = helper.getVisibility(); LOG.assertTrue(visibility != null && visibility.length() > 0); final StringBuilder builder = new StringBuilder(); builder.append(visibility); builder.append(" "); if (helper.isStatic()) { builder.append("static "); } return builder.toString(); }
public static String getTypeString( ExtractMethodInfoHelper helper, boolean forPresentation, String modifier) { PsiType type = helper.getOutputType(); final PsiPrimitiveType outUnboxed = PsiPrimitiveType.getUnboxedType(type); if (outUnboxed != null) type = outUnboxed; String typeText = forPresentation ? type.getPresentableText() : type.getCanonicalText(); String returnType = typeText == null || !helper.specifyType() ? "" : typeText; if (StringUtil.isEmptyOrSpaces(returnType) || "null".equals(returnType)) { if (modifier.length() == 0) { return "def "; } else { return ""; } } else { return returnType + " "; } }
@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 + " "; } }