簡體   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