Cannot Cast PsiElement to KtClass

Answered

I'm learning to create a plugin for IntelliJ but I'm stuck at PsiElement. I can't cast PsiElement to KtClass even though it also a KtClass.

Here's the error log

java.lang.ClassCastException: org.jetbrains.kotlin.psi.KtClass cannot be cast to org.jetbrains.kotlin.psi.KtClass
at action.MyAction.getPsiClassFromEvent(MyAction.kt:32)
at action.MyAction.actionPerformed(MyAction.kt:21)
at com.intellij.openapi.actionSystem.ex.ActionUtil$1.run(ActionUtil.java:261)
at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:278)
at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAwareWithCallbacks(ActionUtil.java:252)
at com.intellij.ui.popup.ActionPopupStep.performAction(ActionPopupStep.java:200)
at com.intellij.ui.popup.ActionPopupStep.performAction(ActionPopupStep.java:190)
at com.intellij.ui.popup.ActionPopupStep.lambda$onChosen$2(ActionPopupStep.java:184)
at com.intellij.openapi.application.TransactionGuardImpl.performUserActivity(TransactionGuardImpl.java:195)
at com.intellij.ui.popup.AbstractPopup.lambda$dispose$13(AbstractPopup.java:1416)
at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:3082)
at com.intellij.ide.IdeEventQueue.ifFocusEventsInTheQueue(IdeEventQueue.java:160)
at com.intellij.ide.IdeEventQueue.executeWhenAllFocusEventsLeftTheQueue(IdeEventQueue.java:112)
at com.intellij.openapi.wm.impl.FocusManagerImpl.doWhenFocusSettlesDown(FocusManagerImpl.java:179)
at com.intellij.openapi.wm.impl.IdeFocusManagerImpl.doWhenFocusSettlesDown(IdeFocusManagerImpl.java:58)
at com.intellij.ui.popup.AbstractPopup.dispose(AbstractPopup.java:1412)
at com.intellij.ui.popup.WizardPopup.dispose(WizardPopup.java:160)
at com.intellij.ui.popup.list.ListPopupImpl.dispose(ListPopupImpl.java:306)
at com.intellij.ui.popup.PopupFactoryImpl$ActionGroupPopup.dispose(PopupFactoryImpl.java:279)
at com.intellij.openapi.util.Disposer$1.execute(Disposer.java:48)
at com.intellij.openapi.util.Disposer$1.execute(Disposer.java:44)
at com.intellij.openapi.util.objectTree.ObjectNode$1.execute(ObjectNode.java:136)
at com.intellij.openapi.util.objectTree.ObjectNode$1.execute(ObjectNode.java:105)
at com.intellij.openapi.util.objectTree.ObjectTree.executeActionWithRecursiveGuard(ObjectTree.java:214)
at com.intellij.openapi.util.objectTree.ObjectNode.execute(ObjectNode.java:105)
at com.intellij.openapi.util.objectTree.ObjectTree.executeAll(ObjectTree.java:162)
at com.intellij.openapi.util.Disposer.dispose(Disposer.java:129)
at com.intellij.openapi.util.Disposer.dispose(Disposer.java:125)
at com.intellij.ui.popup.WizardPopup.disposeAllParents(WizardPopup.java:263)
at com.intellij.ui.popup.list.ListPopupImpl.handleNextStep(ListPopupImpl.java:442)
at com.intellij.ui.popup.list.ListPopupImpl._handleSelect(ListPopupImpl.java:395)
at com.intellij.ui.popup.list.ListPopupImpl.handleSelect(ListPopupImpl.java:336)
at com.intellij.ui.popup.list.ListPopupImpl$1.actionPerformed(ListPopupImpl.java:249)
at com.intellij.ui.popup.WizardPopup.proceedKeyEvent(WizardPopup.java:378)
at com.intellij.ui.popup.WizardPopup.dispatch(WizardPopup.java:358)
at com.intellij.ui.popup.PopupDispatcher.dispatchKeyEvent(PopupDispatcher.java:114)
at com.intellij.ui.popup.PopupDispatcher.dispatch(PopupDispatcher.java:150)
at com.intellij.ide.IdePopupManager.dispatch(IdePopupManager.java:86)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:657)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:375)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)



Here's my function

private fun getPsiClassFromEvent(e: AnActionEvent): KtClass? {
val psiElement = e.getData(
LangDataKeys.PSI_ELEMENT
) ?: return null

return psiElement as KtClass
}

here's my gradle:

plugins {
id 'org.jetbrains.intellij' version '0.4.15'
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
}

group 'intellijtemplate.djaka.id'
version '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

apply plugin: 'org.jetbrains.intellij'
apply plugin: 'kotlin'

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61"
}

// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2019.1.4'
plugins 'org.jetbrains.kotlin:1.3.11-release-IJ2018.3-1'
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
}
17 comments
Comment actions Permalink

I found the following problems:

1) using "import org.jetbrains.kotlin.com.intellij.psi.PsiElement" from Kotlin Plugin instead of "com.intellij.psi.PsiElement" in KtClassHelper

2) in build.gradle, "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61" was included into project's instead of buildscript classpath

Please fix (1) and change your build.gradle file to the version posted below. Please see http://www.jetbrains.org/intellij/sdk/docs/tutorials/kotlin.html in SDK Docs as reference.

 

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61"
}
}

plugins {
id 'org.jetbrains.intellij' version '0.4.15'
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
}

group 'intellijtemplate.djaka.id'
version '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

apply plugin: 'org.jetbrains.intellij'
apply plugin: 'kotlin'

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2019.1.4'
plugins 'Kotlin'
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
}
1
Comment actions Permalink

There is no dedicated tutorial for Kotlin's PSI.

You can find plugins targeting Kotlin in some way with sources available here https://plugins.jetbrains.com/search?search=kotlin&shouldHaveSource=true

Also, make use of https://plugins.jetbrains.com/plugin/227-psiviewer to understand the PSI

1
Comment actions Permalink

I did, here's my plugin.xml

 

<idea-plugin>
<id>intellijtemplate.djaka.id.intellijtemplate</id>
<name>Intelij Template</name>
<vendor email="test@gmail.com" url="http://www.djaka.id">Djaka Pradana</vendor>

<description>It's just a little short experiment hehehe</description>

<!--depend on kotlin plugin-->
<depends>org.jetbrains.kotlin</depends>

<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<!-- uncomment to enable plugin in all products
<depends>com.intellij.modules.lang</depends>
-->

<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
</extensions>

<actions>
<action id="MyAction" class="action.MyAction" text="MyAction"
description="MyAction">
<add-to-group group-id="GenerateGroup" anchor="last"/>
</action>
</actions>
</idea-plugin>
0
Comment actions Permalink

You are referring to a different Kotlin plugin version than the one bundled with 2019.1.4, this will not work.

Please try replacing in build.gradle

plugins 'Kotlin'

instead of

 plugins 'org.jetbrains.kotlin:1.3.11-release-IJ2018.3-1'
0
Comment actions Permalink

I've replaced it to this one, but unfortunately still getting the same error

intellij {
version '2019.1.4'
plugins 'Kotlin'
}
0
Comment actions Permalink

Please provide complete project sources

0
Comment actions Permalink

Thank youu, it works like a charm. I've been stuck in this problem for days.

0
Comment actions Permalink

Hello Mr Yann Cebron I cannot find

import org.jetbrains.kotlin.com.intellij.psi.PsiElement;

This is my build.gradel

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.11"
}
}
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.4.21'
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
}

apply plugin: "kotlin"
apply plugin: 'org.jetbrains.intellij'

group 'com.test.plugin'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
intellij {
plugins "Kotlin"
version '2018.3.6'
alternativeIdePath 'C:\\Program Files\\Android\\Android Studio'
updateSinceUntilBuild false
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
}
0
Comment actions Permalink

Nvh Haui,

You have to add a Java dependency in your Gradle:

intellij {
// ...
plugins = ['java', 'Kotlin']
}

and in plugin.xml:

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

Check the Compatibility with Multiple Products section in our docs for more details.

0
Comment actions Permalink

after I add

plugins = ['java', 'Kotlin'] 
I got error Cannot find builtin plugin java for IDE: C:\Users\hung.nv14\.gradle\caches\modules-2\files-2.1\com.jetbrains.intellij.idea\ideaIC\2018.3.6\be4a083c7dca1159d340fd6dcacc4a78646acfb4\ideaIC-2018.3.6

Should I add plugin in setting -> plugins -> Java ?
I don't know exactly plugin i have to install. Please help me fix it Thank 

0
Comment actions Permalink

Update
in plugin.xml I added

<depends>org.jetbrains.kotlin</depends>
<depends>com.intellij.modules.java</depends>

in build.grable
I added 

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.11"
testCompile group: 'junit', name: 'junit', version: '4.12'
}

// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2020.2'
plugins = ['java', 'Kotlin']
alternativeIdePath 'C:\\Program Files\\Android\\Android Studio'
updateSinceUntilBuild false
}

my language is Java and i can't import import org.jetbrains.kotlin.com.intellij.psi.PsiElement 

0
Comment actions Permalink

Nvh haui, please follow guide from https://jetbrains.org/intellij/sdk/docs/products/android_studio.html

you should ideally not use "alternativeIdePath" and it cannot be mixed with "version" at the same time anyway.

0
Comment actions Permalink

I think it's not relate my problem. current i can not get kt class from PsiElement. This is my code

public static KtClass getKtClassForElement(@NotNull PsiElement psiElement) {
System.out.println("getKtClassForElement" + "name : " + psiElement.toString());
if (psiElement instanceof KtLightElement) {
System.out.println("psiElement instanceof KtLightElement");
PsiElement origin = ((KtLightElement) psiElement).getKotlinOrigin();
if (origin != null) {
return getKtClassForElement(origin);
} else {
return null;
}

} else if (psiElement instanceof KtClass && !((KtClass) psiElement).isEnum() &&
!((KtClass) psiElement).isInterface() &&
!((KtClass) psiElement).isAnnotation() &&
!((KtClass) psiElement).isSealed()) {
System.out.println("psiElement instanceof KtClass ");
return (KtClass) psiElement;

} else {
System.out.println("Else");
PsiElement parent = psiElement.getParent();
if (parent == null) {
return null;
} else {
return getKtClassForElement(parent);
}
}
}

It's always run in System.out.println("Else") and return null.
Should I using "import org.jetbrains.kotlin.com.intellij.psi.PsiElement" from Kotlin Plugin instead of "com.intellij.psi.PsiElement" in KtClassHelper ?

 

0
Comment actions Permalink

Please link your full plugin sources, it's impossible to guess from snippets what is happening.

0
Comment actions Permalink


In my company I can not upload source. Do you have any repository and tutorial about KtClass.

I want to create a plugin which can create test for kotlin file. I want to deep search in KtClass but I can not find some tutorial for it.
Could you give me some guide or tutorial ?


Thank you in advance

0

Please sign in to leave a comment.