From 3a78ab6d4091c70a3ed63662fefcfd0781cc958b Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 27 Dec 2019 17:03:43 +0300 Subject: [PATCH] Do not use qualifiedName property in KClass.cast function, because it's not supported in K/JS Introduce an internal property that returns either qualified or simple name, and use it instead. --- libraries/stdlib/js/src/kotlin/reflect/KClassesImpl.kt | 9 +++++++++ libraries/stdlib/jvm/src/kotlin/reflect/KClassesImpl.kt | 9 +++++++++ libraries/stdlib/src/kotlin/reflect/KClasses.kt | 5 ++++- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 libraries/stdlib/js/src/kotlin/reflect/KClassesImpl.kt create mode 100644 libraries/stdlib/jvm/src/kotlin/reflect/KClassesImpl.kt diff --git a/libraries/stdlib/js/src/kotlin/reflect/KClassesImpl.kt b/libraries/stdlib/js/src/kotlin/reflect/KClassesImpl.kt new file mode 100644 index 00000000000..5783a451bfb --- /dev/null +++ b/libraries/stdlib/js/src/kotlin/reflect/KClassesImpl.kt @@ -0,0 +1,9 @@ +/* + * 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. + */ + +package kotlin.reflect + +internal actual inline val KClass<*>.qualifiedOrSimpleName: String? + get() = simpleName \ No newline at end of file diff --git a/libraries/stdlib/jvm/src/kotlin/reflect/KClassesImpl.kt b/libraries/stdlib/jvm/src/kotlin/reflect/KClassesImpl.kt new file mode 100644 index 00000000000..17baee448c4 --- /dev/null +++ b/libraries/stdlib/jvm/src/kotlin/reflect/KClassesImpl.kt @@ -0,0 +1,9 @@ +/* + * 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. + */ + +package kotlin.reflect + +internal actual inline val KClass<*>.qualifiedOrSimpleName: String? + get() = qualifiedName \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/reflect/KClasses.kt b/libraries/stdlib/src/kotlin/reflect/KClasses.kt index 1265d790d87..92097bd51a1 100644 --- a/libraries/stdlib/src/kotlin/reflect/KClasses.kt +++ b/libraries/stdlib/src/kotlin/reflect/KClasses.kt @@ -23,10 +23,13 @@ import kotlin.internal.LowPriorityInOverloadResolution @ExperimentalStdlibApi @LowPriorityInOverloadResolution fun KClass.cast(value: Any?): T { - if (!isInstance(value)) throw ClassCastException("Value cannot be cast to $qualifiedName") + if (!isInstance(value)) throw ClassCastException("Value cannot be cast to $qualifiedOrSimpleName") return value as T } +// TODO: replace with qualifiedName when it is fully supported in K/JS +internal expect val KClass<*>.qualifiedOrSimpleName: String? + /** * 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.