28

In Java 6, imagine 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 that was passed on to slice2 or any other parameter, the important bit here is that I want to get the value by parameter name.

I know how to get the list of parameter types with getParameterTypes or getGenericParameterTypes.

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

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
Tiago Veloso
  • 8,513
  • 17
  • 64
  • 85
  • This question is about Java 6. For more information about Java 8 onwards, please visit [this tutorial about the `-parameters` option for `javac`](https://docs.oracle.com/javase/tutorial/reflect/member/methodparameterreflection.html) – Maarten Bodewes Feb 02 '17 at 15:21
  • I put mustard at the top AND bottom of my sandwich, `bool` is not specific enough you insensitive clod – William Entriken Aug 08 '19 at 23:25

8 Answers8

58

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.

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Thanks, I am not using spring. Do you know where I can find some usage examples for `ClassReader`? – Tiago Veloso Jul 20 '11 at 10:03
  • 1
    look at the inspectClass(..) method here https://src.springframework.org/svn/spring-framework/trunk/org.springframework.core/src/main/java/org/springframework/core/LocalVariableTableParameterNameDiscoverer.java – Bozho Jul 20 '11 at 10:04
  • Link is broken. `inspectClass()` seems to have been removed from [Spring 3.0.x](http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/core/LocalVariableTableParameterNameDiscoverer.html). – David Moles Aug 27 '12 at 18:58
  • 1
    Does any one know how can I add the debug information when I'm using Maven? – Modi May 21 '14 at 10:27
  • Link broken to Spring source, see https://github.com/spring-projects/spring-framework/tree/master/spring-core/src/main/java/org/springframework/core. Helped hint the issue I had for my code (related to long/double variables). – Ben Oct 01 '20 at 12:02
  • How can do this in IntelliJ or Gradle? – Cardinal System Feb 05 '21 at 18:03
11

I have found another solution after marking this question as answered. The solution is Paranamer.

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
reevesy
  • 3,452
  • 1
  • 26
  • 23
Tiago Veloso
  • 8,513
  • 17
  • 64
  • 85
7

Since Java 1.8, this can be done as long as the parameter names are in the class files. Using javac this is done passing the -parameters flag. From the javac help

-parameters    Generate metadata for reflection on method parameters

From IDEs you will need to look at the compiler settings.

If the parameter names are in the class files then here is an example of doing this

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class ParameterNamesExamples {

  public static void main(String[] args) throws Exception {
    Method theDoSomethingMethod = ExampleClass.class.getMethods()[0];
    // Now loop through the parameters printing the names
    for(Parameter parameter : theDoSomethingMethod.getParameters()) {
      System.out.println(parameter.getName());
    }
  }

  private class ExampleClass {
    public void doSomething(String myFirstParameter, String mySecondParameter) {
      // No-op
    }
  }
}

The output will depend on if the parameter names are in the class files. If they are the output is:

myFirstParameter
mySecondParameter

If not the output is:

arg0
arg1

More information on this from Oracle can be found at Obtaining Names of Method Parameters

James Mudd
  • 1,816
  • 1
  • 20
  • 25
4

In addition to this answer: "Parameter names are available if you have told the compiler to include them"

If you're using Eclipse go to project -> properties -> Java Compiler -> check "Store information about method parameters (usable via reflection)

Community
  • 1
  • 1
Denis K
  • 150
  • 1
  • 9
2

This is not possible. Class files do not contains the argument names, as you can see with your IDE's autocompletion when the source is not available.

Therefore, the reflection API is not able to give out parameter names.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
slaphappy
  • 6,894
  • 3
  • 34
  • 59
2

In Java parameter names are not available via reflection.

Leonard Brünings
  • 12,408
  • 1
  • 46
  • 66
0

You can simply assign the value of the parameter 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());
}
mastaH
  • 1,234
  • 3
  • 15
  • 30
0

Do you own the code of the method? You could annotate the parameters and pass names as arguments @Param("slice1"). Later you will be able to get the annotation and extract parameter name from it.

denis.solonenko
  • 11,645
  • 2
  • 28
  • 23