我正在尝试开发一个 codegen IDEA-Plugin。这个插件应该分析KtClass
Inheritance 并获取所有继承类的全名(如 com.example.config.TestConfig)
我试图通过查看 PsiViewer 找到任何有用的信息。我发现 KtClass 的所有继承信息都存储在 中KtSuperTypeEntry
,我尽力获取继承类的全名。
对于类Dest
:
data class Dest(
val stringValue: String = "123",
override val stringConfig: String = "123",
override val iConfigStr: String = "123",
val b: B = B(),
val c: List<List<Set<Map<String, out Any?>>>> = listOf(),
val config: Config? = Config()
) : Config()
- superTypeListEntry.typeAsUserType.referenceExpression.getReferencedName() -return->
"Config"
- superTypeListEntry.importReceiverMembers() -return->
null
看似 SuperTypeListEntry 只包含继承类简单名称信息。
我也尝试通过 KtFile 查找继承类全名,但不知道何时将继承类作为通配符导入到此 KtFile 中:
fun KtSuperTypeListEntry.getType(ktFile: KtFile): String {
val simpleName = superTypeListEntry.text
// try to find by declared KtClass ...
ktFile.children.filterIsInstance<KtClass>().filter { it.name == simpleName }
// try to find by import ...
ktFile.importDirectives.filter { it.importPath.toString().contains(simpleName) }
// try to find by import wildcards ...
ktFile.importDirectives.filter { it.importPath.toString().endWith("*") }.forEach {
val split = it.importPath.split(".")
split.set(split.size - 1, simpleName)
val maybeFullName = split.joinToString(",") { it }
// confused on how to detect "maybeFullName" is correct ...
}
}
- 问题
如何从 Kotlin Psi API 检索所有继承类全名?谢谢!