0

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 Answer 1

2

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'
}
Sign up to request clarification or add additional context in comments.

3 Comments

It worked like a charm! Maybe jus to make a point about creating the plugin project using Gradle instead of DevKit. By the way, do you know a solution for DevKit?
For other who can't resolve the issue by using the line above, try to use something like that org.jetbrains.kotlin:1.2.21-release-IJ2017.3-1@ Thank you @yole for your help!
Does this work for Android Studio too?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.