Support KClass.isInstance/cast/safeCast in stdlib-only reflection implementation

#KT-14720 Fixed
This commit is contained in:
Alexander Udalov
2019-10-24 19:37:29 +02:00
parent 5c89f2fa54
commit 896512f7cd
10 changed files with 80 additions and 42 deletions
@@ -30,7 +30,8 @@ public class ClassReference(override val jClass: Class<*>) : KClass<Any>, ClassB
get() = error()
@SinceKotlin("1.1")
override fun isInstance(value: Any?): Boolean = error()
override fun isInstance(value: Any?): Boolean =
isInstance(value, jClass)
@SinceKotlin("1.1")
override val typeParameters: List<KTypeParameter>
@@ -88,6 +89,15 @@ public class ClassReference(override val jClass: Class<*>) : KClass<Any>, ClassB
jClass.toString() + Reflection.REFLECTION_NOT_AVAILABLE
companion object {
private val FUNCTION_CLASSES =
listOf(
Function0::class.java, Function1::class.java, Function2::class.java, Function3::class.java, Function4::class.java,
Function5::class.java, Function6::class.java, Function7::class.java, Function8::class.java, Function9::class.java,
Function10::class.java, Function11::class.java, Function12::class.java, Function13::class.java, Function14::class.java,
Function15::class.java, Function16::class.java, Function17::class.java, Function18::class.java, Function19::class.java,
Function20::class.java, Function21::class.java, Function22::class.java
).mapIndexed { i, clazz -> clazz to i }.toMap()
private val primitiveFqNames = HashMap<String, String>().apply {
put("boolean", "kotlin.Boolean")
put("char", "kotlin.Char")
@@ -137,8 +147,8 @@ public class ClassReference(override val jClass: Class<*>) : KClass<Any>, ClassB
primitiveFqNames.values.associateTo(this) { kotlinName ->
"kotlin.jvm.internal.${kotlinName.substringAfterLast('.')}CompanionObject" to "$kotlinName.Companion"
}
for (arity in 0 until 23) {
put("kotlin.jvm.functions.Function$arity", "kotlin.Function$arity")
for ((klass, arity) in FUNCTION_CLASSES) {
put(klass.name, "kotlin.Function$arity")
}
}
@@ -174,5 +184,13 @@ public class ClassReference(override val jClass: Class<*>) : KClass<Any>, ClassB
}
else -> classFqNames[jClass.name] ?: jClass.canonicalName
}
public fun isInstance(value: Any?, jClass: Class<*>): Boolean {
FUNCTION_CLASSES[jClass]?.let { arity ->
return TypeIntrinsics.isFunctionOfArity(value, arity)
}
val objectType = if (jClass.isPrimitive) jClass.kotlin.javaObjectType else jClass
return objectType.isInstance(value)
}
}
}
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2019 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.
*/
@file:kotlin.jvm.JvmName("KClasses")
@file:Suppress("UNCHECKED_CAST")
package kotlin.reflect
/**
* Casts the given [value] to the class represented by this [KClass] object.
* Throws an exception if the value is `null` or if it is not an instance of this class.
*
* This is an experimental function that behaves as a similar function from kotlin.reflect.full on JVM.
*
* @see [KClass.isInstance]
* @see [KClass.safeCast]
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
fun <T : Any> KClass<T>.cast(value: Any?): T {
if (!isInstance(value)) throw ClassCastException("Value cannot be cast to $qualifiedName")
return value as T
}
/**
* Casts the given [value] to the class represented by this [KClass] object.
* Returns `null` if the value is `null` or if it is not an instance of this class.
*
* This is an experimental function that behaves as a similar function from kotlin.reflect.full on JVM.
*
* @see [KClass.isInstance]
* @see [KClass.cast]
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
fun <T : Any> KClass<T>.safeCast(value: Any?): T? {
return if (isInstance(value)) value as T else null
}