Java反射详细例子

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 < 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 < pvec.length; j++)
    System.out.println("param #" + j + " " + pvec[j]);
   
   Class evec[] = m.getExceptionTypes();//取得方法抛出的异常对象类型
   for (int j = 0; j < 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());
 }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值