繁体   English   中英

当与@Repeatable @Retention(AnnotationRetention.Source) 一起使用时,roundEnv.getElementsAnnotatedWith(AnnotationName::class.java) 反射是否被破坏

[英]Is roundEnv.getElementsAnnotatedWith(AnnotationName::class.java) reflection broken when used with a @Repeatable @Retention(AnnotationRetention.Source)

使用 kapt/kotlinpoet 在 Android Studio 中构建 AbstractProcessor 时。 当我尝试使用可重复注释标签时,它会停止从 roundEnv.getElementsAnnotatedWith(AnnotationName::class.java) 获取数据,如果从注释中删除可重复标签,我可以获取带注释的类信息

将尝试使用其他反射方式

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
@Repeatable // <-- issue 
annotation class ConfigurableGroup(
    val key: String,
    val text: String
)
// the processor abbreviated

@AutoService(Processor::class)
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedOptions(AnnotationProcessorNew.
KAPT_KOTLIN_GENERATED_OPTION_NAME)
class AnnotationProcessorNew : AbstractProcessor(){

override fun process(annotations: MutableSet<out TypeElement>, roundEnv: 
RoundEnvironment): Boolean {
    return generateConfigurables(roundEnv)
}

override fun getSupportedAnnotationTypes(): MutableSet<String> {
    return mutableSetOf(
ConfigurableGroup::class.java.name
}

roundEnv.getElementsAnnotatedWith(ConfigurableGroup::class.java)
       .filter { it.kind == ElementKind.CLASS }
       .forEach { classElement ->

          val classPackageName = 
processingEnv.elementUtils.getPackageOf(classElement).toString()
               val classSimpleName = classElement.simpleName.toString()

当注释是否具有 @repeatable 标记时,我希望两次都从反射中获取数据。

看起来 Kotlin 的@Repeatable注释纯粹是一个工具提示,与 Java 的@Repeatable合同@Repeatable

看来java契约需要定义一个容器注解,这样重复的注解就可以打包成单个注解,供注解处理器检索。

您需要为容器注释显式添加@java.lang.annotation.Repeatable(ConfigurableGroups::class)并接受它生成的警告,或者用 Java 而不是 Kotlin 定义注释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM