Exception when using reflections on a Kotlin class


#1

If I define a file named Foo.kt which contains:
fun bar() {}
kotlinc will produce a file FooKt.class

if I execute the following code:

Class.forName("FooKt").kotlin.functions

I get the following exception:

Exception in thread "main" kotlin.reflect.KotlinReflectionInternalError: Class not resolved: class FooKt

the call to functions is causing the exception.

Looking at the KClassImpl code it looks like the descriptor is not getting initialized, but I don’t know enough about the internal workings for a fix. Is there a way to introspect these classes using Kotlin’s reflections?


#2

It’s not currently possible with Kotlin’s reflection library. From Kotlin’s point of view, there’s no class named FooKt in your code, so .kotlin throws an exception as expected (the exception class and the message should be improved though). We’re planning to add support for introspection of packages and their members post-1.0, but the design is not finalized yet. Meanwhile, as a workaround, please use Java reflection for this task, if possible.