{"record":{"id":"549e00320ab2617e","repo":"JetBrains/intellij-community","slug":"psi-error-attempt-to-edit-class-file","errorCode":"psi.error.attempt.to.edit.class.file","errorMessage":"Cannot modify compiled element in file ''{0}''","messagePattern":"Cannot modify compiled element in file ''(.+?)''","errorType":"exception","errorClass":"IncorrectOperationException","httpStatus":null,"severity":"error","filePath":"java/java-psi-impl/src/com/intellij/psi/impl/source/PsiJavaModuleReferenceImpl.java","lineNumber":51,"sourceCode":"  public @NotNull String getCanonicalText() {\n    return getElement().getReferenceText();\n  }\n\n  @Override\n  public PsiJavaModule resolve() {\n    return (PsiJavaModule)super.resolve();\n  }\n\n  @Override\n  public ResolveResult @NotNull [] multiResolve(boolean incompleteCode) {\n    return ResolveCache.getInstance(getProject()).resolveWithCaching(this, Resolver.INSTANCE, false, incompleteCode);\n  }\n\n  @Override\n  public PsiElement handleElementRename(@NotNull String newName) throws IncorrectOperationException {\n    PsiJavaModuleReferenceElement element = getElement();\n    if (element instanceof PsiCompiledElement) {\n      throw new IncorrectOperationException(JavaPsiBundle.message(\"psi.error.attempt.to.edit.class.file\", element.getContainingFile()));\n    }\n    PsiElement newElement = PsiElementFactory.getInstance(element.getProject()).createModuleReferenceFromText(newName, null);\n    return element.replace(newElement);\n  }\n\n  private Project getProject() {\n    return getElement().getProject();\n  }\n\n  private static class Resolver implements ResolveCache.PolyVariantResolver<PsiJavaModuleReferenceImpl> {\n    private static final ResolveCache.PolyVariantResolver<PsiJavaModuleReferenceImpl> INSTANCE = new Resolver();\n\n    @Override\n    public ResolveResult @NotNull [] resolve(@NotNull PsiJavaModuleReferenceImpl reference, boolean incompleteCode) {\n      PsiJavaModuleReferenceElement refElement = reference.getElement();\n      PsiFile file = refElement.getContainingFile();\n      String moduleName = reference.getCanonicalText();\n","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiJavaModuleReferenceImpl.java#L33-L69","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nmoduleRef.handleElementRename(newName); // module-info.class in jar -> throws\n\n// after\nPsiElement elem = moduleRef.getElement();\nif (elem instanceof PsiCompiledElement) {\n  // cannot edit bytecode: attach sources or rename the reference usage instead\n} else {\n  moduleRef.handleElementRename(newName);\n}","handlingStrategy":"type-guard","validationCode":"if (moduleRef.getElement() instanceof PsiCompiledElement) return; // bytecode not editable","typeGuard":"static boolean isEditableModuleRef(PsiJavaModuleReference r) { return !(r.getElement() instanceof PsiCompiledElement); }","tryCatchPattern":"try { moduleRef.handleElementRename(n); } catch (IncorrectOperationException e) { /* attach sources or edit own code */ }","preventionTips":["Attach library sources so module references resolve to editable PSI.","Rename your own requires statements, not third-party module names."],"tags":["java","psi","jpms","module-info","compiled-code","rename"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}