JetBrains/intellij-community · error · IncorrectOperationException

Implicitly declared class may have no name

Error message

Implicitly declared class may have no name

What it means

PsiImplicitClassImpl models a Java 'implicitly declared class' (JEP 445 / unnamed classes, Java 21 preview feature): a compilation unit whose members live in a class with no declaration and no name. Since such a class has no name identifier in source, setName throws IncorrectOperationException("Implicitly declared class may have no name").

Source

Thrown at java/java-psi-impl/src/com/intellij/psi/impl/source/PsiImplicitClassImpl.java:255

  @Override
  public boolean isInheritorDeep(@NotNull PsiClass baseClass, @Nullable PsiClass classToByPass) {
    return InheritanceImplUtil.isInheritorDeep(this, baseClass, classToByPass);
  }

  @Override
  public @Nullable PsiClass getContainingClass() {
    return null;
  }

  @Override
  public @NotNull Collection<HierarchicalMethodSignature> getVisibleSignatures() {
    return PsiSuperMethodImplUtil.getVisibleSignatures(this);
  }

  @Override
  public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
    throw new IncorrectOperationException("Implicitly declared class may have no name");
  }

  @Override
  public boolean isDeprecated() {
    return false;
  }

  @Override
  public @Nullable PsiDocComment getDocComment() {
    return null;
  }

  @Override
  public @Nullable PsiModifierList getModifierList() {
    return null;
  }

  @Override

View on GitHub (pinned to be881553f2)

Solutions

  1. Skip name-based operations when the class has no name: check psiClass.getNameIdentifier() == null or instanceof PsiImplicitClassImpl before setName.
  2. Rename the containing file instead (an implicit class is named by its file in tooling contexts); use RenameFileProcessor for such elements.
  3. If the user tried to rename the class in the editor, guide them to rename the file, since the class name is derived.
  4. Update plugins that assume named classes to handle JEP 445 files when the language level is 21+ with preview enabled.

Example fix

// before
for (PsiClass c : javaFile.getClasses()) c.setName(newName); // throws on implicit class

// after
for (PsiClass c : javaFile.getClasses()) {
  if (c.getNameIdentifier() == null) continue; // implicit class: rename the file instead
c.setName(newName);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (psiClass.getNameIdentifier() == null) continue; // implicit class cannot be renamed

Type guard

static boolean isImplicitClass(PsiClass c) { return c instanceof PsiImplicitClassImpl; }

Try / catch

try { psiClass.setName(n); } catch (IncorrectOperationException e) { /* offer file rename instead */ }

Prevention

When it happens

Trigger: Calling setName on the PsiClass returned by PsiJavaFile.getClasses() for a file that is an implicit (unnamed) class, e.g. from a rename refactoring or a plugin that walks all classes in a file and renames them.

Common situations: Projects using Java 21+ preview features (unnamed classes with main); rename refactorings or code generators that assume every PsiClass has a name; structural searches that try to normalize class names.

Related errors


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