Bundling org.jetbrains.kotlin:kotlin-scripting-jsr223 into a plugin

已回答

Hello,

I am trying to create a plugin for IntelliJ which parses some KotlinScripts (kts) during runtime. Unfortunately, as soon as I add the following dependency to the plugin and run the IDE, I run into tons of ClassCastExceptions from the IntelliJ class files.

implementation("org.jetbrains.kotlin:kotlin-scripting-jsr223")

Caused by: java.lang.ExceptionInInitializerError

at myplugin.CustomFileType.<init>(CustomFileType.kt:10)
at com.intellij.serviceContainer.ComponentManagerImpl.instantiateClass(ComponentManagerImpl.kt:830)
... 29 more
Caused by: java.lang.ClassCastException: class org.jetbrains.kotlin.idea.KotlinLanguage cannot be cast to class com.intellij.lang.Language (org.jetbrains.kotlin.idea.KotlinLanguage is in unnamed module of loader com.intellij.ide.plugins.cl.PluginClassLoader @3d680b5a; com.intellij.lang.Language is in unnamed module of loader com.intellij.util.lang.PathClassLoader @335eadca)
at myplugin.CustomLanguage.<init>(CustomLanguage.kt:10)
Caused by: java.lang.ClassCastException: class org.jetbrains.kotlin.idea.KotlinLanguage cannot be cast to class com.intellij.lang.Language (org.jetbrains.kotlin.idea.KotlinLanguage is in unnamed module of loader com.intellij.ide.plugins.cl.PluginClassLoader @3d680b5a; com.intellij.lang.Language is in unnamed module of loader com.intellij.util.lang.PathClassLoader @335eadca)

It seems like the bundled KotlinScript modules are the cause of class exceptions. I have already tried to load the dependency in its own classloader but I wasn't able to get it working because the script engine

ScriptEngineManager().getEngineByExtension("kts")

always returned null.

My question:

How can I correctly bundle org.jetbrains.kotlin:kotlin-scripting-jsr223 into a IntelliJ IDEA plugin, so I can parse kts scripts during runtime?

 

0

 With implementation, you're bundling the Kotlin library within your plugin, so KotlinLanguage (and the base Language) are in a specific version, which doesn't match the one from the runtime.

Try attaching mentioned library with compileOnly instead.

0

Thank you for your reply. I have tried using compileOnly but then some of the required files for the Kotlin Script compiler seem to be missing.

val scriptEngine: ScriptEngine = ScriptEngineManager().getEngineByExtension("kts")
// scriptEngine is null :(

Does IntelliJ IDEA already load all classes of the embedded script compiler per default?

0

Maybe, my question is actually:

How can I use the Kotlin ScriptEngine in a IntelliJ IDEA plugin? 

0

It does, but you need to set proper dependencies to Kotlin at first. Do you have it added to your plugin.xml file?

0

I have set the following in the plugin.xml. Is there anything else I am missing?

<depends>org.jetbrains.kotlin</depends>
0

I'll post a new question with my plugin details.

0

请先登录再写评论。