JetBrains/intellij-community · error · IncorrectOperationException

Annotations only supported at language level 1.5 and higher

Error message

Annotations only supported at language level 1.5 and higher

What it means

After creating the class from template, the handler checks the effective language level of the target directory (JavaDirectoryService.getLanguageLevel); if it is below JDK_1_5 and the created type is an annotation type, this IncorrectOperationException is thrown. Annotations were introduced in Java 5, so writing '@interface' into a source root configured for 1.4 or lower is rejected before the file is added.

Source

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

    }
    final PsiClass[] classes = psiJavaFile.getClasses();
    if (classes.length == 0) {
      throw new IncorrectOperationException("This template did not produce a Java class or an interface\n"+psiFile.getText());
    }
    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 {

View on GitHub (pinned to be881553f2)

Solutions

  1. Raise the language level: File | Project Structure | Modules -> Sources tab -> Language Level, or set maven-compiler-plugin source/target (or Gradle sourceCompatibility) to 1.5+ and re-import.
  2. Or choose a template without an annotation type (plain class/interface) if the code must stay at the old level.
  3. After changing build config, re-import the project so the IDE language level updates.

Example fix

<!-- before (pom.xml) -->
<maven-compiler-plugin><configuration><source>1.4</source></configuration></maven-compiler-plugin>

<!-- after -->
<maven-compiler-plugin><configuration><source>1.8</source><target>1.8</target></configuration></maven-compiler-plugin>
Defensive patterns

Strategy: validation

Validate before calling

// Before creating from an annotation-type template:
LanguageLevel ll = JavaDirectoryService.getInstance().getLanguageLevel(directory);
if (ll.isLessThan(LanguageLevel.JDK_1_5) && templateDeclaresAnnotationType) {
  Messages.showWarningDialog("Raise the module language level to 1.5+ to create @interface files", "Language Level Too Low");
  return;
}

Prevention

When it happens

Trigger: Using a template that declares an @interface while the module/directory language level is JDK_1_4 or lower (e.g. legacy project, module set explicitly to 1.4, or a source root inheriting an old project level).

Common situations: Old projects never migrated past pre-5 language levels; modules whose language level was pinned for old build tooling; SDK or Maven/Gradle import setting a low source compatibility that maps to a low language level.

Related errors


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