简体   繁体   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)

When building a AbstractProcessor in Android studio using kapt/kotlinpoet.使用 kapt/kotlinpoet 在 Android Studio 中构建 AbstractProcessor 时。 When I try to use the repeatable annotation tag it stop getting data back from roundEnv.getElementsAnnotatedWith(AnnotationName::class.java), I am able get the annotated classes info back if the repeatable tag is removed from the annotation当我尝试使用可重复注释标签时,它会停止从 roundEnv.getElementsAnnotatedWith(AnnotationName::class.java) 获取数据,如果从注释中删除可重复标签,我可以获取带注释的类信息

going to try to use other means of reflection将尝试使用其他反射方式

@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()

I would expect to get data from reflection both times when the annotation has a @repeatable tag or not.当注释是否具有 @repeatable 标记时,我希望两次都从反射中获取数据。

It seems that Kotlin's @Repeatable annotation is purely a hint for tooling and does not match up with Java's @Repeatable contract.看起来 Kotlin 的@Repeatable注释纯粹是一个工具提示,与 Java 的@Repeatable合同@Repeatable

It seems that the java contract requires a container annotation to be defined so that the repeated annotations can be packaged as a single annotation for retrieval by the annotation processor.看来java契约需要定义一个容器注解,这样重复的注解就可以打包成单个注解,供注解处理器检索。

You either need to explicitly add @java.lang.annotation.Repeatable(ConfigurableGroups::class) for a container annotation and accept the warning it generates or define the annotations in Java instead of Kotlin.您需要为容器注释显式添加@java.lang.annotation.Repeatable(ConfigurableGroups::class)并接受它生成的警告,或者用 Java 而不是 Kotlin 定义注释。

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

相关问题 什么意思::class.java - What is the meaning of ::class.java Android Studio - ActivityClass::class.java - Android Studio - ActivityClass::class.java 为什么T :: class.java是Class <out Int> 在科特林? - Why T::class.java is Class<out Int> in Kotlin? 为什么我们编写NextActivity :: class.java,尽管这是一个kotlin类? - Why we write NextActivity::class.java although this is a kotlin class? 为什么Intent中的类具有“:: class.java”后缀? - Why does the class in Intent have the “::class.java” suffix? Boolean::class.java can not cast to boolean (java) when receive data from Signal R and pass to broadcast receiver - Boolean::class.java can not cast to boolean (java) when receive data from Signal R and pass to broadcast receiver Kotlin 中的“ClassInstance.[Someclass::class.java]”是什么意思? - What is the meaning of “ClassInstance.[Someclass::class.java]” in Kotlin? 为什么我们在Kotlin中使用::class.java? - Why do we use ::class.java in Kotlin? AndroidX Room 生成 class 错误:“类是公共的,应在名为 class.java 的文件中声明” - AndroidX Room generated class error: "Class is public, should be declared in a file named class.java" java.lang.NullPointerException: Gson().fromJson(placeJson, Place::class.java) 不能是 null - java.lang.NullPointerException: Gson().fromJson(placeJson, Place::class.java) must not be null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM