Do not use new stdlib API in reflection implementation
Otherwise this brings incompatibility between kotlin-reflect 1.3.70+ and kotlin-stdlib 1.3.60-. This commit reverts the relevant parts ofc164745301,5c89f2fa54and896512f7cd.
This commit is contained in:
+12
@@ -40,6 +40,18 @@ val Class<*>.primitiveByWrapper: Class<*>?
|
|||||||
val Class<*>.wrapperByPrimitive: Class<*>?
|
val Class<*>.wrapperByPrimitive: Class<*>?
|
||||||
get() = PRIMITIVE_TO_WRAPPER[this]
|
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
|
* NOTE: does not perform a Java -> Kotlin mapping. If this is not expected, consider using KClassImpl#classId instead
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -24,8 +24,11 @@ import org.jetbrains.kotlin.types.TypeSubstitutor
|
|||||||
import org.jetbrains.kotlin.types.Variance
|
import org.jetbrains.kotlin.types.Variance
|
||||||
import org.jetbrains.kotlin.utils.DFS
|
import org.jetbrains.kotlin.utils.DFS
|
||||||
import kotlin.reflect.*
|
import kotlin.reflect.*
|
||||||
import kotlin.reflect.jvm.internal.*
|
import kotlin.reflect.jvm.internal.KCallableImpl
|
||||||
import kotlin.reflect.safeCast as commonSafeCast
|
import kotlin.reflect.jvm.internal.KClassImpl
|
||||||
|
import kotlin.reflect.jvm.internal.KFunctionImpl
|
||||||
|
import kotlin.reflect.jvm.internal.KTypeImpl
|
||||||
|
import kotlin.reflect.jvm.internal.KotlinReflectionInternalError
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the primary constructor of this class, or `null` if this class has no primary constructor.
|
* Returns the primary constructor of this class, or `null` if this class has no primary constructor.
|
||||||
@@ -250,7 +253,6 @@ fun KClass<*>.isSuperclassOf(derived: KClass<*>): Boolean =
|
|||||||
*/
|
*/
|
||||||
@SinceKotlin("1.1")
|
@SinceKotlin("1.1")
|
||||||
fun <T : Any> KClass<T>.cast(value: Any?): T {
|
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")
|
if (!isInstance(value)) throw TypeCastException("Value cannot be cast to $qualifiedName")
|
||||||
return value as T
|
return value as T
|
||||||
}
|
}
|
||||||
@@ -263,9 +265,9 @@ fun <T : Any> KClass<T>.cast(value: Any?): T {
|
|||||||
* @see [KClass.cast]
|
* @see [KClass.cast]
|
||||||
*/
|
*/
|
||||||
@SinceKotlin("1.1")
|
@SinceKotlin("1.1")
|
||||||
fun <T : Any> KClass<T>.safeCast(value: Any?): T? =
|
fun <T : Any> KClass<T>.safeCast(value: Any?): T? {
|
||||||
@UseExperimental(ExperimentalStdlibApi::class)
|
return if (isInstance(value)) value as T else null
|
||||||
commonSafeCast(value)
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ package kotlin.reflect.jvm.internal
|
|||||||
import org.jetbrains.kotlin.builtins.CompanionObjectMapping
|
import org.jetbrains.kotlin.builtins.CompanionObjectMapping
|
||||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
import org.jetbrains.kotlin.descriptors.runtime.components.ReflectKotlinClass
|
|
||||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||||
import org.jetbrains.kotlin.load.java.JvmAbi
|
import org.jetbrains.kotlin.load.java.JvmAbi
|
||||||
import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
|
import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
|
||||||
@@ -33,10 +32,13 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
|||||||
import org.jetbrains.kotlin.serialization.deserialization.MemberDeserializer
|
import org.jetbrains.kotlin.serialization.deserialization.MemberDeserializer
|
||||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor
|
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor
|
||||||
import org.jetbrains.kotlin.utils.compact
|
import org.jetbrains.kotlin.utils.compact
|
||||||
import kotlin.jvm.internal.ClassReference
|
import kotlin.jvm.internal.TypeIntrinsics
|
||||||
import kotlin.reflect.*
|
import kotlin.reflect.*
|
||||||
import kotlin.reflect.jvm.internal.KDeclarationContainerImpl.MemberBelonginess.DECLARED
|
import kotlin.reflect.jvm.internal.KDeclarationContainerImpl.MemberBelonginess.DECLARED
|
||||||
import kotlin.reflect.jvm.internal.KDeclarationContainerImpl.MemberBelonginess.INHERITED
|
import kotlin.reflect.jvm.internal.KDeclarationContainerImpl.MemberBelonginess.INHERITED
|
||||||
|
import org.jetbrains.kotlin.descriptors.runtime.components.ReflectKotlinClass
|
||||||
|
import org.jetbrains.kotlin.descriptors.runtime.structure.functionClassArity
|
||||||
|
import org.jetbrains.kotlin.descriptors.runtime.structure.wrapperByPrimitive
|
||||||
|
|
||||||
internal class KClassImpl<T : Any>(override val jClass: Class<T>) : KDeclarationContainerImpl(), KClass<T>, KClassifierImpl {
|
internal class KClassImpl<T : Any>(override val jClass: Class<T>) : KDeclarationContainerImpl(), KClass<T>, KClassifierImpl {
|
||||||
inner class Data : KDeclarationContainerImpl.Data() {
|
inner class Data : KDeclarationContainerImpl.Data() {
|
||||||
@@ -53,6 +55,37 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) : KDeclaration
|
|||||||
|
|
||||||
val annotations: List<Annotation> by ReflectProperties.lazySoft { descriptor.computeAnnotations() }
|
val annotations: List<Annotation> by ReflectProperties.lazySoft { descriptor.computeAnnotations() }
|
||||||
|
|
||||||
|
val simpleName: String? by ReflectProperties.lazySoft {
|
||||||
|
if (jClass.isAnonymousClass) return@lazySoft null
|
||||||
|
|
||||||
|
val classId = classId
|
||||||
|
when {
|
||||||
|
classId.isLocal -> calculateLocalClassName(jClass)
|
||||||
|
else -> classId.shortClassName.asString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val qualifiedName: String? by ReflectProperties.lazySoft {
|
||||||
|
if (jClass.isAnonymousClass) return@lazySoft null
|
||||||
|
|
||||||
|
val classId = classId
|
||||||
|
when {
|
||||||
|
classId.isLocal -> null
|
||||||
|
else -> classId.asSingleFqName().asString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun calculateLocalClassName(jClass: Class<*>): String {
|
||||||
|
val name = jClass.simpleName
|
||||||
|
jClass.enclosingMethod?.let { method ->
|
||||||
|
return name.substringAfter(method.name + "$")
|
||||||
|
}
|
||||||
|
jClass.enclosingConstructor?.let { constructor ->
|
||||||
|
return name.substringAfter(constructor.name + "$")
|
||||||
|
}
|
||||||
|
return name.substringAfter('$')
|
||||||
|
}
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
val constructors: Collection<KFunction<T>> by ReflectProperties.lazySoft {
|
val constructors: Collection<KFunction<T>> by ReflectProperties.lazySoft {
|
||||||
constructorDescriptors.map { descriptor ->
|
constructorDescriptors.map { descriptor ->
|
||||||
@@ -147,17 +180,7 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) : KDeclaration
|
|||||||
|
|
||||||
override val annotations: List<Annotation> get() = data().annotations
|
override val annotations: List<Annotation> get() = data().annotations
|
||||||
|
|
||||||
private val classId: ClassId
|
private val classId: ClassId get() = RuntimeTypeMapper.mapJvmClassToKotlinClassId(jClass)
|
||||||
get() = RuntimeTypeMapper.mapJvmClassToKotlinClassId(jClass).also { result ->
|
|
||||||
if (!jClass.isAnonymousClass && !jClass.isLocalClass) {
|
|
||||||
assert(result.asSingleFqName().asString() == qualifiedName) {
|
|
||||||
"Incorrect class name computed for class ${jClass.name}. Result: $result. Expected qualified name $qualifiedName"
|
|
||||||
}
|
|
||||||
assert(result.shortClassName.asString() == simpleName) {
|
|
||||||
"Incorrect class name computed for class ${jClass.name}. Result: $result. Expected simple name $simpleName"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Note that we load members from the container's default type, which might be confusing. For example, a function declared in a
|
// Note that we load members from the container's default type, which might be confusing. For example, a function declared in a
|
||||||
// generic class "A<T>" would have "A<T>" as the receiver parameter even if a concrete type like "A<String>" was specified
|
// generic class "A<T>" would have "A<T>" as the receiver parameter even if a concrete type like "A<String>" was specified
|
||||||
@@ -205,9 +228,9 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) : KDeclaration
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override val simpleName: String? get() = ClassReference.getClassSimpleName(jClass)
|
override val simpleName: String? get() = data().simpleName
|
||||||
|
|
||||||
override val qualifiedName: String? get() = ClassReference.getClassQualifiedName(jClass)
|
override val qualifiedName: String? get() = data().qualifiedName
|
||||||
|
|
||||||
override val constructors: Collection<KFunction<T>> get() = data().constructors
|
override val constructors: Collection<KFunction<T>> get() = data().constructors
|
||||||
|
|
||||||
@@ -215,7 +238,13 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) : KDeclaration
|
|||||||
|
|
||||||
override val objectInstance: T? get() = data().objectInstance
|
override val objectInstance: T? get() = data().objectInstance
|
||||||
|
|
||||||
override fun isInstance(value: Any?): Boolean = ClassReference.isInstance(value, jClass)
|
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() = data().typeParameters
|
override val typeParameters: List<KTypeParameter> get() = data().typeParameters
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user