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:
committed by
Space Team
parent
51ce829b96
commit
4dcf50d483
+21
-14
@@ -18,13 +18,15 @@ package org.jetbrains.kotlin.resolve.constants
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.builtins.PrimitiveType
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.TypeUtils
|
||||
|
||||
object ConstantValueFactory {
|
||||
fun createArrayValue(value: List<ConstantValue<*>>, type: KotlinType) = ArrayValue(value) { type }
|
||||
fun createArrayValue(value: List<ConstantValue<*>>, type: KotlinType): ArrayValue =
|
||||
TypedArrayValue(value, type)
|
||||
|
||||
fun createConstantValue(value: Any?): ConstantValue<*>? {
|
||||
fun createConstantValue(value: Any?, module: ModuleDescriptor? = null): ConstantValue<*>? {
|
||||
return when (value) {
|
||||
is Byte -> ByteValue(value)
|
||||
is Short -> ShortValue(value)
|
||||
@@ -35,14 +37,14 @@ object ConstantValueFactory {
|
||||
is Double -> DoubleValue(value)
|
||||
is Boolean -> BooleanValue(value)
|
||||
is String -> StringValue(value)
|
||||
is ByteArray -> createArrayValue(value.toList(), PrimitiveType.BYTE)
|
||||
is ShortArray -> createArrayValue(value.toList(), PrimitiveType.SHORT)
|
||||
is IntArray -> createArrayValue(value.toList(), PrimitiveType.INT)
|
||||
is LongArray -> createArrayValue(value.toList(), PrimitiveType.LONG)
|
||||
is CharArray -> createArrayValue(value.toList(), PrimitiveType.CHAR)
|
||||
is FloatArray -> createArrayValue(value.toList(), PrimitiveType.FLOAT)
|
||||
is DoubleArray -> createArrayValue(value.toList(), PrimitiveType.DOUBLE)
|
||||
is BooleanArray -> createArrayValue(value.toList(), PrimitiveType.BOOLEAN)
|
||||
is ByteArray -> createArrayValue(value.toList(), module, PrimitiveType.BYTE)
|
||||
is ShortArray -> createArrayValue(value.toList(), module, PrimitiveType.SHORT)
|
||||
is IntArray -> createArrayValue(value.toList(), module, PrimitiveType.INT)
|
||||
is LongArray -> createArrayValue(value.toList(), module, PrimitiveType.LONG)
|
||||
is CharArray -> createArrayValue(value.toList(), module, PrimitiveType.CHAR)
|
||||
is FloatArray -> createArrayValue(value.toList(), module, PrimitiveType.FLOAT)
|
||||
is DoubleArray -> createArrayValue(value.toList(), module, PrimitiveType.DOUBLE)
|
||||
is BooleanArray -> createArrayValue(value.toList(), module, PrimitiveType.BOOLEAN)
|
||||
null -> NullValue()
|
||||
else -> null
|
||||
}
|
||||
@@ -58,10 +60,15 @@ object ConstantValueFactory {
|
||||
}
|
||||
}
|
||||
|
||||
private fun createArrayValue(value: List<*>, componentType: PrimitiveType): ArrayValue =
|
||||
ArrayValue(value.toList().mapNotNull(this::createConstantValue)) { module ->
|
||||
module.builtIns.getPrimitiveArrayKotlinType(componentType)
|
||||
}
|
||||
private fun createArrayValue(value: List<*>, module: ModuleDescriptor?, componentType: PrimitiveType): ArrayValue {
|
||||
val elements = value.toList().mapNotNull(this::createConstantValue)
|
||||
return if (module != null)
|
||||
TypedArrayValue(elements, module.builtIns.getPrimitiveArrayKotlinType(componentType))
|
||||
else
|
||||
ArrayValue(elements) {
|
||||
it.builtIns.getPrimitiveArrayKotlinType(componentType)
|
||||
}
|
||||
}
|
||||
|
||||
fun createIntegerConstantValue(
|
||||
value: Long,
|
||||
|
||||
@@ -29,8 +29,8 @@ import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.error.ErrorUtils
|
||||
import org.jetbrains.kotlin.types.error.ErrorTypeKind
|
||||
import org.jetbrains.kotlin.types.error.ErrorUtils
|
||||
import org.jetbrains.kotlin.types.typeUtil.replaceArgumentsWithStarProjections
|
||||
|
||||
abstract class ConstantValue<out T>(open val value: T) {
|
||||
@@ -69,6 +69,8 @@ open class ArrayValue(
|
||||
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitArrayValue(this, data)
|
||||
}
|
||||
|
||||
class TypedArrayValue(value: List<ConstantValue<*>>, val type: KotlinType) : ArrayValue(value, { type })
|
||||
|
||||
class BooleanValue(value: Boolean) : ConstantValue<Boolean>(value) {
|
||||
override fun getType(module: ModuleDescriptor) = module.builtIns.booleanType
|
||||
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitBooleanValue(this, data)
|
||||
|
||||
Reference in New Issue
Block a user