如何从 RoundEnvironment 为自定义注释处理器获取正确的 kotlin 类型?

How to get proper kotlin types from RoundEnvironment for custom Annotation processor?

假设我的注释处理器的处理函数如下所示

override fun process(
        annotations: MutableSet<out TypeElement>,
        roundEnv: RoundEnvironment
    ): Boolean {
        try {
            roundEnv.getElementsAnnotatedWith(CustomAnnotation::class.java)
                .mapNotNull {
                    if (it.kind != ElementKind.INTERFACE) {
                        printError(
                            "Only interfaces can be annotated with " +
                                    MapperConfig::class.java.simpleName
                        )
                        null
                    } else {
                        it as TypeElement
                    }
                }.forEach {
                    processMapperConfigInterface(it, roundEnv)
                }
        } catch (ex: Exception) {
            messager.printError(ex.message!!)
        }
        return true
    }

roundEnv.getElementsAnnotatedWith returns 我遇到了没有任何 kotlin 类型信息的 java 元素,如何使用注解处理来获取正确的 kotlin 类型信息?

我遇到了同样的问题,我能想到的唯一解决方案是使用 kotlinpoet-metadata API.

解析元素的元数据

重要说明: kotlinpoet-metadata 仍处于早期生产阶段,它本身基于实验性 kotlinx-metadata 库,因此将来可能会出现问题。不过目前在稳定版中使用的是Moshi Kotlin code generator.

首先,确保您已将以下内容添加到 build.gradle 中的依赖项中:

dependencies {
    implementation 'com.squareup:kotlinpoet:1.7.1'
    implementation 'com.squareup:kotlinpoet-metadata:1.7.1'`
}

1.7.1 是截至今天的最新 KotlinPoet 版本。

您可以通过 KmClass 获取 Kotlin 类型信息,该信息是根据元素的元数据构建的。这是一个使用 KmClass 的示例,这是您需要在代码中更改的内容:

override fun process(
    annotations: MutableSet<out TypeElement>,
    roundEnv: RoundEnvironment
): Boolean {
    try {
        roundEnv.getElementsAnnotatedWith(CustomAnnotation::class.java)
            .mapNotNull {
                if (it.kind != ElementKind.INTERFACE) {
                    printError(
                        "Only interfaces can be annotated with " +
                                MapperConfig::class.java.simpleName
                    )
                    null
                } else {
                    it as TypeElement
                }
            }.forEach {
                val typeMetadata = it.getAnnotation(Metadata::class.java) ?: return@forEach
                var kmClass = typeMetadata.toImmutableKmClass()
                // HERE: kmClass should contain all the Kotlin type information.
            }
    } catch (ex: Exception) {
        messager.printError(ex.message!!)
    }
    return true
}