KtFile cannot be cast to KtFile: Problem with Kotlin PSI while plugin development

21 views Asked by At

Please, help me identify my mistake. I'm developing the custom plugin for Android Studio and while trying to process the Kotlin file, received from the Action I receive the following error:

java.lang.ClassCastException: class org.jetbrains.kotlin.psi.KtFile cannot be cast to class org.jetbrains.kotlin.psi.KtFile (org.jetbrains.kotlin.psi.KtFile is in unnamed module of loader com.intellij.ide.plugins.cl.PluginClassLoader @6bf6bf87; org.jetbrains.kotlin.psi.KtFile is in unnamed module of loader com.intellij.ide.plugins.cl.PluginClassLoader @62ccc687) at com.ordinarythinker.jcat.ProcessKtFileAction.actionPerformed(ProcessKtFileAction.kt:24)

The exception occurs in the indicated line:

import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtNamedFunction

class ProcessKtFileAction : AnAction() {
    override fun actionPerformed(e: AnActionEvent) {
        val project: Project = e.project ?: return
        val file: VirtualFile? = e.getData(CommonDataKeys.VIRTUAL_FILE)

        if (file != null && file.isValid) {
            val psiFile: PsiFile? = e.getData(CommonDataKeys.PSI_FILE)

            try {
                val ktFile = psiFile as KtFile // exception is thrown here
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
    // some other code
}

The problem is in different loaders of the class and, as the result, in impossibility to convert it to the KtFile type, though debugger shows that the PsiFile I received from action is KtFile.

TLDR: if you wish to reproduce the error, you can use my repository on github

I followed instructions from Intellij docs and from this thread.

Here is my build.gradle.kts:

plugins {
   id("org.jetbrains.intellij") version "1.16.0"
   id("org.jetbrains.kotlin.jvm") version "1.9.21"
}
group = "com.ordinarythinker"
version = "1.0-SNAPSHOT"
repositories {
   mavenCentral()
}
dependencies {
   implementation(kotlin("compiler-embeddable", "1.9.21"))
   implementation(kotlin("reflect"))
}
apply(plugin = "org.jetbrains.intellij")
apply(plugin = "kotlin")
intellij {
   version.set("2022.3.1")
   type.set("IC")
   plugins.set(listOf("android", "Kotlin"))
}
tasks {
   withType<JavaCompile> {
       sourceCompatibility = "17"
       targetCompatibility = "17"
   }
   withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
       kotlinOptions.jvmTarget = "17"
   }
   runIde {
       ideDir.set(file("C:\\Program Files\\Android\\Android Studio2"))
   }
   patchPluginXml {
       sinceBuild.set("223")
       untilBuild.set("241.*")
   }
   signPlugin {
       certificateChain.set(System.getenv("CERTIFICATE_CHAIN"))
       privateKey.set(System.getenv("PRIVATE_KEY"))
       password.set(System.getenv("PRIVATE_KEY_PASSWORD"))
   }
   publishPlugin {
       token.set(System.getenv("PUBLISH_TOKEN"))
   }
}

To avoid clash in stblib versions, according to docs, I excluded stdlib, provided by Intellij plugin by default, in my gradle.properties file:

kotlin.stdlib.default.dependency = false

I also indicated that my plugin relies on the org.jetbrains.kotlin by adding <depends>org.jetbrains.kotlin</depends> to plugin.xml:

<idea-plugin>
    <id>com.ordinarythinker.jCat</id>

    <name>jCat</name>

    <vendor email="" url="https://github.com/ordinarythinker">Andrii D.</vendor>

    <description><![CDATA[
    JCat is a plugin for automated tests generation for Composable functions<br>
    <em>Currently is underway the development</em>
  ]]></description>

    <depends>com.intellij.modules.platform</depends>

    <extensions defaultExtensionNs="com.intellij">
    </extensions>

    <actions>
        <action
                id="ProcessKtFileAction"
                class="com.ordinarythinker.jcat.ProcessKtFileAction"
                text="Generate Test"
                description="Generate test">
            <add-to-group group-id="EditorPopupMenu" anchor="last"/>
        </action>
    </actions>

    <depends>org.jetbrains.kotlin</depends>
</idea-plugin>

I tried adding org.jetbrains.kotlin to plugin in intellij block as described here, but the problem remains. And the actions from this don't help either.

Can anyone point me at what am I missing? Thanks in advance for any help

0

There are 0 answers