Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

310
Views
How to get Methods from KClass in Kotlin

In java when i have a Class<> instance, i have the getMethod that return a Method and it is very easy to use. As i can see Kotlin makes life harder because such a method doesn't exist. is there a nice way to get a specific method beside of turning the KClass to Class and then using it exactly as in Java?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Supposing you have this class:

class Test {
    fun sayHello() = println("Hello world")

    companion object {
        fun foo() = println("foo")
    }
}

This gives you a function instance you can call with invoke() (which is an operator function, so you can just use parentheses like with any other function. If you get the function using the class name like this, it is not bound to an instance. So you pass the instance as the first parameter when invoking. (If sayHello() had additional parameters, they would just go after the first parameter of Test.)

val sayHelloFunction = Test::sayHello
val test = Test()
helloWorldFunction(test) // same as calling test.sayHello()

You can also get a function reference that's bound to an instance like this. It cannot be called on other instances of Test.

val test = Test()
val sayHelloFunction = test::sayHello
helloWorldFunction() // same as calling test.sayHello()

The above method doesn't require the Kotlin reflection library.

To get a member by String name, there isn't a specific function like Java's getMethod, so you have to pick it out of the list of members, which are KCallable. KCallable doesn't have the operator fun invoke, so you have to use call to call it, or cast it to a specific function type (in this case it would be (Test) -> Unit).

val sayHelloCallable = Test::class.members.single { it.name == "sayHello" }
val test = Test()
sayHelloCallable.call(test) // same as calling test.sayHello()

This does not list the members of the companion object, as that is a different class. For that you would have to get the companion's class:

Test.Companion::class.members.single { it.name == "foo" }

I don't know if there's a way to get an instance bound version by name.

As for why there's no convenient getMethod function for KClass, I can only guess that because Kotlin classes have so many different types of members (properties, functions, delegates, backing fields), that maybe they want to limit API bloat and feel that the inline filtering functions like single are adequate.

over 4 years ago · Santiago Trujillo Report

0

To get a Class from a KClass, you just call .java on it (and .kotlin to go the other way).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!