Java 动态代理与反射机制

java动态代理必须的两个类与两个接口:

首先需要有一个接口(委托者需要实现该接口的方法)示例如下:

<pre name="code" class="html">public interface TheInterface{
public void printStr(String str);
}



然后当然需要一个委托者类来实现上述接口:

public class TheDelegator implements TheInterface {

	@Override
	public void printStr(String str) {
		System.out.println(str);

	}

}

然后我们需要一个类来负责生成代理对象(就是它要替委托者执行任务)

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;


public class DynamicProxy implements InvocationHandler {
	//这个就是我们要代理的委托者对象
private Object delegator;
//构造方法,为我们要代理的委托者对象赋值
public DynamicProxy(Object object){
	this.delegator=object;
}
//实现InvocationHandler 接口中最关键也是唯一的一个方法
	@Override
	public Object invoke(Object arg0, Method arg1, Object[] arg2)
			throws Throwable {
		//在代理委托者对象之前我们可以添加一些自己的操作
		System.out.println("这是代理委托者对象之前我自己的操作");
		//当代理对象调用委托者对象的方法时,其会自动跳转到代理对象关联的hander对象的invoke方法来进行调用。
		//最关键的代码
		arg1.invoke(delegator,arg2);
		//在代理真实对象后我们也可以添加一些自己的操作
		System.out.println("这是代理委托者对象之后我自己的操作");
		return null;
	}

}

在这里要注意,使用java动态代理,一定要写一个类来生成代理对象,而这个类一定是实现了InvocationHandler 接口。
最后就是我们的测试类,来验证动态代理是否成功。

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Test
{
    public static void main(String[] args)
    {
        //    我们要代理的委托者对象
        TheInterface delegator= new TheDelegator();
     
        // 传入我们要代理的委托者对象,生成负责创建代理的对象(注意是fuze创建代理的对象,还不是实际的代理对象)
        InvocationHandler handler = new DynamicProxy(delegator);

        /*
         * 通过Proxy的newProxyInstance方法来创建我们的代理对象,我们来看看其三个参数
         * 第一个参数 handler.getClass().getClassLoader() ,我们这里使用handler这个类的ClassLoader对象来加载我们的代理对象
         * 第二个参数realSubject.getClass().getInterfaces(),我们这里为代理对象提供的接口是真实对象所实行的接口,表示我要代理的是该真实对象,这样我就能调用这组接口中的方法了
         * 第三个参数handler, 我们这里将这个代理对象关联到了上方的 InvocationHandler 这个对象上*/
//这里才是真正的代理者对象
        TheInterface theproxy= (TheInterface)Proxy.newProxyInstance(handler.getClass().getClassLoader(), delegator
                .getClass().getInterfaces(), handler);

        theproxy.printStr(" This is my first program");
//此处以Integer类作为示例
        printAllInform(Integer.class);
    }
    //因为有时候在 Android SDK 中有些类的方法不能直接调出来使用,所以需要用Java的反射机制去调用其函数。
    static public void printAllInform(Class clsShow) { 
        try { 
            // 取得所有方法 
            Method[] hideMethod = clsShow.getMethods(); 
            int i = 0; 
            for (i=0; i < hideMethod.length; i++) { 
            	System.out.println("method name: "+ hideMethod[i].getName()); 
            } 
            // 取得所有常量 
            Field[] allFields = clsShow.getFields(); 
            for (i = 0; i < allFields.length; i++) { 
            	System.out.println("Field name: "+ allFields[i].getName()); 
            } 
        } catch (SecurityException e) { 
            // throw new RuntimeException(e.getMessage()); 
            e.printStackTrace(); 
        } catch (IllegalArgumentException e) { 
            // throw new RuntimeException(e.getMessage()); 
            e.printStackTrace(); 
        } catch (Exception e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
    }
}
<h3>代码纯手打,可能有一点错误,敬请原谅。</h3>





微信公众号 日记文学
原文地址:https://www.cnblogs.com/xtubjut/p/5624737.html