Reflection: add KClass.isInstance, KClass.cast, KClass.safeCast
#KT-11284 Fixed
This commit is contained in:
@@ -58,6 +58,11 @@ public interface KClass<T : Any> : KDeclarationContainer, KAnnotatedElement, KCl
|
||||
*/
|
||||
public val objectInstance: T?
|
||||
|
||||
/**
|
||||
* Returns `true` if [value] is an instance of this class on a given platform.
|
||||
*/
|
||||
public fun isInstance(value: Any?): Boolean
|
||||
|
||||
/**
|
||||
* The list of type parameters of this class. This list does *not* include type parameters of outer classes.
|
||||
*/
|
||||
|
||||
+19
-3
@@ -27,13 +27,29 @@ val Class<*>.safeClassLoader: ClassLoader
|
||||
fun Class<*>.isEnumClassOrSpecializedEnumEntryClass(): Boolean =
|
||||
Enum::class.java.isAssignableFrom(this)
|
||||
|
||||
private val WRAPPER_TO_PRIMITIVE = listOf(
|
||||
Boolean::class, Byte::class, Char::class, Double::class, Float::class, Int::class, Long::class, Short::class
|
||||
).map { it.javaObjectType to it.javaPrimitiveType }.toMap()
|
||||
private val PRIMITIVE_CLASSES =
|
||||
listOf(Boolean::class, Byte::class, Char::class, Double::class, Float::class, Int::class, Long::class, Short::class)
|
||||
private val WRAPPER_TO_PRIMITIVE = PRIMITIVE_CLASSES.map { it.javaObjectType to it.javaPrimitiveType }.toMap()
|
||||
private val PRIMITIVE_TO_WRAPPER = PRIMITIVE_CLASSES.map { it.javaPrimitiveType to it.javaObjectType }.toMap()
|
||||
|
||||
val Class<*>.primitiveByWrapper: Class<*>?
|
||||
get() = WRAPPER_TO_PRIMITIVE[this]
|
||||
|
||||
val Class<*>.wrapperByPrimitive: Class<*>?
|
||||
get() = PRIMITIVE_TO_WRAPPER[this]
|
||||
|
||||
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()
|
||||
|
||||
val Class<*>.functionClassArity: Int?
|
||||
get() = FUNCTION_CLASSES[this]
|
||||
|
||||
/**
|
||||
* NOTE: does not perform a Java -> Kotlin mapping. If this is not expected, consider using KClassImpl#classId instead
|
||||
*/
|
||||
|
||||
@@ -236,3 +236,29 @@ fun KClass<*>.isSubclassOf(base: KClass<*>): Boolean =
|
||||
*/
|
||||
fun KClass<*>.isSuperclassOf(derived: KClass<*>): Boolean =
|
||||
derived.isSubclassOf(this)
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @see [KClass.isInstance]
|
||||
* @see [KClass.safeCast]
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun <T : Any> KClass<T>.cast(value: Any?): T {
|
||||
if (!isInstance(value)) throw TypeCastException("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.
|
||||
*
|
||||
* @see [KClass.isInstance]
|
||||
* @see [KClass.cast]
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun <T : Any> KClass<T>.safeCast(value: Any?): T? {
|
||||
return if (isInstance(value)) value as T else null
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotated
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
import org.jetbrains.kotlin.load.java.JvmAbi
|
||||
import org.jetbrains.kotlin.load.java.structure.reflect.functionClassArity
|
||||
import org.jetbrains.kotlin.load.java.structure.reflect.wrapperByPrimitive
|
||||
import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
|
||||
import org.jetbrains.kotlin.load.kotlin.reflect.ReflectKotlinClass
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
@@ -27,6 +29,7 @@ import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
import org.jetbrains.kotlin.serialization.deserialization.findClassAcrossModuleDependencies
|
||||
import kotlin.jvm.internal.TypeIntrinsics
|
||||
import kotlin.reflect.*
|
||||
|
||||
internal class KClassImpl<T : Any>(override val jClass: Class<T>) :
|
||||
@@ -136,6 +139,14 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) :
|
||||
override val objectInstance: T?
|
||||
get() = objectInstance_()
|
||||
|
||||
override fun isInstance(value: Any?): Boolean {
|
||||
// TODO: use Kotlin semantics for mutable/read-only collections once KT-11754 is supported (see TypeIntrinsics)
|
||||
jClass.functionClassArity?.let { arity ->
|
||||
return TypeIntrinsics.isFunctionOfArity(value, arity)
|
||||
}
|
||||
return (jClass.wrapperByPrimitive ?: jClass).isInstance(value)
|
||||
}
|
||||
|
||||
override val typeParameters: List<KTypeParameter>
|
||||
get() = descriptor.declaredTypeParameters.map(::KTypeParameterImpl)
|
||||
|
||||
|
||||
@@ -40,6 +40,8 @@ class ClassReference(override val jClass: Class<*>) : KClass<Any>, ClassBasedDec
|
||||
override val objectInstance: Any?
|
||||
get() = error()
|
||||
|
||||
override fun isInstance(value: Any?): Boolean = error()
|
||||
|
||||
override val typeParameters: List<KTypeParameter>
|
||||
get() = error()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user