private static void setupMethodBody(final PsiMethod method, final PsiClass aClass, 
                                      final FileTemplate template, @Nullable ModPsiUpdater updater) 
    throws IncorrectOperationException {
    PsiType returnType = method.getReturnType();
    if (returnType == null) {
      returnType = PsiTypes.voidType();
    }

    JVMElementFactory factory = JVMElementFactories.getFactory(aClass.getLanguage(), aClass.getProject());

    LOG.assertTrue(!aClass.isInterface() ||
                   PsiUtil.isAvailable(JavaFeature.EXTENSION_METHODS, method) ||
                   method.getLanguage() != JavaLanguage.INSTANCE, "Interface bodies should be already set up");

    FileType fileType = FileTypeManager.getInstance().getFileTypeByExtension(template.getExtension());
    Properties properties = FileTemplateManager.getInstance(method.getProject()).getDefaultProperties();
    properties.setProperty(FileTemplate.ATTRIBUTE_RETURN_TYPE, returnType.getPresentableText());
    properties.setProperty(FileTemplate.ATTRIBUTE_DEFAULT_RETURN_VALUE,
                           PsiTypesUtil.getDefaultValueOfType(returnType));

    JavaTemplateUtil.setClassAndMethodNameProperties(properties, aClass, method);

    @NonNls String methodText;
    CodeStyleManager csManager = CodeStyleManager.getInstance(method.getProject());
    try {
      String bodyText = template.getText(properties);
      if (!bodyText.isEmpty()) bodyText += "\n";
      methodText = returnType.getPresentableText() + " foo () {\n" + bodyText + "}";
      methodText = FileTemplateUtil.indent(methodText, method.getProject(), fileType);
    }
    catch (ProcessCanceledException e) {
      throw e;
    }
    catch (Exception e) {
      throw new IncorrectOperationException("Failed to parse file template", (Throwable)e);
    }

    PsiMethod m;
    try {
      m = factory.createMethodFromText(methodText, aClass);
    }
    catch (IncorrectOperationException e) {
      if (updater == null) {
        ApplicationManager.getApplication().invokeLater(
          () -> Messages.showErrorDialog(QuickFixBundle.message("new.method.body.template.error.text"),
                                         QuickFixBundle.message("new.method.body.template.error.title")));
      } else {
        updater.cancel(QuickFixBundle.message("new.method.body.template.error.text"));
      }
      return;
    }

    PsiElement newBody = m.getBody();
    LOG.assertTrue(newBody != null);

    PsiElement oldBody = method.getBody();
    if (oldBody == null) {
      PsiElement last = method.getLastChild();
      if (last instanceof PsiErrorElement &&
          JavaPsiBundle.message("expected.lbrace.or.semicolon").equals(((PsiErrorElement)last).getErrorDescription())) {
        oldBody = last;
      }
    }
    if (oldBody != null) {
      oldBody.replace(newBody);
    }
    else {
      method.add(newBody);
    }

    csManager.reformat(method);
  }
private static void setupMethodBody(final PsiMethod method, final PsiClass aClass, 
                                      final FileTemplate template, @Nullable ModPsiUpdater updater)