解析异常java.lang.ClassCastException:org.jetbrains.kotlin.psi.KtFile [英] Resolve exception java.lang.ClassCastException: org.jetbrains.kotlin.psi.KtFile
问题描述
我正在为Intellij Idea编写一个插件,其主要想法是获取Kotlin类的结构:properties,methods等。
在我的 plugin.xml 我添加了以下行< depends> org.jetbrains.kotlin< / depends>
访问类 KtVisitor
我添加到gradle的以下库中:
org.jetbrains.kotlin:kotlin-compiler:1.2.30
一切都在编译时发生异常,当我运行代码并收听动作时,出现以下错误:
java.lang.ClassCastException:org.jetbrains.kotlin.psi.KtFile无法转换为com.intellij.psi.PsiFile
负责监听动作的Mine类如下:
class ConvertAction:AnAction(),DumbAware {
重写fun actionPerformed(event:AnActionEvent?){
val psiFile = event?.getData(PlatformDataKeys.PSI_FIL E)
val s = true
}}
赞赏如何解决这个问题。谢谢。
kotlin-compiler.jar
。添加插件依赖项的正确方法是将以下代码添加到 build.gradle
中:
intellij {
plugins'org.jetbrains.kotlin'
}
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.
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'
}
这篇关于解析异常java.lang.ClassCastException:org.jetbrains.kotlin.psi.KtFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!