Hi,
I’m trying to extend a plugin for files written in Kotlin. The plugin is supposed to modify Kotlin classes. For this purpose I use methods like
PsiClass.addBefore() PsiClass.addAfter()
I have no problem in getting PsiClass instances, their methods of Kotlin files, but once it comes down to using the above mentioned method, I get the exception
IncorrectOperationException
The plugin handles correctly Java classes, but Kotlin ones are always giving this exception when I use the above methods.
Could somebody give me a hint how to deal with Kotlin classes as well?
Thanks!
You need to get the underlying Kotlin class instance (check if the PsiClass is an instance of KtLightElement, and if it is, access its kotlinOrigin property) and to add the PSI elements (created using KtPsiFactory, not the Java PSI factory) to the returned KtClass.
when i put kotlin-plugin.jar in my project dir ‘libs’, i can use KtLightElement bu find exception java.lang.ClassCastException: org.jetbrains.kotlin.asJava.classes.KtLightClassImpl cannot be cast to org.jetbrains.kotlin.asJava.classes.KtLightClassImpl: Ljava/lang/ClassCastException;
I just wanted to mention that I am not making use of any of those methods myself. Instead, I wanted to auto generate an OnClick function defined in my layout xml file and it threw the same error.