Fix reflection for repeated annotations with array arguments

This is related to 8308f5d7d3 (KT-44977).

It turns out that it's necessary to create `ArrayValue` with a
precomputed type not only in case of annotations stored directly in
Kotlin metadata (i.e. annotations on types and type parameters), but
also for arguments of repeated annotations. The reason is that repeated
annotations are stored in an array themselves, as an argument to another
(container) annotation. The annotation-reading code unwraps this array
to load repeated annotations as individual instances of
`AnnotationDescriptor`, but in contrast to loading normal annotations,
it doesn't set `source` to the reflection object, it uses `NO_SOURCE`
(see BinaryClassAnnotationAndConstantLoaderImpl.AbstractAnnotationArgumentVisitor.visitArray).

So when kotlin-reflect is later asked for the arguments of the
annotation, it needs to recreate the reflection object from
`AnnotationDescriptor` in `util.kt`. Doing so requires knowing the array
type, which is now present because we're creating a `TypedArrayValue`
(previously `DeserializedArrayValue`) in places where arrays are read as
annotation arguments.

Alternative solution would be to fix source passed to
`AnnotationDescriptorImpl` in the annotation-reading code, but that
seems a bit messy because the code is very abstract and is used in lots
of other places.

 #KT-53279 Fixed
This commit is contained in:
Alexander Udalov
2022-09-29 00:57:29 +02:00
committed by Space Team
parent 51ce829b96
commit 4dcf50d483
10 changed files with 164 additions and 32 deletions
@@ -0,0 +1,62 @@
// TARGET_BACKEND: JVM_IR
// JVM_TARGET: 1.8
// FULL_JDK
// WITH_REFLECT
// Android doesn't have @Repeatable before API level 24, so findAnnotations can't unpack repeatable annotations.
// IGNORE_BACKEND: ANDROID
import kotlin.test.assertEquals
import kotlin.reflect.full.findAnnotations
@JvmRepeatable(ArrayOfInt.ContainerOfInt::class)
annotation class ArrayOfInt(val ints: IntArray = []) {
annotation class ContainerOfInt(val value: Array<ArrayOfInt>)
}
@JvmRepeatable(ArrayOfString.ContainerOfString::class)
annotation class ArrayOfString(val strings: Array<String> = []) {
annotation class ContainerOfString(val value: Array<ArrayOfString>)
}
@JvmRepeatable(ArrayOfEnum.ContainerOfEnum::class)
annotation class ArrayOfEnum(val enums: Array<DeprecationLevel> = []) {
annotation class ContainerOfEnum(val value: Array<ArrayOfEnum>)
}
@JvmRepeatable(ArrayOfAnnotation.ContainerOfAnnotation::class)
annotation class ArrayOfAnnotation(val annotations: Array<ArrayOfString> = []) {
annotation class ContainerOfAnnotation(val value: Array<ArrayOfAnnotation>)
}
class C {
@ArrayOfInt([1])
@ArrayOfInt([2])
val arrayOfInt = ""
@ArrayOfString(["a"])
@ArrayOfString(["b"])
val arrayOfString = ""
@ArrayOfEnum([DeprecationLevel.WARNING])
@ArrayOfEnum([DeprecationLevel.ERROR])
val arrayOfEnum = ""
@ArrayOfAnnotation([ArrayOfString(arrayOf("a"))])
@ArrayOfAnnotation([ArrayOfString(arrayOf("b"))])
val arrayOfAnnotation = ""
}
fun box(): String {
assertEquals("[1, 2]", C::arrayOfInt.findAnnotations<ArrayOfInt>().map { it.ints.single() }.toString())
assertEquals("[a, b]", C::arrayOfString.findAnnotations<ArrayOfString>().map { it.strings.single() }.toString())
assertEquals("[WARNING, ERROR]", C::arrayOfEnum.findAnnotations<ArrayOfEnum>().map { it.enums.single() }.toString())
assertEquals(
"[a, b]",
C::arrayOfAnnotation.findAnnotations<ArrayOfAnnotation>().map {
it.annotations.single().strings.single()
}.toString()
)
return "OK"
}
@@ -0,0 +1,50 @@
// TARGET_BACKEND: JVM_IR
// JVM_TARGET: 1.8
// FULL_JDK
// WITH_REFLECT
import kotlin.test.assertEquals
@Repeatable
annotation class ArrayOfInt(val ints: IntArray = [])
@Repeatable
annotation class ArrayOfString(val strings: Array<String> = [])
@Repeatable
annotation class ArrayOfEnum(val enums: Array<DeprecationLevel> = [])
@Repeatable
annotation class ArrayOfAnnotation(val annotations: Array<ArrayOfString> = [])
class C {
@ArrayOfInt([1])
@ArrayOfInt([2])
val arrayOfInt = ""
@ArrayOfString(["a"])
@ArrayOfString(["b"])
val arrayOfString = ""
@ArrayOfEnum([DeprecationLevel.WARNING])
@ArrayOfEnum([DeprecationLevel.ERROR])
val arrayOfEnum = ""
@ArrayOfAnnotation([ArrayOfString(arrayOf("a"))])
@ArrayOfAnnotation([ArrayOfString(arrayOf("b"))])
val arrayOfAnnotation = ""
}
fun box(): String {
assertEquals("[1, 2]", C::arrayOfInt.annotations.filterIsInstance<ArrayOfInt>().map { it.ints.single() }.toString())
assertEquals("[a, b]", C::arrayOfString.annotations.filterIsInstance<ArrayOfString>().map { it.strings.single() }.toString())
assertEquals("[WARNING, ERROR]", C::arrayOfEnum.annotations.filterIsInstance<ArrayOfEnum>().map { it.enums.single() }.toString())
assertEquals(
"[a, b]",
C::arrayOfAnnotation.annotations.filterIsInstance<ArrayOfAnnotation>().map {
it.annotations.single().strings.single()
}.toString()
)
return "OK"
}