JetBrains/intellij-community · error · IncorrectOperationException
psi.error.attempt.to.edit.class.file
psi.error.attempt.to.edit.class.file
Error message
Cannot modify compiled element in file ''{0}'' What it means
PsiJavaModuleReferenceImpl.handleElementRename rewrites a 'requires' module name inside module-info.java. When the reference element belongs to a compiled module descriptor (module-info.class read from a jar), there is no writable source to edit, so it throws IncorrectOperationException with the localized key psi.error.attempt.to.edit.class.file, including the containing (class) file.
Source
Thrown at java/java-psi-impl/src/com/intellij/psi/impl/source/PsiJavaModuleReferenceImpl.java:51
public @NotNull String getCanonicalText() {
return getElement().getReferenceText();
}
@Override
public PsiJavaModule resolve() {
return (PsiJavaModule)super.resolve();
}
@Override
public ResolveResult @NotNull [] multiResolve(boolean incompleteCode) {
return ResolveCache.getInstance(getProject()).resolveWithCaching(this, Resolver.INSTANCE, false, incompleteCode);
}
@Override
public PsiElement handleElementRename(@NotNull String newName) throws IncorrectOperationException {
PsiJavaModuleReferenceElement element = getElement();
if (element instanceof PsiCompiledElement) {
throw new IncorrectOperationException(JavaPsiBundle.message("psi.error.attempt.to.edit.class.file", element.getContainingFile()));
}
PsiElement newElement = PsiElementFactory.getInstance(element.getProject()).createModuleReferenceFromText(newName, null);
return element.replace(newElement);
}
private Project getProject() {
return getElement().getProject();
}
private static class Resolver implements ResolveCache.PolyVariantResolver<PsiJavaModuleReferenceImpl> {
private static final ResolveCache.PolyVariantResolver<PsiJavaModuleReferenceImpl> INSTANCE = new Resolver();
@Override
public ResolveResult @NotNull [] resolve(@NotNull PsiJavaModuleReferenceImpl reference, boolean incompleteCode) {
PsiJavaModuleReferenceElement refElement = reference.getElement();
PsiFile file = refElement.getContainingFile();
String moduleName = reference.getCanonicalText();
View on GitHub (pinned to be881553f2)
Solutions
- Check element instanceof PsiCompiledElement before rename; if compiled, navigate to source (attach library sources or find the source module-info.java) and rename there.
- Attach sources for the jar (Library properties -> Sources tab) so the reference resolves to editable source PSI.
- If the module is third-party, rename the reference in your own code (requires statement) instead of renaming the module itself.
Example fix
// before
moduleRef.handleElementRename(newName); // module-info.class in jar -> throws
// after
PsiElement elem = moduleRef.getElement();
if (elem instanceof PsiCompiledElement) {
// cannot edit bytecode: attach sources or rename the reference usage instead
} else {
moduleRef.handleElementRename(newName);
} Defensive patterns
Strategy: type-guard
Validate before calling
if (moduleRef.getElement() instanceof PsiCompiledElement) return; // bytecode not editable
Type guard
static boolean isEditableModuleRef(PsiJavaModuleReference r) { return !(r.getElement() instanceof PsiCompiledElement); } Try / catch
try { moduleRef.handleElementRename(n); } catch (IncorrectOperationException e) { /* attach sources or edit own code */ } Prevention
- Attach library sources so module references resolve to editable PSI.
- Rename your own requires statements, not third-party module names.
When it happens
Trigger: Renaming a Java module whose module-info is only available as bytecode: the reference element is a PsiCompiledElement (from a library jar's module-info.class), e.g. rename module refactoring run against a dependency's descriptor, or a reference in a decompiled view.
Common situations: Rename module refactoring applied to a library module; plugins resolving module references from jars and attempting rename; reading module info from binary dependencies (compiled with JPMS) instead of sources.
Related errors
- Cannot rename light class
- Cannot set package name for module declarations
- ''{0}'' is not an identifier.
- Implicitly declared class may have no name
- Can't change name of JShell holder method
AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14).
Data as JSON: /api/errors/549e00320ab2617e.
Report an issue: GitHub.