天天看點

java reflect 擷取對象的方法及參數



import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class TestReflect {

 public static void main(String[] args) throws  ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

  Class<?> class1=Class.forName("TestReflect");

  Object o=class1.newInstance();

  Method[] m= o.getClass().getMethods();

  for(int i=0;i<m.length;i++){

  Method ms=m[i];

  System.out.println(ms.getName());

   Class<?>[] parameterTypes = ms.getParameterTypes();

         for (Class<?> clas : parameterTypes) {

             String parameterName = clas.getName();

             System.out.println("參數名稱:" + parameterName);

         }

  }

 }

 public TestReflect(){

  System.out.println("TestReflect");

 }

 public void play(String str){

  System.out.println(str);

 }

}