Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to obtain names of method parameters in Java? [duplicate]

I'm writing small and very DRY framework, which heavily relies on metadata. I'd like to know if there is a way to obtain 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 annotate parameters, but that wouldn't be nice...

public void a(
    @Param("myIntParam") int myIntParam,
    @Param("theString") String theString
) { ... }
like image 646
ansgri Avatar asked Dec 19 '08 16:12

ansgri


People also ask

Can I obtain method parameter name using Java reflection?

You can obtain the names of the formal parameters of any method or constructor with the method java. lang. reflect.

Which of the following method can be used to read parameter names?

Full Stack Java developer - Java + JSP + Restful WS + Spring Following is a generic example which uses getParameterNames() method of HttpServletRequest to read all the available form parameters. This method returns an Enumeration that contains the parameter names in an unspecified order.

What is the name of the method of parameter passing that Java uses?

We learned that parameter passing in Java is always Pass-by-Value. However, the context changes depending upon whether we're dealing with Primitives or Objects: For Primitive types, parameters are pass-by-value.


2 Answers

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

Cons:

  • Requires that you know the location of compiled class file.
  • It has to be compiled with the -g flag.

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

like image 180
Jonny Heggheim Avatar answered Oct 07 '22 03:10

Jonny Heggheim


I could be wrong about this... but I don't think parameter names appear in a class file so I would guess that there is no way to get them via reflection.

like image 27
bobwienholt Avatar answered Oct 07 '22 03:10

bobwienholt