KT1043 implementing problems

Hello Kotlin developers!

I want to implement http://youtrack.jetbrains.com/issue/KT-1043, It’s important for me and for others too, I think.
At first look it should be easy to fix, but I met two hard problems in which I need your help:

  1. When I add PsiDocCommentOwner to JetNamedFunction implementing began to work, but now every time I get this exception:

[2397010]  ERROR - .psi.impl.compiled.ClsFileImpl - /home/ice/projects/sandbox/kotlin/src/KT1043/Kotlin.kt com.intellij.psi.impl.compiled.ClsElementImpl$InvalidMirrorException: stub:PsiPackageStatement:KT1043; mirror:null      at com.intellij.psi.impl.compiled.ClsElementImpl.setMirrorIfPresent(ClsElementImpl.java:311)      at com.intellij.psi.impl.compiled.ClsFileImpl.setMirror(ClsFileImpl.java:252)      at com.intellij.psi.impl.compiled.ClsFileImpl.getMirror(ClsFileImpl.java:295)      at com.intellij.psi.impl.compiled.ClsElementImpl.getMirror(ClsElementImpl.java:156)      at com.intellij.psi.impl.compiled.ClsElementImpl.getText(ClsElementImpl.java:228)      at com.intellij.psi.impl.source.tree.JavaTreeGenerator.generateTreeFor(JavaTreeGenerator.java:59)      at com.intellij.psi.impl.source.tree.ChangeUtil.generateTreeElement(ChangeUtil.java:164)      at com.intellij.psi.impl.source.tree.ChangeUtil.copyToElement(ChangeUtil.java:147)      at com.intellij.psi.impl.source.tree.SharedImplUtil.doReplace(SharedImplUtil.java:195)      at com.intellij.psi.impl.source.JavaStubPsiElement.replace(JavaStubPsiElement.java:150)      at com.intellij.codeInsight.generation.GenerateMembersUtil.copyOrReplaceModifierList(GenerateMembersUtil.java:475)      at com.intellij.codeInsight.generation.GenerateMembersUtil.substituteParameters(GenerateMembersUtil.java:371)      at com.intellij.codeInsight.generation.GenerateMembersUtil.substituteGenericMethod(GenerateMembersUtil.java:244)      at com.intellij.codeInsight.generation.OverrideImplementUtil.overrideOrImplementMethod(OverrideImplementUtil.java:158)      at com.intellij.codeInsight.generation.OverrideImplementUtil.overrideOrImplementMethod(OverrideImplementUtil.java:135)      at com.intellij.codeInsight.generation.OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(OverrideImplementUtil.java:608)      at com.intellij.codeInsight.generation.OverrideImplementUtil$5.run(OverrideImplementUtil.java:468)      at com.intellij.openapi.application.RunResult.run(RunResult.java:37)      at com.intellij.openapi.command.WriteCommandAction$2$1.run(WriteCommandAction.java:126)      at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:1013)      at com.intellij.openapi.command.WriteCommandAction$2.run(WriteCommandAction.java:123)      at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:117)      at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:99)      at com.intellij.openapi.command.WriteCommandAction.performWriteCommandAction(WriteCommandAction.java:120)      at com.intellij.openapi.command.WriteCommandAction.access$000(WriteCommandAction.java:35)      at com.intellij.openapi.command.WriteCommandAction$1.run(WriteCommandAction.java:76)      at com.intellij.openapi.command.WriteCommandAction.execute(WriteCommandAction.java:81)      at com.intellij.codeInsight.generation.OverrideImplementUtil.chooseAndOverrideOrImplementMethods(OverrideImplementUtil.java:465)      at com.intellij.codeInsight.generation.OverrideImplementUtil.chooseAndImplementMethods(OverrideImplementUtil.java:433)      at com.intellij.codeInsight.daemon.impl.quickfix.ImplementMethodsFix.invoke(ImplementMethodsFix.java:93)      at com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement.invoke(LocalQuickFixAndIntentionActionOnPsiElement.java:67)      at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler$2.run(ShowIntentionActionsHandler.java:164)      at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:124)      at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:99)      at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:85)      at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.chooseActionAndInvoke(ShowIntentionActionsHandler.java:183)      at com.intellij.codeInsight.intention.impl.IntentionListStep$2$1.run(IntentionListStep.java:244)      at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.run(LaterInvocator.java:343)      at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:666)      at java.awt.EventQueue.access$400(EventQueue.java:81)      at java.awt.EventQueue$2.run(EventQueue.java:627)      at java.awt.EventQueue$2.run(EventQueue.java:625)      at java.security.AccessController.doPrivileged(Native Method)      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)      at java.awt.EventQueue.dispatchEvent(EventQueue.java:636)      at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:700)      at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:525)      at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:348)      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)      at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) [2397012]  ERROR - .psi.impl.compiled.ClsFileImpl - IntelliJ IDEA (Community Edition) IC-129.SNAPSHOT  Build #IC-129.SNAPSHOT [2397012]  ERROR - .psi.impl.compiled.ClsFileImpl - JDK: 1.6.0_37 [2397013]  ERROR - .psi.impl.compiled.ClsFileImpl - VM: Java HotSpot(TM) Server VM [2397013]  ERROR - .psi.impl.compiled.ClsFileImpl - Vendor: Sun Microsystems Inc. [2397013]  ERROR - .psi.impl.compiled.ClsFileImpl - OS: Linux [2397013]  ERROR - .psi.impl.compiled.ClsFileImpl - Last Action: ShowIntentionActions [2397013]  ERROR - .psi.impl.compiled.ClsFileImpl - Current Command: Implement Methods [2397013]  ERROR - i.impl.compiled.ClsElementImpl - Mirror wasn't set for PsiModifierList, expected text: java.lang.Throwable      at com.intellij.openapi.diagnostic.Logger.error(Logger.java:54)      at com.intellij.psi.impl.compiled.ClsElementImpl.getText(ClsElementImpl.java:234)      at com.intellij.psi.impl.source.tree.JavaTreeGenerator.generateTreeFor(JavaTreeGenerator.java:59)      at com.intellij.psi.impl.source.tree.ChangeUtil.generateTreeElement(ChangeUtil.java:164)      at com.intellij.psi.impl.source.tree.ChangeUtil.copyToElement(ChangeUtil.java:147)      at com.intellij.psi.impl.source.tree.SharedImplUtil.doReplace(SharedImplUtil.java:195)      at com.intellij.psi.impl.source.JavaStubPsiElement.replace(JavaStubPsiElement.java:150)      at com.intellij.codeInsight.generation.GenerateMembersUtil.copyOrReplaceModifierList(GenerateMembersUtil.java:475)      at com.intellij.codeInsight.generation.GenerateMembersUtil.substituteParameters(GenerateMembersUtil.java:371)      at com.intellij.codeInsight.generation.GenerateMembersUtil.substituteGenericMethod(GenerateMembersUtil.java:244)      at com.intellij.codeInsight.generation.OverrideImplementUtil.overrideOrImplementMethod(OverrideImplementUtil.java:158)      at com.intellij.codeInsight.generation.OverrideImplementUtil.overrideOrImplementMethod(OverrideImplementUtil.java:135)      at com.intellij.codeInsight.generation.OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(OverrideImplementUtil.java:608)      at com.intellij.codeInsight.generation.OverrideImplementUtil$5.run(OverrideImplementUtil.java:468)      at com.intellij.openapi.application.RunResult.run(RunResult.java:37)      at com.intellij.openapi.command.WriteCommandAction$2$1.run(WriteCommandAction.java:126)      at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:1013)      at com.intellij.openapi.command.WriteCommandAction$2.run(WriteCommandAction.java:123)      at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:117)      at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:99)      at com.intellij.openapi.command.WriteCommandAction.performWriteCommandAction(WriteCommandAction.java:120)      at com.intellij.openapi.command.WriteCommandAction.access$000(WriteCommandAction.java:35)      at com.intellij.openapi.command.WriteCommandAction$1.run(WriteCommandAction.java:76)      at com.intellij.openapi.command.WriteCommandAction.execute(WriteCommandAction.java:81)      at com.intellij.codeInsight.generation.OverrideImplementUtil.chooseAndOverrideOrImplementMethods(OverrideImplementUtil.java:465)      at com.intellij.codeInsight.generation.OverrideImplementUtil.chooseAndImplementMethods(OverrideImplementUtil.java:433)      at com.intellij.codeInsight.daemon.impl.quickfix.ImplementMethodsFix.invoke(ImplementMethodsFix.java:93)      at com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement.invoke(LocalQuickFixAndIntentionActionOnPsiElement.java:67)      at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler$2.run(ShowIntentionActionsHandler.java:164)      at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:124)      at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:99)      at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:85)      at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.chooseActionAndInvoke(ShowIntentionActionsHandler.java:183)      at com.intellij.codeInsight.intention.impl.IntentionListStep$2$1.run(IntentionListStep.java:244)      at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.run(LaterInvocator.java:343)      at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:666)      at java.awt.EventQueue.access$400(EventQueue.java:81)      at java.awt.EventQueue$2.run(EventQueue.java:627)      at java.awt.EventQueue$2.run(EventQueue.java:625)      at java.security.AccessController.doPrivileged(Native Method)      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)      at java.awt.EventQueue.dispatchEvent(EventQueue.java:636)      at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:700)      at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:525)      at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:348)      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)      at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) [2397015]  ERROR - i.impl.compiled.ClsElementImpl - IntelliJ IDEA (Community Edition) IC-129.SNAPSHOT  Build #IC-129.SNAPSHOT [2397015]  ERROR - i.impl.compiled.ClsElementImpl - JDK: 1.6.0_37 [2397015]  ERROR - i.impl.compiled.ClsElementImpl - VM: Java HotSpot(TM) Server VM [2397015]  ERROR - i.impl.compiled.ClsElementImpl - Vendor: Sun Microsystems Inc. [2397015]  ERROR - i.impl.compiled.ClsElementImpl - OS: Linux [2397015]  ERROR - i.impl.compiled.ClsElementImpl - Last Action: ShowIntentionActions [2397015]  ERROR - i.impl.compiled.ClsElementImpl - Current Command: Implement Methods

The problem(as I found) is in com.intellij.psi.impl.compiled.ClsModifierListImpl - it haven't his mirror. I tried to create JetClsModifierListImpl - same as JetClsMethodImpl, but to use it I should modify ClassBodyCodegen(I think), which is bit hard for me at this moment - can I fix this exception in another way?

  1. Adding PsiDocCommentOwner to JetNamedFunction requires JetModifierList implement com.intellij.psi.PsiModifierList since getModifierList from JetDeclarationStub return JetModifierList.
    PsiModifierList have methods to work with PsiAnnotation’s, but Kotlin PSI annotations elements does not implement this interface. Also this is not the bottom, implementing PsiAnnotation requires another interfaces to be implemented in Kotlin Psi - they all add a bunch of methods to current PSI, some of them are already contained in PSI(but named differently). I decided to stop at implementing PsiModifierList and return empy arrays in methods that works with annotations. Implementing now works fine(except that flag ‘Copy javadoc’ doesn’t work - but I think I’ve missed something). Is this acceptable solution or I should choose another way?

Commit with current state can be found at my fork https://github.com/crazyproger/kotlin/commit/305959f2fae15019b2dc8f9556f381b5dd360536

Thanks in advance!

PsiDocComment owner is a Java-specific class Kotlin PSI should not depend on. This problem requires a fix in IntelliJ IDEA: the dumb cast made there must be guarded.

Can I fill issue to IDEA youtrack project or IDEA will be fixed in current issue?

I don't think we need a separate issue.

Even after I made cast checked(patch is attached) I always get an Exception that was listed above(InvalidMirrorException). Andrey, can you suggest something or at this moment better way do not touch this issue?



[KT1043.patch.zip|attachment](upload://peZigJtoqbubAant2OZd08MqOww.zip) (5.57 KB)

Can't say off the top of my head. It will take some debugging to figure out what to do with this exception.