Determine the Parameter Names of a Java Method at Run-time

The Java reflection API does not allow you to determine parameter names of a method at run-time; however, you can accomplish this by using a library within the Apache Axis2 project to read parameter names of a Java method at run-time.  Here is an example…

import java.lang.reflect.*;
import org.apache.axis2.description.java2wsdl.bytecode.*;

public class Test {

  public static void main(String[] args) {

    try {
      // Nothing new here, just find the method we want to
      // lookup the parameter names
      Method m = Test.class.getMethod(
        "addNumbers",
        new Class[] {Long.class, Long.class}
      );

      // This uses the Apache Axis2 ChainedParamReader class to
      // determine the parameter names
      ChainedParamReader pr = new ChainedParamReader(Test.class);
      String[] paramNames = pr.getParameterNames(m);

      // Print the resulting paramNames
      for (String paramName : paramNames) {
        System.out.println(paramName);
      }
    }
    catch (Exception e) {
      System.err.println(e);
    }
  }

  public Long addNumbers(Long firstValue, Long secondValue) {
    return firstValue + secondValue;
  }
}

You can compile and execute this which result in…

C:\temp>javac -cp axis2-kernel-1.3.jar -g Test.java

C:\temp>java -cp .;axis2-kernel-1.3.jar Test

firstValuesecondValue

Important: You must compile your classes with the debug switch (-g) for this to work.  The ChainedParamReader class uses the debug information in the binary class file to determine the parameter names.

You can get the axis2-kernel-1.3.jar from http://ws.apache.org/axis2/.

4 Responses to “Determine the Parameter Names of a Java Method at Run-time”

  1. No offense, but why would I need to know the parameter names? (Unless I am making a decompiler of course).

  2. Мде

    Быстро сообразили ))))…

  3. ERIC says:


    Medicamentspot.com. Canadian Health&Care.Special Internet Prices.Best quality drugs.No prescription online pharmacy. Low price pills. Buy drugs online

    Buy:Zyban.Synthroid.Retin-A.100% Pure Okinawan Coral Calcium.Nexium.Actos.Human Growth Hormone.Prevacid.Valtrex.Prednisolone.Accutane.Lumigan.Mega Hoodia.Arimidex.Petcam (Metacam) Oral Suspension.Zovirax….

Leave a Reply