方法一:如果項目依賴spring,可以使用
String[] parameterNames = new LocalVariableTableParameterNameDiscoverer().getParameterNames(methods);
上面的methods是通過反射獲得的Method對象
方法二:jdk1.8提供了獲取參數名的方法
但是在編譯的時候要加上–parameters參數,如果不加這個參數會得到參數名為arg0...
Parameter[] parameters = methods.getParameters();
System.out.println(parameters[0].getName());
但是如果使用maven,只需要在編譯插件上加上一個配置<compilerArgument>-parameters</compilerArgument>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-parameters</compilerArgument>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>