---
title: "Java反射详细例子"
author: "scorpio"
date: "2007-08-21T18:13:13Z"
tags: []
slug: "java"
draft: false
lastmod: "2007-08-21T18:13:13Z"
id: 114959
url: "https://scorpio.iteye.com/blog/114959"
---

package com.wxf.reflect;

import java.lang.reflect.Field;  
import java.lang.reflect.InvocationTargetException;  
import java.lang.reflect.Method;

/\**  
 * Java 反射测试  
 * @author Administrator  
 *  
 \*/

public class ReflectTest {

 /\**  
  * For the test of method :getAllMethodList()  
  * @param obj  
  * @param x  
  * @return  
  * @throws NullPointerException  
  \*/  
 private ReflectTest testMethod(Object obj, int x) throws NullPointerException {  
  if (obj == null)  
   throw new NullPointerException();  
  return new ReflectTest();  
 }

 /\**  
  * 获取类中所有的方法列表  
  * 方法名称、行参类型、异常类型、返回值类型  
  \*/  
 private void getAllMethodList(Class c){

  Method methlist\[] = c.getDeclaredMethods();//c.getMethods()可获取继承来的所有方法  
  for (int i = 0; i &lt; methlist.length; i++) {

   Method m = methlist\[i];  
   System.out.println("name = " + m.getName());//方法名  
   System.out.println("declaring  class = " + m.getDeclaringClass());//所在的类  
     
   Class pvec\[] = m.getParameterTypes();//取得方法的形参类型  
   for (int j = 0; j &lt; pvec.length; j++)  
    System.out.println("param #" + j + " " + pvec\[j]);  
     
   Class evec\[] = m.getExceptionTypes();//取得方法抛出的异常对象类型  
   for (int j = 0; j &lt; evec.length; j++)  
    System.out.println("exc #" + j + " " + evec\[j]);  
     
   System.out.println("return type = " + m.getReturnType());//取得方法返回值的类型  
   System.out.println("-----");  
  }   
 }  
   
 /\**  
  * For the test of  method "excuteMethodByMethodName"  
  * @param a  
  * @param b  
  * @return  
  \*/  
 public int add(int a ,int b){  
  return a+b;  
 }  
   
   
 /\**  
  * 动态调用方法  
  * @param c  
  * @return  
  \*/  
 public Integer excuteMethodByMethodName(Class c,ReflectTest rt){  
    
  Object resultObj = null;//存储结果对象  
  Method md = null;  
    
  Class\[] ptpyes=new Class\[2];//预定义要被调用的方法参数列表  
  ptpyes\[0]=Integer.TYPE;  
  ptpyes\[1]=Integer.TYPE;  
    
  try {  
   md = c.getMethod("add", ptpyes);//add是要被调用的方法名;通过行参ptpyes定位到具体哪个方法被调用(考虑多态的情况)  
  } catch (SecurityException e) {  
   e.printStackTrace();  
  } catch (NoSuchMethodException e) {  
   e.printStackTrace();  
  }  
    
  Object\[] argus=new Object\[2];//argus是传给被调用方法的实参  
  argus\[0]=new Integer(22);  
  argus\[1]=new Integer(33);  
    
  try {  
            //通过Method md      定位要被调用的方法名;  
   //通过ReflectTest rt 定位要被调用的方法所在的类；  
   //通过Object\[] argus 将实参传递给被调用的方法  
   resultObj = md.invoke(rt, argus);    
  } catch (IllegalArgumentException e) {  
   e.printStackTrace();  
  } catch (IllegalAccessException e) {  
   e.printStackTrace();  
  } catch (InvocationTargetException e) {  
   e.printStackTrace();  
  }  
  Integer result=(Integer)resultObj;  
  return result;  
 }  
   
 public static void main(String args\[]) {  
  Class c=null;  
  try {  
   c=Class.forName("com.wxf.reflect.ReflectTest");  
  } catch (ClassNotFoundException e) {  
   e.printStackTrace();  
  }  
  ReflectTest rt=new ReflectTest();  
  rt.getAllMethodList(c);  
  Integer itg=rt.excuteMethodByMethodName(c, rt);  
  System.out.println(itg.intValue());  
 }

```
}
```