Improve exception on creating KClass for classes which are not classes in Kotlin

This commit is contained in:
Alexander Udalov
2016-03-03 13:04:49 +03:00
parent 4553afbd0c
commit dc085c45b7
@@ -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<T : Any>(override val jClass: Class<T>) : 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<T : Any>(override val jClass: Class<T>) : 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")
}
}
}
}