How to get names of method's parameters


#1

Hi all

Is there possibility to get names of method’s parameters inside method code? Can anyone show example of it?


#2

this is my current "bad solution" (i have to write method name)

class Greeter() {

  fun greet(name: String) {
  val c = Greeter::class;
  for (m in c.memberFunctions) {
           if (m.name == “greet”) {
           val p = m.parameters
           println(p.toString())
           }
  }
  println(“Hello, ${name});
  }
}

fun main(args: Array<String>) {
  Greeter().greet(“UserName”)
}


#3

I found another solution

val p = Greeter::greet.parameters

but know I have one more question: How to get pairs ParamName, ParamValue at function greet?


#4

This is not possible. The JVM has no API that would allow to introspect the local variables or parameters of the current method invocation dynamically.