Java 运行时获取方法参数名
本文整理 Java 运行时获取方法参数名的两种方法,Java 8 的最新的方法和 Java 8 之前的方法。 Java 8 的新特性 翻阅 Java 8 的 新特性 ,可以看到有这么一条“ JEP 118 : Access to Parameter Names at Runtime”。这个特性就是为了能运行时获取参数名新加的。这个 JEP 只是功能增强的提案,并没有最终实现的 JDK 相关的 API 的介绍。查看“ Enhancements to the Reflection API ” 会看到如下介绍: Enhancements in Java SE 8 Method Parameter Reflection: You can obtain the names of the formal parameters of any method or constructor with the method java.lang.reflect.Executable.getParameters . However, .class files do not store formal parameter names by default. To store formal parameter names in a particular .class file, and thus enable