{"record":{"id":"8e902ac8aebccafe","repo":"JetBrains/intellij-community","slug":"cannot-rename-light-class","errorCode":null,"errorMessage":"Cannot rename light class","messagePattern":"Cannot rename light class","errorType":"exception","errorClass":"IncorrectOperationException","httpStatus":null,"severity":"error","filePath":"java/java-psi-impl/src/com/intellij/psi/impl/light/LightPsiClassBase.java","lineNumber":218,"sourceCode":"\n  @Override\n  public boolean isInheritor(@NotNull PsiClass baseClass, boolean checkDeep) {\n    return InheritanceImplUtil.isInheritor(this, baseClass, checkDeep);\n  }\n\n  @Override\n  public boolean isInheritorDeep(@NotNull PsiClass baseClass, @Nullable PsiClass classToByPass) {\n    return InheritanceImplUtil.isInheritorDeep(this, baseClass, classToByPass);\n  }\n\n  @Override\n  public @NotNull Collection<HierarchicalMethodSignature> getVisibleSignatures() {\n    return PsiSuperMethodImplUtil.getVisibleSignatures(this);\n  }\n\n  @Override\n  public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {\n    throw new IncorrectOperationException(\"Cannot rename light class\");\n  }\n\n  @Override\n  public @Nullable PsiDocComment getDocComment() {\n    return null;\n  }\n\n  @Override\n  public boolean isDeprecated() {\n    return false;\n  }\n\n  @Override\n  public boolean hasTypeParameters() {\n    return PsiImplUtil.hasTypeParameters(this);\n  }\n\n  @Override","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/java-psi-impl/src/com/intellij/psi/impl/light/LightPsiClassBase.java#L200-L236","documentation":"LightPsiClassBase is the base for synthetic, in-memory 'light' classes that are not backed by source text (e.g. classes inferred from class files, cache delegates, or synthesized wrappers). Because a light class has no source element to edit, its setName permanently throws IncorrectOperationException(\"Cannot rename light class\").","triggerScenarios":"Invoking PsiClass.setName on any LightPsiClassBase subclass: light classes created from compiled code (ClsClassImpl-style wrappers), PsiTypeParameter-like synthetics, or custom LightClass implementations; typically reached from a rename refactoring or direct setName call.","commonSituations":"A rename refactoring resolves a reference to a class without source (from a library .class or .jar) and tries to rename it; a plugin iterates classes and calls setName without checking canNavigate/to the source; tests that pick up light mirrors of source classes.","solutions":["Before renaming, resolve to the source element: check element instanceof PsiCompiledElement or navigate with JavaPsiFacade.findClass and prefer the source class; skip if only a compiled/light mirror exists.","Guard with a check such as !(aClass instanceof LightPsiClassBase) (or !aClass.isPhysical() / instanceof PsiCompiledElement) before calling setName.","In rename refactors, use JavaRenameUtil/name suggesters that already filter non-renamable elements, or mark the handler unavailable for light classes.","If you own the light class, override setName to redirect the rename to the underlying source class."],"exampleFix":"// before\nclassRef.resolve().setName(newName); // resolves to LightPsiClassBase -> throws\n\n// after\nPsiElement target = classRef.resolve();\nif (target instanceof PsiCompiledElement || target instanceof LightPsiClassBase) return; // not renamable\ntarget.setName(newName);","handlingStrategy":"type-guard","validationCode":"if (aClass instanceof LightPsiClassBase) return; // synthetic, not renamable","typeGuard":"static boolean isRenamableClass(PsiClass c) {\n  return !(c instanceof LightPsiClassBase) && !(c instanceof PsiCompiledElement) && c.getNameIdentifier() != null;\n}","tryCatchPattern":"try { aClass.setName(n); } catch (IncorrectOperationException e) { /* skip light class, log at most */ }","preventionTips":["Resolve to source elements before renames; skip PsiCompiledElement.","Filter synthetic (light) elements out of refactoring pipelines.","Prefer the platform Rename refactoring over direct setName."],"tags":["java","psi","light-elements","rename","compiled-code"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}