private static @Nullable PsiClass createClassFromTemplate(final @NotNull Properties attributes, final @Nullable String templateName,
                                                            final @NotNull PsiDirectory directoryRoot,
                                                            final @NotNull String className) throws IncorrectOperationException {
    if (templateName == null) return null;
    if (templateName.equals(DO_NOT_CREATE_CLASS_TEMPLATE)) return null;

    final Project project = directoryRoot.getProject();
    try {
      final PsiDirectory directory = createParentDirectories(directoryRoot, className);
      final PsiFile psiFile = directory.findFile(className + "." + JavaFileType.INSTANCE.getDefaultExtension());
      if (psiFile != null) {
        psiFile.delete();
      }

      final String rawClassName = extractClassName(className);
      final PsiFile existing = directory.findFile(rawClassName + ".java");
      if (existing instanceof PsiJavaFile) {
        final PsiClass[] classes = ((PsiJavaFile)existing).getClasses();
        if (classes.length > 0) {
          return classes[0];
        }
        return null;
      }

      final PsiClass aClass;
      if (templateName.equals(DEFAULT_CLASS_TEMPLATE)) {
        aClass = JavaDirectoryService.getInstance().createClass(directory, rawClassName);
      }
      else {
        final FileTemplateManager fileTemplateManager = FileTemplateManager.getInstance(project);
        FileTemplate fileTemplate = fileTemplateManager.getJ2eeTemplate(templateName);
        final String text = fileTemplate.getText(attributes);
        aClass = JavaCreateFromTemplateHandler.createClassOrInterface(project, directory, text, true, fileTemplate.getExtension());
      }
      return (PsiClass)JavaCodeStyleManager.getInstance(project).shortenClassReferences(aClass);
    }
    catch (IOException e) {
      throw new IncorrectOperationException(e);
    }
  }
private static @Nullable PsiClass createClassFromTemplate(final @NotNull Properties attributes, final @Nullable String templateName,
                                                            final @NotNull PsiDirectory directoryRoot,
                                                            final @NotNull String className)