Java Reflection method get parameter types

Introduction

There are two methods that return a list of parameter a method takes.

public Class<?>[] getParameterTypes()  
public Type[] getGenericParameterTypes()  

The former returns an array of Class whereas the latter returns an array of Type.


import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class Main {
   public static void main(String args[]) throws Exception {

      Class c = Class.forName("java.lang.String");
      Method[] methods = c.getDeclaredMethods();
      for (Method m : methods) {
         System.out.println("\n\nMethod: " + m.getName());
         System.out.print("Parameters taken:");
         Type[] params = m.getGenericParameterTypes();
         for (Type p : params) {
            System.out.print("  " + p);
         }/*w  ww  .ja  v a2 s . c om*/
      }
   }
}



PreviousNext

Related