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

如何从RoundEnvironment为自定义注释处理器获取适当的kotlin类型?,kotlin,annotation-processing,kotlinpoet,Kotlin,Annotation Processing,Kotlinpoet,假设注释处理器的处理函数如下所示 override fun process( annotations: MutableSet<out TypeElement>, roundEnv: RoundEnvironment ): Boolean { try { roundEnv.getElementsAnnotatedWith(CustomAnnotation::class.java)

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

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:RoundEnvironment
):布尔值{
试一试{
roundEnv.getElementsAnnotatedWith(CustomAnnotation::class.java)
.mapNotNull{
if(it.kind!=ElementKind.INTERFACE){
打印错误(
“只有接口可以用注释”+
MapperConfig::class.java.simpleName
)
无效的
}否则{
它作为类型元素
}
}弗雷奇先生{
processMapperConfigInterface(it、roundEnv)
}
}捕获(例如:异常){
messager.printError(例如message!!)
}
返回真值
}

roundEnv.getElementsAnnotatedWith
返回没有任何kotlin类型信息的java元素,如何使用注释处理来获取正确的kotlin类型信息?

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

重要提示:
kotlinpoet元数据
仍处于早期生产阶段,其本身基于实验性的
kotlinx元数据
库,因此将来可能会出现问题。但是,它目前用于稳定版本的

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

dependencies {
    implementation 'com.squareup:kotlinpoet:1.7.1'
    implementation 'com.squareup:kotlinpoet-metadata:1.7.1'`
}
1.7.1是目前最新的KotlinPoet版本

您可以通过从元素的元数据构造的
KmClass
获取Kotlin类型信息。下面是一个关于使用的示例,下面是您需要在代码中更改的内容:

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
}
覆盖有趣的过程(
注释:可变集,
roundEnv:RoundEnvironment
):布尔值{
试一试{
roundEnv.getElementsAnnotatedWith(CustomAnnotation::class.java)
.mapNotNull{
if(it.kind!=ElementKind.INTERFACE){
打印错误(
“只有接口可以用注释”+
MapperConfig::class.java.simpleName
)
无效的
}否则{
它作为类型元素
}
}弗雷奇先生{
val typeMetadata=it.getAnnotation(元数据::class.java)?:return@forEach
var kmClass=typeMetadata.toImmutableKmClass()
//这里:kmClass应该包含所有Kotlin类型信息。
}
}捕获(例如:异常){
messager.printError(例如message!!)
}
返回真值
}

太棒了,这正是我错过的,非常感谢@如果你对Kapt的工作还不算太远,你也可以考虑看一下Ksp:这就是我最终使用的,我不得不说这是一个非常愉快的经历:)快到这里来的人回答,扔掉KOTLIN PoETT,使用谷歌的KSP代替,Github. COM/GoGoLe/KSP。