Getting the Name of a Method Parameter

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.

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.

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.

Obtaining name of a method parameter from the corresponding argument in a method invocation in Roslyn

Sadly I don't think there is a good public way to do this. See Roslyn's internal DetermineParameter helper for something that might help.

How to get method parameter names?

Take a look at the inspect module - this will do the inspection of the various code object properties for you.

>>> inspect.getfullargspec(a_method)
(['arg1', 'arg2'], None, None, None)

The other results are the name of the *args and **kwargs variables, and the defaults provided. ie.

>>> def foo(a, b, c=4, *arglist, **keywords): pass
>>> inspect.getfullargspec(foo)
(['a', 'b', 'c'], 'arglist', 'keywords', (4,))

Note that some callables may not be introspectable in certain implementations of Python. For Example, in CPython, some built-in functions defined in C provide no metadata about their arguments. As a result, you will get a ValueError if you use inspect.getfullargspec() on a built-in function.

Since Python 3.3, you can use inspect.signature() to see the call signature of a callable object:

>>> inspect.signature(foo)
<Signature (a, b, c=4, *arglist, **keywords)>

How can you get the names of method parameters?

public static string GetParamName(System.Reflection.MethodInfo method, int index)
{
string retVal = string.Empty;

if (method != null && method.GetParameters().Length > index)
retVal = method.GetParameters()[index].Name;

return retVal;
}

The above sample should do what you need.



Related Topics



Leave a reply



Submit