{"record":{"id":"967aa069e4a46d60","repo":"JetBrains/intellij-community","slug":"enums-only-supported-at-language-level-1-5-and-hig","errorCode":null,"errorMessage":"Enums only supported at language level 1.5 and higher","messagePattern":"Enums only supported at language level 1\\.5 and higher","errorType":"exception","errorClass":"IncorrectOperationException","httpStatus":null,"severity":"error","filePath":"java/java-impl/src/com/intellij/ide/fileTemplates/JavaCreateFromTemplateHandler.java","lineNumber":80,"sourceCode":"    }\n    PsiClass createdClass = classes[0];\n    String className;\n    if (optionalClassName != null && createdClass instanceof PsiImplicitClass) {\n      className = optionalClassName;\n    }\n    else {\n      className = createdClass.getName();\n    }\n    JavaDirectoryServiceImpl.checkCreateClassOrInterface(directory, className);\n\n    final LanguageLevel ll = JavaDirectoryService.getInstance().getLanguageLevel(directory);\n    if (ll.compareTo(LanguageLevel.JDK_1_5) < 0) {\n      if (createdClass.isAnnotationType()) {\n        throw new IncorrectOperationException(\"Annotations only supported at language level 1.5 and higher\");\n      }\n\n      if (createdClass.isEnum()) {\n        throw new IncorrectOperationException(\"Enums only supported at language level 1.5 and higher\");\n      }\n    }\n\n    psiJavaFile = (PsiJavaFile)psiJavaFile.setName(className + \".\" + extension);\n    PsiElement addedElement = directory.add(psiJavaFile);\n    if (addedElement instanceof PsiJavaFile) {\n      psiJavaFile = (PsiJavaFile)addedElement;\n      if(reformat){\n        CodeStyleManager.getInstance(project).scheduleReformatWhenSettingsComputed(psiJavaFile);\n      }\n\n      return psiJavaFile.getClasses()[0];\n    }\n    else {\n      PsiFile containingFile = addedElement.getContainingFile();\n      throw new IncorrectOperationException(\"The file '\" + containingFile +  \"' was expected to be of JAVA file type, but got: '\"+ containingFile.getFileType() + \"'\");\n    }\n  }","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/java-impl/src/com/intellij/ide/fileTemplates/JavaCreateFromTemplateHandler.java#L62-L98","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Raise the module/project language level to 1.5 or higher and retry creation.","If the low level is required, use a class with int constants (the typesafe-enum pattern) instead of an enum.","Fix the root cause in the build descriptor (sourceCompatibility / maven source+target) and re-import so the IDE stops reverting the level."],"exampleFix":"// before (Gradle)\njava { sourceCompatibility = JavaVersion.VERSION_1_4 }\n\n// after\njava { sourceCompatibility = JavaVersion.VERSION_1_8 }","handlingStrategy":"validation","validationCode":"LanguageLevel ll = JavaDirectoryService.getInstance().getLanguageLevel(directory);\nif (ll.isLessThan(LanguageLevel.JDK_1_5) && templateDeclaresEnum) {\n  Messages.showWarningDialog(\"Raise the module language level to 1.5+ to create enums\", \"Language Level Too Low\");\n  return;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify language level before enum creation in automated/templated flows","Migrate legacy 1.4 modules or avoid enum templates in them"],"tags":["java","language-level","enum","file-templates","project-configuration"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}