JetBrains/intellij-community · error · IncorrectOperationException

The file '{}' was expected to be of JAVA file type, but got:

Error message

The file '{}' was expected to be of JAVA file type, but got: '{}'

What it means

After adding the generated file to the target directory, the added element is expected to be a PsiJavaFile; if the containing file's type resolves to something other than the JAVA file type, an IncorrectOperationException names the file and its actual file type. The element was created with a '.java' extension, so this means IntelliJ's file-type resolution mapped that name to a different type — usually because a custom/other plugin registered a pattern or extension collision that wins over JAVA.

Source

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

      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() + "'");
    }
  }

  static void hackAwayEmptyPackage(PsiJavaFile file, FileTemplate template, Map<String, Object> props) throws IncorrectOperationException {
    if (!template.isTemplateOfType(JavaFileType.INSTANCE)) return;

    String packageName = (String)props.get(FileTemplate.ATTRIBUTE_PACKAGE_NAME);
    if(packageName == null || packageName.isEmpty() || packageName.equals(FileTemplate.ATTRIBUTE_PACKAGE_NAME)){
      PsiPackageStatement packageStatement = file.getPackageStatement();
      if (packageStatement != null) {
        packageStatement.delete();
      }
    }
  }

  @Override
  public boolean handlesTemplate(@NotNull FileTemplate template) {
    FileType fileType = FileTypeManagerEx.getInstanceEx().getFileTypeByExtension(template.getExtension());

View on GitHub (pinned to be881553f2)

Solutions

  1. Open Settings | Editor | File Types, find the non-Java type that lists '*.java' or the colliding pattern, and remove that pattern so it lives only under Java.
  2. Disable suspect plugins one by one (especially language/template plugins registering file patterns) and retry file creation.
  3. As a last resort, restore default file-type associations (delete the 'filetypes' chunk in config or use 'Restore Defaults' on the affected type).
Defensive patterns

Strategy: try-catch

Try / catch

try { directory.add(psiJavaFile); } catch (IncorrectOperationException e) { if (e.getMessage().contains("expected to be of JAVA file type")) { fixFileTypeAssociations(); } throw e; }

Prevention

When it happens

Trigger: directory.add(psiJavaFile) returns an element whose file type is not JavaFileType — e.g. a plugin registered '*.java' (or an overlapping filename pattern) under a different file type, or a corrupted file-type association set.

Common situations: Third-party plugins claiming the .java extension; manually edited 'file types' configuration in Settings | Editor | File Types; importing settings from another machine with conflicting patterns.

Related errors


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