Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get parameter names via reflection in kotlin?

java8 has "-parameters" argument, passed to compiler, what about kotlin ? As I can see, kotlin compiler add @JetValueParameter annotation with param names to parameters, but one is depricated..

like image 208
Евгений Савин Avatar asked Dec 25 '22 19:12

Евгений Савин


1 Answers

Update: Reflection API now supports obtaining parameter names: KCallable.parameters will give you a list of parameters, and KParameter.name will give you the name.

The current solution is to use the JetValueParameter annotation, although it is deprecated.

We're working on support for the parameter names in Kotlin reflection, which will be available shortly. At that point JetValueParameter will be dropped in favor of the new API.

I'd like to note that Kotlin compiler currently only produces JVM bytecode compatible with Java 1.6, whereas the MethodParameters attribute corresponding to the -parameters of javac only appeared in bytecode of Java 1.8. So, you'll still be unable to get parameter names of Kotlin methods via Java reflection. We're considering the support for bytecode target 1.8 though, so that will likely work in the future.

like image 117
Alexander Udalov Avatar answered Dec 28 '22 07:12

Alexander Udalov