Cannot Cast PsiElement to KtClass Follow
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>"""
}
Please sign in to leave a comment.
Did you declare dependency on Kotlin plugin in plugin.xml? http://www.jetbrains.org/intellij/sdk/docs/basics/plugin_structure/plugin_dependencies.html#dependency-declaration-in-pluginxml
I did, here's my plugin.xml
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
I've replaced it to this one, but unfortunately still getting the same error
Please provide complete project sources
https://github.com/DjakaTechnology/plugin-experiment Here's the complete sources
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.
Thank youu, it works like a charm. I've been stuck in this problem for days.
Hello Mr Yann Cebron I cannot find
This is my build.gradel
Nvh Haui,
You have to add a Java dependency in your Gradle:
and in plugin.xml:
Check the Compatibility with Multiple Products section in our docs for more details.
after I add
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
Update
in plugin.xml I added
in build.grable
I added
my language is Java and i can't import import org.jetbrains.kotlin.com.intellij.psi.PsiElement
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.
I think it's not relate my problem. current i can not get kt class from PsiElement. This is my code
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 ?
Please link your full plugin sources, it's impossible to guess from snippets what is happening.
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
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