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:
@@ -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