Getting the name of a method parameter

In Java 6 , imagine that I have the following method signature:

public void makeSandwich(Bread slice1, Bread slice2, List<Filling> fillings, boolean mustard) 

I would like to know, at runtime, the value passed to slice2 or any other parameter, the important bit here is that I want to get the value by the parameter name.

I know how to get a list of parameter types using getParameterTypes or getGenericParameterTypes .

Ideally, I would like to get a list of parameter names instead of types. Is there any way to do this?

+15
java reflection runtime java-6
Jul 20 2018-11-11T00:
source share
7 answers

Parameter names are available if you told the compiler to enable them (compile with debugging information). Spring has a ParameterNameDiscoverer to help you get the names. the default implementation uses asm ClassReader to do therefore.

With javac you must include the -g argument to enable debugging information. With Eclipse, I think it is by default; it can be configured using the settings: Java → Compiler and then enable "Save information about the method parameters (can be used with reflection)" (see also this answer ).

Some structures use this. For example, spring -mvc has @RequestParam , which by default has a parameter name if it is resolvable. It also supports explicit naming - @RequestParam("foo") in the absence of debugging information.

+42
Jul 20 2018-11-11T00:
source share

I found another solution after he answered this question. Paranamer Solution.

Example:

  Method method = Foo.class.getMethod(...); Paranamer paranamer = new CachingParanamer(); String[] parameterNames = paranamer.lookupParameterNames(method) // throws ParameterNamesNotFoundException if not found // or ... parameterNames = paranamer.lookupParameterNames(method, false) // will return null if not found 
+9
Jul 20 2018-11-21T00:
source share

It's impossible. Class files do not contain argument names, as you can see with IDE completion when the source is unavailable.

Therefore, the reflection API cannot provide parameter names.

+2
Jul 20 2018-11-11T00:
source share

In parameter names, Java is not accessible through reflection.

+2
Jul 20 2018-11-11T00: 00Z
source share

In addition to this, the answer is: “Parameter names are available if you told the compiler to include them”

If you use Eclipse, go to the project -> properties -> Java compiler -> check "Save information about method parameters (can be used with reflection)

+1
May 4 '16 at 7:26
source share

You can simply assign a parameter value to another value

 Bread slice2; public void makeSandwich(Bread slice1, Bread slice2, List<Filling> fillings, boolean mustard) { this.slice2 = slice2; System.out.println(this.slice2.getSomething()); } 
0
Jul 20 2018-11-11T00: 00Z
source share

Do you have a method code? You could annotate the parameters and pass the names as arguments to @Param("slice1") . You can later get the annotation and extract the parameter name from it.

0
Jul 20 '11 at 9:50 a.m.
source share



All Articles