Home > Enterprise >  Getting a Method from a method reference
Getting a Method from a method reference

Time:09-14

I have a method inside a class:

class Foo {
    fun bar()
}

And I want to pass the method bar to a third-party library method that takes a java.lang.reflect.Method argument:

   fun baz(Method method)

How do I do that? Ideally is there some function call f(Foo::bar) that takes a method reference and returns a Method?

CodePudding user response:

With dependency org.jetbrains.kotlin:kotlin-reflect you can write

import kotlin.reflect.jvm.javaMethod

val method = Foo::bar.javaMethod
  • Related