Create array instances of correct types in reflection
Based on #4168. #KT-44977 Fixed Co-authored-by: Arkady Bazhanov <arkady.bazhanov@gmail.com>
This commit is contained in:
@@ -55,7 +55,7 @@ class AnnotationValue(value: AnnotationDescriptor) : ConstantValue<AnnotationDes
|
||||
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitAnnotationValue(this, data)
|
||||
}
|
||||
|
||||
class ArrayValue(
|
||||
open class ArrayValue(
|
||||
value: List<ConstantValue<*>>,
|
||||
private val computeType: (ModuleDescriptor) -> KotlinType
|
||||
) : ConstantValue<List<ConstantValue<*>>>(value) {
|
||||
|
||||
+1
-2
@@ -30,7 +30,6 @@ import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.constants.*
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
|
||||
import org.jetbrains.kotlin.types.ErrorUtils
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
@@ -84,7 +83,7 @@ class AnnotationDeserializer(private val module: ModuleDescriptor, private val n
|
||||
Type.CLASS -> KClassValue(nameResolver.getClassId(value.classId), value.arrayDimensionCount)
|
||||
Type.ENUM -> EnumValue(nameResolver.getClassId(value.classId), nameResolver.getName(value.enumValueId))
|
||||
Type.ANNOTATION -> AnnotationValue(deserializeAnnotation(value.annotation, nameResolver))
|
||||
Type.ARRAY -> ConstantValueFactory.createArrayValue(
|
||||
Type.ARRAY -> DeserializedArrayValue(
|
||||
value.arrayElementList.map { resolveValue(builtIns.anyType, it, nameResolver) },
|
||||
expectedType
|
||||
)
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.serialization.deserialization
|
||||
|
||||
import org.jetbrains.kotlin.resolve.constants.ArrayValue
|
||||
import org.jetbrains.kotlin.resolve.constants.ConstantValue
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
class DeserializedArrayValue(value: List<ConstantValue<*>>, val type: KotlinType) : ArrayValue(value, { type })
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package kotlin.reflect.jvm.internal
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.builtins.PrimitiveType
|
||||
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotated
|
||||
@@ -42,6 +44,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
|
||||
import org.jetbrains.kotlin.resolve.isInlineClassType
|
||||
import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext
|
||||
import org.jetbrains.kotlin.serialization.deserialization.DeserializedArrayValue
|
||||
import org.jetbrains.kotlin.serialization.deserialization.MemberDeserializer
|
||||
import java.lang.reflect.Type
|
||||
import kotlin.jvm.internal.FunctionReference
|
||||
@@ -138,7 +141,7 @@ private fun AnnotationDescriptor.toAnnotationInstance(): Annotation? {
|
||||
// TODO: consider throwing exceptions such as AnnotationFormatError/AnnotationTypeMismatchException if a value of unexpected type is found
|
||||
private fun ConstantValue<*>.toRuntimeValue(classLoader: ClassLoader): Any? = when (this) {
|
||||
is AnnotationValue -> value.toAnnotationInstance()
|
||||
is ArrayValue -> value.map { it.toRuntimeValue(classLoader) }.toTypedArray()
|
||||
is ArrayValue -> arrayToRuntimeValue(classLoader)
|
||||
is EnumValue -> {
|
||||
val (enumClassId, entryName) = value
|
||||
loadClass(classLoader, enumClassId)?.let { enumClass ->
|
||||
@@ -158,6 +161,39 @@ private fun ConstantValue<*>.toRuntimeValue(classLoader: ClassLoader): Any? = wh
|
||||
else -> value // Primitives and strings
|
||||
}
|
||||
|
||||
private fun ArrayValue.arrayToRuntimeValue(classLoader: ClassLoader): Any? {
|
||||
val type = (this as? DeserializedArrayValue)?.type ?: return null
|
||||
val values = value.map { it.toRuntimeValue(classLoader) }
|
||||
|
||||
return when (KotlinBuiltIns.getPrimitiveArrayElementType(type)) {
|
||||
PrimitiveType.BOOLEAN -> BooleanArray(value.size) { values[it] as Boolean }
|
||||
PrimitiveType.CHAR -> CharArray(value.size) { values[it] as Char }
|
||||
PrimitiveType.BYTE -> ByteArray(value.size) { values[it] as Byte }
|
||||
PrimitiveType.SHORT -> ShortArray(value.size) { values[it] as Short }
|
||||
PrimitiveType.INT -> IntArray(value.size) { values[it] as Int }
|
||||
PrimitiveType.FLOAT -> FloatArray(value.size) { values[it] as Float }
|
||||
PrimitiveType.LONG -> LongArray(value.size) { values[it] as Long }
|
||||
PrimitiveType.DOUBLE -> DoubleArray(value.size) { values[it] as Double }
|
||||
null -> {
|
||||
check(KotlinBuiltIns.isArray(type)) { "Not an array type: $type" }
|
||||
val argType = type.arguments.single().type
|
||||
val classifier = argType.constructor.declarationDescriptor as? ClassDescriptor ?: error("Not a class type: $argType")
|
||||
when {
|
||||
KotlinBuiltIns.isString(argType) -> Array(value.size) { values[it] as String }
|
||||
KotlinBuiltIns.isKClass(classifier) -> Array(value.size) { values[it] as Class<*> }
|
||||
else -> {
|
||||
val argClass = classifier.classId?.let { loadClass(classLoader, it) } ?: return null
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val array = java.lang.reflect.Array.newInstance(argClass, value.size) as Array<in Any?>
|
||||
repeat(values.size) { array[it] = values[it] }
|
||||
array
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: wrap other exceptions
|
||||
internal inline fun <R> reflectionCall(block: () -> R): R =
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user