If I have a class like this:
public class Whatever
{
public void aMethod(int aParam);
}
is there any way to know that aMethod
uses a parameter named aParam
, that is of type int
?
If I have a class like this:
public class Whatever
{
public void aMethod(int aParam);
}
is there any way to know that aMethod
uses a parameter named aParam
, that is of type int
?
Can I obtain method parameter name using Java reflection?
No, that's not possible. This information (the parameter name, obviously not the parameter type as most other responders here seem to misinterpret) is already lost after compiling. Only the method name, parameter types and return type are known, as outlined in the Sun's reflection tutorial and Method
API.
Why would you need to know about it then?
The only way to know about them is parsing the sourcecode or the Javadocs.
You can retrieve the method with reflection and detect it's argument types. Check http://java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/Method.html#getParameterTypes%28%29
However, you can't tell the name of the argument used.
Parameter names are only useful to the compiler. When the compiler generates a class file, the parameter names are not included - a method's argument list only consists of the number and types of its arguments. So it would be impossible to retrieve the parameter name using reflection (as tagged in your question) - it doesn't exist anywhere.
However, if the use of reflection is not a hard requirement, you can retrieve this information directly from the source code (assuming you have it).
To summarize:
method.getParameterTypes()
For the sake of writing autocomplete functionality for an editor (as you stated in one of the comments) there are a few options:
arg0
, arg1
, arg2
etc.intParam
, stringParam
, objectTypeParam
, etc.To add my 2 cents; parameter info is available in a class file "for debugging" when you use javac -g to compile the source. And it is available to APT but you'll need an annotation so no use to you. (Somebody discussed something similar 4-5 years ago here: http://forums.java.net/jive/thread.jspa?messageID=13467&tstart=0 )
Overall in-short you can't get it unless you work on Source files directly (similar to what APT does at compile time).
It is possible and Spring MVC 3 does it, but I didn't take the time to see exactly how.
The matching of method parameter names to URI Template variable names can only be done if your code is compiled with debugging enabled. If you do have not debugging enabled, you must specify the name of the URI Template variable name in the @PathVariable annotation in order to bind the resolved value of the variable name to a method parameter. For example:
Taken from the spring documentation
The Paranamer library was created to solve this same problem.
It tries to determine method names in a few different ways. If the class was compiled with debugging it can extract the information by reading the bytecode of the class.
Another way is for it to inject a private static member into the bytecode of the class after it is compiled, but before it is placed in a jar. It then uses reflection to extract this information from the class at runtime.
http://paranamer.codehaus.org/
I had problems using this library, but I did get it working in the end. I'm hoping to report the problems to the maintainer.
and how does a decompiler gets the parameter name if it is not in the Byte code ?