Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the declared functions of a Kotlin class (KClass in M12)?

Basically that. I'm wondering how to get the functions/methods given a KClass... looks like I can only iterate over the properties and extension properties.

like image 787
Alejandro Navas Avatar asked Sep 16 '25 20:09

Alejandro Navas


2 Answers

Update: You can now get functions of a class with extensions declared in package kotlin.reflect: functions, declaredFunctions, memberFunctions, staticFunctions, etc.

Kotlin reflection is a work in progress at the moment. We plan to ship API for introspecting functions in the next milestone, presumably at the end of this summer.

Meanwhile, the only workaround is to use Java reflection for this task.

like image 104
Alexander Udalov Avatar answered Sep 18 '25 17:09

Alexander Udalov


Kotlin reflection is more full featured in later milestones include the 1.0 betas.

View the overview documentation for Kotlin Reflection and specifically that of KClass.

For example, given a class you can see member functions using:

val functions = Someclass::class.declaredMemberFunctions

or properties:

val properties = Someclass::class.declaredMemberProperties

And to get from a Java Class to a KClass:

val kclz = this.javaClass.kotlin

See also: Kotlin.reflect package API docs

like image 39
Jayson Minard Avatar answered Sep 18 '25 18:09

Jayson Minard