JetBrains/intellij-community · error · IncorrectOperationException

Enums only supported at language level 1.5 and higher

Error message

Enums only supported at language level 1.5 and higher

What it means

Companion guard to the annotation check: if the directory's language level is below JDK_1_5 and the template produced an enum, creation aborts with IncorrectOperationException. Enums arrived in Java 5, so a 1.4-level source root cannot host the generated file; the check runs after JavaDirectoryServiceImpl.checkCreateClassOrInterface but before the file is added to the directory.

Source

Thrown at java/java-impl/src/com/intellij/ide/fileTemplates/JavaCreateFromTemplateHandler.java:80

    }
    PsiClass createdClass = classes[0];
    String className;
    if (optionalClassName != null && createdClass instanceof PsiImplicitClass) {
      className = optionalClassName;
    }
    else {
      className = createdClass.getName();
    }
    JavaDirectoryServiceImpl.checkCreateClassOrInterface(directory, className);

    final LanguageLevel ll = JavaDirectoryService.getInstance().getLanguageLevel(directory);
    if (ll.compareTo(LanguageLevel.JDK_1_5) < 0) {
      if (createdClass.isAnnotationType()) {
        throw new IncorrectOperationException("Annotations only supported at language level 1.5 and higher");
      }

      if (createdClass.isEnum()) {
        throw new IncorrectOperationException("Enums only supported at language level 1.5 and higher");
      }
    }

    psiJavaFile = (PsiJavaFile)psiJavaFile.setName(className + "." + extension);
    PsiElement addedElement = directory.add(psiJavaFile);
    if (addedElement instanceof PsiJavaFile) {
      psiJavaFile = (PsiJavaFile)addedElement;
      if(reformat){
        CodeStyleManager.getInstance(project).scheduleReformatWhenSettingsComputed(psiJavaFile);
      }

      return psiJavaFile.getClasses()[0];
    }
    else {
      PsiFile containingFile = addedElement.getContainingFile();
      throw new IncorrectOperationException("The file '" + containingFile +  "' was expected to be of JAVA file type, but got: '"+ containingFile.getFileType() + "'");
    }
  }

View on GitHub (pinned to be881553f2)

Solutions

  1. Raise the module/project language level to 1.5 or higher and retry creation.
  2. If the low level is required, use a class with int constants (the typesafe-enum pattern) instead of an enum.
  3. Fix the root cause in the build descriptor (sourceCompatibility / maven source+target) and re-import so the IDE stops reverting the level.

Example fix

// before (Gradle)
java { sourceCompatibility = JavaVersion.VERSION_1_4 }

// after
java { sourceCompatibility = JavaVersion.VERSION_1_8 }
Defensive patterns

Strategy: validation

Validate before calling

LanguageLevel ll = JavaDirectoryService.getInstance().getLanguageLevel(directory);
if (ll.isLessThan(LanguageLevel.JDK_1_5) && templateDeclaresEnum) {
  Messages.showWarningDialog("Raise the module language level to 1.5+ to create enums", "Language Level Too Low");
  return;
}

Prevention

When it happens

Trigger: Creating an enum from template ('New | Enum' or a custom enum template) in a module/directory whose language level is JDK_1_4 or lower.

Common situations: Legacy projects pinned to old language levels; source roots inheriting a low project-wide level; imported Maven/Gradle projects declaring source 1.3/1.4 for compatibility with ancient toolchains.

Related errors


AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14). Data as JSON: /api/errors/967aa069e4a46d60. Report an issue: GitHub.