3

codegen IDEA-Plugin を開発しようとしています。このプラグインはKtClass継承を分析し、すべての継承クラスのフル ネーム (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()
  1. superTypeListEntry.typeAsUserType.referenceExpression.getReferencedName() -return->"Config"
  2. 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からすべての継承クラスのフルネームを取得するにはどうすればよいですか? ありがとうございました!

4

1 に答える 1