How can I dynamically get a function by name in Kotlin ?
i.e:
fun myFunc11() { println("Very useful function 11") }
val funcName = "myFunc" + 11
val funcRef = getFunction(funcName)
funcRef()
Edit: The accepted answer appears to be correct, however the code is currently hitting some bug in Kotlin. Bug report submitted: https://youtrack.jetbrains.com/issue/KT-10690
The first parameter is the method name to call and the second one the list of parameters. class MethodPicker() { val commands = listOf("foo", "tango") fun messageReceiver() { messageParam : String = "Hi stackoverflow!" invokeMethod(commands.
In Kotlin, Reflection is a combination of language and library capabilities that allow you to introspect a program while it's running. Kotlin reflection is used at runtime to utilize a class and its members, such as properties, methods, and constructors.
The global functions such as fun myFunc11() { ... }
defined in file named i.e. Global.kt
are compiled to static
methods on a class named GlobalKt
as described in the documentation.
To get a function reference by name you'll need to load the class that defines it. If you know the file name that defines the function reference you're trying to find you can do:
fun getFunctionFromFile(fileName: String, funcName: String): KFunction<*>? {
val selfRef = ::getFunctionFromFile
val currentClass = selfRef.javaMethod!!.declaringClass
val classDefiningFunctions = currentClass.classLoader.loadClass("${fileName}Kt")
val javaMethod = classDefiningFunctions.methods.find { it.name == funcName && Modifier.isStatic(it.modifiers)}
return javaMethod?.kotlinFunction
}
Then you can find and call function defined in Global.kt
file:
fun myFunc11() { println("Very useful function 11") }
like so:
val kFunction = getFunctionFromFile("Global", "myFunc11")
kFunction?.call()
However the above is pretty useless. A better solution would be to search through all classes available in classpath and suffixed with Kt
to reach all global functions. However due to nature of jvm class loaders this is a bit more involved as described in this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With