From dc085c45b7baf19d1a0b6e8300232ed91180ab48 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 3 Mar 2016 13:04:49 +0300 Subject: [PATCH] Improve exception on creating KClass for classes which are not classes in Kotlin --- .../kotlin/reflect/jvm/internal/KClassImpl.kt | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt index 73d6e29335f..44192d46c89 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt @@ -22,6 +22,8 @@ import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.load.java.reflect.tryLoadClass import org.jetbrains.kotlin.load.java.structure.reflect.safeClassLoader +import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader +import org.jetbrains.kotlin.load.kotlin.reflect.ReflectKotlinClass import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.platform.JavaToKotlinClassMap @@ -41,7 +43,7 @@ internal class KClassImpl(override val jClass: Class) : KDeclaration if (classId.isLocal) moduleData.localClassResolver.resolveLocalClass(classId) else moduleData.module.findClassAcrossModuleDependencies(classId) - descriptor ?: throw KotlinReflectionInternalError("Class not resolved: $jClass") + descriptor ?: reportUnresolvedClass() } val descriptor: ClassDescriptor @@ -160,4 +162,31 @@ internal class KClassImpl(override val jClass: Class) : KDeclaration packagePrefix + classSuffix } } + + private fun reportUnresolvedClass(): Nothing { + val kind = ReflectKotlinClass.create(jClass)?.classHeader?.kind + when (kind) { + KotlinClassHeader.Kind.FILE_FACADE, KotlinClassHeader.Kind.MULTIFILE_CLASS, KotlinClassHeader.Kind.MULTIFILE_CLASS_PART -> { + throw UnsupportedOperationException( + "Packages and file facades are not yet supported in Kotlin reflection. " + + "Meanwhile please use Java reflection to inspect this class: $jClass" + ) + } + KotlinClassHeader.Kind.SYNTHETIC_CLASS -> { + throw UnsupportedOperationException( + "This class is an internal synthetic class generated by the Kotlin compiler, such as an anonymous class " + + "for a lambda, a SAM wrapper, a callable reference, etc. It's not a Kotlin class or interface, so the reflection " + + "library has no idea what declarations does it have. Please use Java reflection to inspect this class: $jClass" + ) + } + KotlinClassHeader.Kind.UNKNOWN -> { + // Should not happen since ABI-related exception must have happened earlier + throw KotlinReflectionInternalError("Unknown class: $jClass (kind = $kind)") + } + KotlinClassHeader.Kind.CLASS, null -> { + // Should not happen since a proper Kotlin- or Java-class must have been resolved + throw KotlinReflectionInternalError("Unresolved class: $jClass") + } + } + } }