作者:平從老 | 来源:互联网 | 2023-08-29 15:45
我正在尝试编写一个插件,以将模拟数据添加到Kotlin项目。第一部分涉及在当前项目中查找所有从特定基类继承的Kotlin类。我希望能够解析这些类以读取注释的值并获取构造函数的结构。然后,此信息将用于将代码添加到项目中,从而将选定类的实例添加到模拟数据库实例中。
我一直在使用PsiViewer插件检查Kotlin类文件中的PSI树。为了访问KtFile,KtClass等,在我的build-gradle文件中向“ org.jetbrains.kotlin:kotlin-compiler-embeddable”添加了一个依赖项。在我尝试运行插件之前,这似乎还可以。我在使用例如psiFile is KtFile
。我仍然可以通过对PsiFile
字段的简单解析来掌握正确的text
实例,但是当我尝试强制转换PsiFile
实例时,出现了以下异常:
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 @2ed18f3d; org.jetbrains.kotlin.psi.KtFile is in unnamed module of loader com.intellij.ide.plugins.cl.PluginClassLoader @4308c14c)
目前,我正在使用IntelliJ IDEA 2019.2.3。尝试使用不同版本的kotlin插件(1.3.31和1.3.41)。在Windows 10上运行。
build.gradle
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.4.11'
id 'org.jetbrains.kotlin.jvm' version '1.3.31'
}
group 'se.winassist'
version '1.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.31"
testCompile group: 'junit',name: 'junit',version: '4.12'
}
// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2019.2.3'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
patchPluginXml {
changeNotes """
Add change notes here.
most HTML tags may be used"""
}
CreateMockdataFromPostgreSQLClass.kt:
import com.intellij.openapi.actionSystem.Anaction
import com.intellij.openapi.actionSystem.AnactionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.ui.Messages
import org.jetbrains.kotlin.psi.KtFile
class CreateMockdataFromPostgreSQLClass: Anaction() {
override fun actionPerformed(e: AnactionEvent) {
val project = e.getRequiredData(CommonDataKeys.PROJECT)
val kotlinFileHandler = KotlinFileHandler(project)
val dbClasses = kotlinFileHandler.getallKotlinDbClasses()
val classnames = dbClasses.map {
val ktFile = it as KtFile
ktFile.name
}.joinToString(separator = "\n")
Messages.showInfoMessage(classnames,"Test")
}
}
KotlinFileHandler.kt :
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
data class KotlinFileHandler(val aProject: Project) {
fun getallKotlinDbClasses(): List {
val roots = ProjectRootManager.getInstance(aProject).contentsourceRoots
val dirs = roots.map { PsiManager.getInstance(aProject).findDirectory(it) }
val result = mutableListOf()
getKotlinDbClassesInPsiDir(result,dirs)
return result.toList()
}
private tailrec fun getKotlinDbClassesInPsiDir(aResult: MutableList,aPsiDirectoryList: List) {
if (aPsiDirectoryList.isEmpty()) return
val subDirectories = mutableListOf()
aPsiDirectoryList.filter { it != null }.forEach {
val children = it!!.children
val files = children.filter {
it is PsiFile && it.language.toString().toLowerCase() == "language: kotlin"
}.map { it as PsiFile }
subDirectories += children.filter { it is PsiDirectory }.map { it as PsiDirectory }
aResult += files.filter {
hasBasePersistenceSuperclass(it) && hasTableNameAnnotation(it)
}.toList()
}
getKotlinDbClassesInPsiDir(aResult,subDirectories)
}
}
我认为我没有正确设置gradle。我已经在广泛地寻找适当的设置,但是找不到有效的解决方案。谁能指导我正确设置gradle?
您是否通过向IntelliJ任务添加插件来检查
intellij {
version '2020.1'
plugins = ['java','Kotlin']
intellij.type = 'IC'
}
请检查并更新