-->

Resolve exception java.lang.ClassCastException: or

2019-03-04 11:04发布

问题:

I'm writing a plugin for Intellij Idea, where the main idea is to get the structure of a Kotlin class as: properties, methods, etc. In my plugin.xml I added the following line <depends>org.jetbrains.kotlin</depends>

And as a library to access the class KtVisitor I added to gradle the following library: org.jetbrains.kotlin:kotlin-compiler:1.2.30

Everything is compiling with exception, that when I'm running the code and listen to the action, I'm getting the following error: java.lang.ClassCastException: org.jetbrains.kotlin.psi.KtFile cannot be cast to com.intellij.psi.PsiFile

Mine class responsible for listening to the action is following:

class ConvertAction: AnAction(), DumbAware {
override fun actionPerformed(event: AnActionEvent?) {
    val psiFile = event?.getData(PlatformDataKeys.PSI_FILE)
    val s = true
}}

Appreciate any help how to resolve this issue. Thank you.

回答1:

The ClassCastException happens because IDEA loads two copies of the Kotlin plugin classes, one from the actual Kotlin plugin and another from the kotlin-compiler.jar that you're providing. The correct way to add the plugin dependency is to add the following to your build.gradle:

intellij {
    plugins 'org.jetbrains.kotlin'
}