Can I obtain method parameter name using Java reflection?
To summarize:
- getting parameter names is possible if debug information is included during compilation. See this answer for more details
- otherwise getting parameter names is not possible
- getting parameter type is possible, using
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:
- use
arg0
,arg1
,arg2
etc. - use
intParam
,stringParam
,objectTypeParam
, etc. - use a combination of the above - the former for non-primitive types, and the latter for primitive types.
- don't show argument names at all - just the types.
How to get parameter names with Java reflection
To get the method i of a class C you call C.class.getMethods()[i].toString()
.
EDIT: Obtaining parameter names is not possible using the reflection API.
But wen you compiled your class with debug information, it is possible to extract the information from bytecode. Spring does it using the ASM bytecode engineering library.
See this answer for further information.
How can I get the parameter name in java at run time
Starting from Java 8, you can use Reflection API to retrieve parameters names:
Method someMethod = Main.class.getDeclaredMethod("someMethod");
Parameter[] parameters = someMethod.getParameters();
for(Parameter parameter : parameters)
{
System.out.println(parameter.getName());
}
Also, see JavaDoc of Parameter#getName()
:
Returns the name of the parameter. If the parameter's name is present, then this method returns the name provided by the class file. Otherwise, this method synthesizes a name of the form argN, where N is the index of the parameter in the descriptor of the method which declares the parameter.
Getting the name of a method parameter
Parameter names are available if you have told the compiler to include them (compile with debug information). Spring has ParameterNameDiscoverer
which can help you obtain the names. The default implementation uses asm ClassReader
to do so.
With javac
you should include the -g
argument to include debug information. With Eclipse I think it is there by default; it can be configured using the preferences: Java -> Compiler and then enable "Store information about method parameters (usable via reflection)" (see also this answer).
Some frameworks use this. For example spring-mvc has @RequestParam
which defaults to the param name, if resolvable. It also supports explicit naming - @RequestParam("foo")
in case no debug information is provided.
Is there a way to obtain names of method parameters in Java?
We created a custom annotation for the method that holds a String[] of parameter names. This approach felt a little easier to manage than having to annotate each individual parameter. We plan to add build-time checking that the number of annotated parameter names matches the number of arguments, since that it what we require.
Related Topics
How to Use a Delimiter with Scanner.Usedelimiter in Java
Arithmeticexception: "Non-Terminating Decimal Expansion; No Exact Representable Decimal Result"
Split String on Spaces in Java, Except If Between Quotes (I.E. Treat \"Hello World\" as One Token)
Java Random Numbers Using a Seed
How Good Is Java's Uuid.Randomuuid
How Come Invoking a (Static) Method on a Null Reference Doesn't Throw Nullpointerexception
Java List.Contains(Object with Field Value Equal to X)
Why Do These Two Multiplication Operations Give Different Results
Spring 4 Loading Static Resources
Remove All Occurrences of Char from String
What Is This Spring.Jpa.Open-In-View=True Property in Spring Boot
How to Parse Date String to Date
Java Simpledateformat("Yyyy-Mm-Dd'T'Hh:Mm:Ss'Z'") Gives Timezone as Ist