Is there an easy to to get the `Class`/`KClass` ob...
# getting-started
t
Is there an easy to to get the `Class`/`KClass` object of a top-level .kt file?
k
What's a top level file? What if it only has functions? What if it has multiple classes in it?
t
Suppose you declare a file named
Util.kt
with
Copy code
fun foo() { ... }
fun bar() { ... }
In Java you'd be able to access these by writing
Copy code
UtilKt.foo()
And you'd also be able to get the class:
Copy code
UtilKt.class
Can I do the same in Kotlin?
I understand that this isn't appropriate for other backends... but I'm surprised I can't do this even specifically for Kotlin/JVM
j
I guess the wrapping class only exists so that Java can use such declarations, but is not supposed to exist from Kotlin's point of view. What is your ultimate goal here? What do you want to achieve with this class instance?
t
I would like to list all of the utility functions in
UtilKt.class
reflectively, e.g.
Copy code
UtilKt::class.declaredFunctions
s
Put them in a kotlin class?
e
not even
Copy code
Class.forName("UtilKt").kotlin.declaredFunctions
will work, as "UtilKt" is not a Kotlin class, it is a Kotlin file facade class
you may be interested in following https://youtrack.jetbrains.com/issue/KT-21599 https://youtrack.jetbrains.com/issue/KT-16479 but it doesn't look like there's any plans for them
t
Thank you! I've bookmaked those issues for now 🙂