@Nullable
  public static GrTypeDefinition createClassByType(@NotNull final PsiDirectory directory,
                                                   @NotNull final String name,
                                                   @NotNull final PsiManager manager,
                                                   @Nullable final PsiElement contextElement,
                                                   @NotNull final String templateName,
                                                   boolean allowReformatting) {
    return WriteAction.compute(() -> {
      try {
        GrTypeDefinition targetClass = null;
        try {
          PsiFile file = GroovyTemplatesFactory.createFromTemplate(directory, name, name + ".groovy", templateName, allowReformatting);
          for (PsiElement element : file.getChildren()) {
            if (element instanceof GrTypeDefinition) {
              targetClass = ((GrTypeDefinition)element);
              break;
            }
          }
          if (targetClass == null) {
            throw new IncorrectOperationException(GroovyBundle.message("no.class.in.file.template"));
          }
        }
        catch (final IncorrectOperationException e) {
          ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(
            GroovyBundle.message("cannot.create.class.error.text", name, e.getLocalizedMessage()),
            GroovyBundle.message("cannot.create.class.error.title")));
          return null;
        }
        PsiModifierList modifiers = targetClass.getModifierList();
        if (contextElement != null &&
            !JavaPsiFacade.getInstance(manager.getProject()).getResolveHelper().isAccessible(targetClass, contextElement, null) &&
            modifiers != null) {
          modifiers.setModifierProperty(PsiModifier.PUBLIC, true);
        }
        return targetClass;
      }
      catch (IncorrectOperationException e) {
        LOG.error(e);
        return null;
      }
    });
  }
@Nullable
  public static GrTypeDefinition createClassByType(@NotNull final PsiDirectory directory,
                                                   @NotNull final String name,
                                                   @NotNull final PsiManager manager,
                                                   @Nullable final PsiElement contextElement,
                                                   @NotNull final String templateName,
                                                   boolean allowReformatting)