解析异常java.lang.ClassCastException:org.jetbrains.kotlin.psi.KtFile [英] Resolve exception java.lang.ClassCastException: org.jetbrains.kotlin.psi.KtFile

查看:2356
本文介绍了解析异常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
}}

赞赏如何解决这个问题。谢谢。

解决方案

ClassCastException 是因为IDEA加载了两份Kotlin插件类,一个来自实际的Kotlin插件,另一个来自您提供的 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆