Is there a way to get method parameter names in Java?

I am writing a small and very DRY framework that is heavily dependent on metadata. I would like to know if there is a way to get the method parameter names, i.e. Given some method

public void a(int myIntParam, String theString) { ... } 

get the strings "myIntParam" and "theString" .

I know I could comment on the parameters, but that would be nice ...

 public void a( @Param("myIntParam") int myIntParam, @Param("theString") String theString ) { ... } 
+17
java reflection
Dec 19 '08 at 16:50
source share
8 answers

We created a custom annotation for a method that contains String [] parameter names. This approach has been simplified a bit than the need to comment on each individual parameter. We plan to add a build time check so that the number of names of annotated parameters matches the number of arguments, as this is what we need.

+4
Mar 30 '09 at 22:33
source share

Here is a dirty solution that needs some tweaking. Maybe someone can do it better.

Minuses:

  • It is required that you know the location of the compiled class file.
  • It must be compiled with the -g flag.

The code:

 import com.sun.org.apache.bcel.internal.classfile.ClassParser; import com.sun.org.apache.bcel.internal.classfile.JavaClass; import com.sun.org.apache.bcel.internal.classfile.LocalVariable; import com.sun.org.apache.bcel.internal.classfile.Method; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { ClassParser parser = new ClassParser("Main.class"); JavaClass clazz = parser.parse(); for (Method m : clazz.getMethods()) { System.out.println("Method: " + m.getName()); int size = m.getArgumentTypes().length; if (!m.isStatic()) { size++; } for (int i = 0; i < size; i++) { LocalVariable variable = m.getLocalVariableTable().getLocalVariable(i); System.out.println(" - Param: " + variable.getName()); } } } public void a(int myIntParam, String theString) { } } 

Output:

$ javac -g Main.java
$ java Main
Method: <init>
- Param: this
Method: main
- Param: args
Method: a
- Param: this
- Param: myIntParam
- Param: theString

+15
Dec 21 '08 at 20:50
source share

Not really, but code books have this library, which will do for many purposes: http://paranamer.codehaus.org/

+13
Dec 19 '08 at 17:07
source share

I could be wrong about that ... but I don’t think that the parameter names appear in the class file, so I assume that there is no way to get them through reflection.

+8
Dec 19 '08 at 16:54
source share

The parameter name is present in the class file when the Java code was compiled with debugging information (using the -g option). Then the class file contains the LocalVariableTable attribute (see http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#5956 ). Here, the names of local variables and parameters are only local variables. Parameters correspond to variable slots starting at index 1 (or index 0 for static methods).

+3
Dec 21 '08 at 19:28
source share

If you use Spring, you're in luck. Just add this to your applicationContext.xml:

 <bean class="org.springframework.core.LocalVariableTableParameterNameDiscoverer"/> 

Then you can enter this bean where necessary:

 @Autowired private ParameterNameDiscoverer parameterNameDiscoverer; Method m = ... String[] names = parameterNameDiscoverer.getParameterNames(m); 

As the name suggests, this implementation is based on compiling classes with debugging information.

+3
Sep 01 '10 at 18:52
source share

@bobwienholt is correct - parameter names are not compiled into java classes and therefore are not available at runtime.

+2
Dec 19 '08 at 17:01
source share

Parameter names are available through apt (now part of javac).

0
Dec 21 '08 at 16:30
source share



All Articles