mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-04 08:31:30 +00:00
Support KClass.isInstance/cast/safeCast in stdlib-only reflection implementation
#KT-14720 Fixed
This commit is contained in:
@@ -48,7 +48,7 @@ abstract class AbstractReflectionApiCallChecker(
|
||||
private val kClass by storageManager.createLazyValue { reflectionTypes.kClass }
|
||||
|
||||
protected open fun isAllowedKClassMember(name: Name): Boolean =
|
||||
name.asString() == "simpleName"
|
||||
name.asString() == "simpleName" || name.asString() == "isInstance"
|
||||
|
||||
final override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
|
||||
if (isWholeReflectionApiAvailable) return
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
|
||||
// TARGET_BACKEND: JVM
|
||||
|
||||
// WITH_REFLECT
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.full.cast
|
||||
import kotlin.reflect.full.safeCast
|
||||
import kotlin.reflect.cast
|
||||
import kotlin.reflect.safeCast
|
||||
import kotlin.test.*
|
||||
|
||||
fun testInstance(value: Any?, klass: KClass<*>) {
|
||||
|
||||
@@ -39,6 +39,7 @@ fun <T : Any> kclass(k: KClass<*>, kt: KClass<T>) {
|
||||
k.<!NO_REFLECTION_IN_CLASS_PATH!>isCompanion<!>
|
||||
|
||||
k.<!NO_REFLECTION_IN_CLASS_PATH!>annotations<!>
|
||||
k.isInstance(42)
|
||||
|
||||
k == kt
|
||||
k.hashCode()
|
||||
|
||||
@@ -40,18 +40,6 @@ val Class<*>.primitiveByWrapper: Class<*>?
|
||||
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
|
||||
*/
|
||||
|
||||
@@ -24,11 +24,8 @@ import org.jetbrains.kotlin.types.TypeSubstitutor
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
import org.jetbrains.kotlin.utils.DFS
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.jvm.internal.KCallableImpl
|
||||
import kotlin.reflect.jvm.internal.KClassImpl
|
||||
import kotlin.reflect.jvm.internal.KFunctionImpl
|
||||
import kotlin.reflect.jvm.internal.KTypeImpl
|
||||
import kotlin.reflect.jvm.internal.KotlinReflectionInternalError
|
||||
import kotlin.reflect.jvm.internal.*
|
||||
import kotlin.reflect.safeCast as commonSafeCast
|
||||
|
||||
/**
|
||||
* Returns the primary constructor of this class, or `null` if this class has no primary constructor.
|
||||
@@ -253,6 +250,7 @@ fun KClass<*>.isSuperclassOf(derived: KClass<*>): Boolean =
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
fun <T : Any> KClass<T>.cast(value: Any?): T {
|
||||
// Note: can't use kotlin.reflect.cast because it throws ClassCastException.
|
||||
if (!isInstance(value)) throw TypeCastException("Value cannot be cast to $qualifiedName")
|
||||
return value as T
|
||||
}
|
||||
@@ -265,9 +263,9 @@ fun <T : Any> KClass<T>.cast(value: Any?): T {
|
||||
* @see [KClass.cast]
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
fun <T : Any> KClass<T>.safeCast(value: Any?): T? {
|
||||
return if (isInstance(value)) value as T else null
|
||||
}
|
||||
fun <T : Any> KClass<T>.safeCast(value: Any?): T? =
|
||||
@UseExperimental(ExperimentalStdlibApi::class)
|
||||
commonSafeCast(value)
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,8 +20,6 @@ import org.jetbrains.kotlin.builtins.CompanionObjectMapping
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.runtime.components.ReflectKotlinClass
|
||||
import org.jetbrains.kotlin.descriptors.runtime.structure.functionClassArity
|
||||
import org.jetbrains.kotlin.descriptors.runtime.structure.wrapperByPrimitive
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
import org.jetbrains.kotlin.load.java.JvmAbi
|
||||
import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
|
||||
@@ -36,7 +34,6 @@ import org.jetbrains.kotlin.serialization.deserialization.MemberDeserializer
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor
|
||||
import org.jetbrains.kotlin.utils.compact
|
||||
import kotlin.jvm.internal.ClassReference
|
||||
import kotlin.jvm.internal.TypeIntrinsics
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.jvm.internal.KDeclarationContainerImpl.MemberBelonginess.DECLARED
|
||||
import kotlin.reflect.jvm.internal.KDeclarationContainerImpl.MemberBelonginess.INHERITED
|
||||
@@ -218,13 +215,7 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) : KDeclaration
|
||||
|
||||
override val objectInstance: T? get() = data().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 fun isInstance(value: Any?): Boolean = ClassReference.isInstance(value, jClass)
|
||||
|
||||
override val typeParameters: List<KTypeParameter> get() = data().typeParameters
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.Errors.UNSUPPORTED
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.AbstractReflectionApiCallChecker
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.CallCheckerContext
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
@@ -39,9 +38,6 @@ class JsReflectionAPICallChecker(
|
||||
reflectionTypes: ReflectionTypes,
|
||||
storageManager: StorageManager
|
||||
) : AbstractReflectionApiCallChecker(reflectionTypes, storageManager) {
|
||||
override fun isAllowedKClassMember(name: Name): Boolean =
|
||||
super.isAllowedKClassMember(name) || name.asString() == "isInstance"
|
||||
|
||||
override val isWholeReflectionApiAvailable: Boolean
|
||||
get() = false
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
40
libraries/stdlib/src/kotlin/reflect/KClasses.kt
Normal file
40
libraries/stdlib/src/kotlin/reflect/KClasses.kt
Normal file
@@ -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
|
||||
}
|
||||
@@ -3299,6 +3299,7 @@ public final class kotlin/jvm/internal/ClassReference : kotlin/jvm/internal/Clas
|
||||
public final class kotlin/jvm/internal/ClassReference$Companion {
|
||||
public final fun getClassQualifiedName (Ljava/lang/Class;)Ljava/lang/String;
|
||||
public final fun getClassSimpleName (Ljava/lang/Class;)Ljava/lang/String;
|
||||
public final fun isInstance (Ljava/lang/Object;Ljava/lang/Class;)Z
|
||||
}
|
||||
|
||||
public final class kotlin/jvm/internal/CollectionToArray {
|
||||
@@ -4382,6 +4383,11 @@ public abstract interface class kotlin/reflect/KClass : kotlin/reflect/KAnnotate
|
||||
public abstract fun isSealed ()Z
|
||||
}
|
||||
|
||||
public final class kotlin/reflect/KClasses {
|
||||
public static final fun cast (Lkotlin/reflect/KClass;Ljava/lang/Object;)Ljava/lang/Object;
|
||||
public static final fun safeCast (Lkotlin/reflect/KClass;Ljava/lang/Object;)Ljava/lang/Object;
|
||||
}
|
||||
|
||||
public abstract interface class kotlin/reflect/KClassifier {
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user