public static GrStatement replaceStatement(
     GrStatementOwner declarationOwner, ExtractInfoHelper helper) {
   GrStatement realStatement;
   if (declarationOwner != null && !isSingleExpression(helper.getStatements())) {
     // Replace set of statements
     final GrStatement[] newStatement = createResultStatement(helper);
     // add call statement
     final GrStatement[] statements = helper.getStatements();
     LOG.assertTrue(statements.length > 0);
     realStatement = null;
     for (GrStatement statement : newStatement) {
       realStatement = declarationOwner.addStatementBefore(statement, statements[0]);
       GrReferenceAdjuster.shortenReferences(realStatement);
     }
     LOG.assertTrue(realStatement != null);
     // remove old statements
     removeOldStatements(declarationOwner, helper);
     PsiImplUtil.removeNewLineAfter(realStatement);
   } else {
     // Expression call replace
     GrExpression methodCall = createMethodCall(helper);
     GrExpression oldExpr = (GrExpression) helper.getStatements()[0];
     realStatement = oldExpr.replaceWithExpression(methodCall, true);
     GrReferenceAdjuster.shortenReferences(realStatement);
   }
   return realStatement;
 }
  public static GrMethod generateDelegate(
      PsiMethod prototype, IntroduceParameterData.ExpressionWrapper initializer, Project project) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);

    GrMethod result;
    if (prototype instanceof GrMethod) {
      result = (GrMethod) prototype.copy();
    } else {
      StringBuilder builder = new StringBuilder();
      builder.append(prototype.getModifierList().getText()).append(' ');

      if (prototype.getReturnTypeElement() != null) {
        builder.append(prototype.getReturnTypeElement().getText());
      }
      builder.append(' ').append(prototype.getName());
      builder.append(prototype.getParameterList().getText());
      builder.append("{}");
      result = factory.createMethodFromText(builder.toString());
    }

    StringBuilder call = new StringBuilder();
    call.append("def foo(){\n");
    final GrParameter[] parameters = result.getParameters();
    call.append(prototype.getName());
    if (initializer.getExpression() instanceof GrClosableBlock) {
      if (parameters.length > 0) {
        call.append('(');
        for (GrParameter parameter : parameters) {
          call.append(parameter.getName()).append(", ");
        }
        call.replace(call.length() - 2, call.length(), ")");
      }
      call.append(initializer.getText());
    } else {
      call.append('(');
      for (GrParameter parameter : parameters) {
        call.append(parameter.getName()).append(", ");
      }
      call.append(initializer.getText());
      call.append(")");
    }
    call.append("\n}");
    final GrOpenBlock block = factory.createMethodFromText(call.toString()).getBlock();

    result.getBlock().replace(block);
    final PsiElement parent = prototype.getParent();
    final GrMethod method = (GrMethod) parent.addBefore(result, prototype);
    GrReferenceAdjuster.shortenReferences(method);
    return method;
  }