Support new repeatable annotations in kotlin-reflect
- Unwrap Kotlin-repeatable annotations (with implicit container) - Introduce `KAnnotatedElement.findAnnotations` to find instances of repeated annotations #KT-12794
This commit is contained in:
+44
@@ -0,0 +1,44 @@
|
||||
// !LANGUAGE: +RepeatableAnnotations
|
||||
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
|
||||
// TARGET_BACKEND: JVM_IR
|
||||
// JVM_TARGET: 1.8
|
||||
// FULL_JDK
|
||||
// WITH_REFLECT
|
||||
// FILE: box.kt
|
||||
|
||||
import kotlin.reflect.full.findAnnotation
|
||||
import kotlin.reflect.full.findAnnotations
|
||||
import kotlin.reflect.full.hasAnnotation
|
||||
|
||||
@A("O")
|
||||
@A("")
|
||||
@A("K")
|
||||
fun f() {}
|
||||
|
||||
fun box(): String {
|
||||
val element = ::f
|
||||
if (element.hasAnnotation<A>()) return "Fail hasAnnotation $element"
|
||||
val find = element.findAnnotation<A>()
|
||||
if (find != null) return "Fail findAnnotation $element: $find"
|
||||
|
||||
val all = (element.annotations.single() as A.Container).value.asList()
|
||||
val findAll = element.findAnnotations<A>()
|
||||
if (all != findAll) throw AssertionError("Fail findAnnotations $element: $all != $findAll")
|
||||
|
||||
return all.fold("") { acc, it -> acc + it.value }
|
||||
}
|
||||
|
||||
// FILE: A.java
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Repeatable(A.Container.class)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface A {
|
||||
String value();
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Container {
|
||||
A[] value();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user