The Java reflection API does not allow you to determine parameter names of a method at run-time; however, you can accomplish this by using a library within the Apache Axis2 project to read parameter names of a Java method at run-time. Here is an example…
import java.lang.reflect.*;
import org.apache.axis2.description.java2wsdl.bytecode.*;public class Test {
public static void main(String[] args) {
try {
// Nothing new here, just find the method we want to
// lookup the parameter names
Method m = Test.class.getMethod(
"addNumbers",
new Class[] {Long.class, Long.class}
);// This uses the Apache Axis2 ChainedParamReader class to
// determine the parameter names
ChainedParamReader pr = new ChainedParamReader(Test.class);
String[] paramNames = pr.getParameterNames(m);// Print the resulting paramNames
for (String paramName : paramNames) {
System.out.println(paramName);
}
}
catch (Exception e) {
System.err.println(e);
}
}public Long addNumbers(Long firstValue, Long secondValue) {
return firstValue + secondValue;
}
}
You can compile and execute this which result in…
C:\temp>javac -cp axis2-kernel-1.3.jar -g Test.java C:\temp>java -cp .;axis2-kernel-1.3.jar Test firstValuesecondValue
Important: You must compile your classes with the debug switch (-g) for this to work. The ChainedParamReader class uses the debug information in the binary class file to determine the parameter names.
You can get the axis2-kernel-1.3.jar from http://ws.apache.org/axis2/.