如何通过Kotlin中的反射获取参数名称? [英] How to get parameter names via reflection in kotlin?
问题描述
java8具有传递给编译器的"-parameters"参数,那么kotlin呢? 如我所见,kotlin编译器在参数中添加了带有参数名称的@JetValueParameter批注,但其中一个已被描述..
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..
推荐答案
更新: Reflection API现在支持获取参数名称:KCallable.parameters
将为您提供参数列表,并且KParameter.name
会给您命名.
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.
当前的解决方案是使用JetValueParameter
注释,尽管已弃用.
The current solution is to use the JetValueParameter
annotation, although it is deprecated.
我们正在努力支持Kotlin反射中的参数名称,这将很快提供.届时,将放弃JetValueParameter
,而使用新的API.
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.
我想指出,Kotlin编译器当前仅生成与Java 1.6兼容的JVM字节码,而与javac的-parameters
相对应的MethodParameters
属性仅出现在Java 1.8的字节码中.因此,您仍然无法通过 Java Reflection 获取Kotlin方法的参数名称.不过,我们正在考虑对字节码目标1.8的支持,因此将来可能会起作用.
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.
这篇关于如何通过Kotlin中的反射获取参数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!