JDK 动态代理

Proxy

public class Proxy implements java.io.Serializable {

    protected InvocationHandler h;

    protected Proxy(InvocationHandler h) {
        doNewInstanceCheck();
        this.h = h;
    }


   public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces,InvocationHandler h)
    {

        /*
         * Look up or generate the designated proxy class.
         */
        Class<?> cl = getProxyClass0(loader, interfaces); // stack walk magic: do not refactor


        /*
         * Invoke its constructor with the designated invocation handler.
         */
            final Constructor<?> cons = cl.getConstructor(constructorParams);
            final InvocationHandler ih = h;
            return newInstance(cons, ih);
         
    }


    private static Object newInstance(Constructor<?> cons, InvocationHandler h) {
            return cons.newInstance(new Object[] {h} );
    }


Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) 

创建代理类class,代理类实现了指定的接口并继承了Proxy类。代理类中持有InvocationHandler的引用。通过代理类的构造函数反射创建代理类的实例,并InvocationHandler注入到代理类中。

代理类如下,

  1. public final class $Proxy11 extends Proxy implements UserService  
  2. {  
  3.   
  4.     // 构造方法,参数就是刚才传过来的MyInvocationHandler类的实例  
  5.     public $Proxy11(InvocationHandler invocationhandler)  
  6.     {  
  7.         super(invocationhandler);  
  8.     }  
  9.   
  10.     public final boolean equals(Object obj)  
  11.     {  
  12.         try  
  13.         {  
  14.             return ((Boolean)super.h.invoke(this, m1, new Object[] {  
  15.                 obj  
  16.             })).booleanValue();  
  17.         }  
  18.         catch(Error _ex) { }  
  19.         catch(Throwable throwable)  
  20.         {  
  21.             throw new UndeclaredThrowableException(throwable);  
  22.         }  
  23.     }  
  24.   
  25.     /** 
  26.      * 这个方法是关键部分 
  27.      */  
  28.     public final void add()  
  29.     {  
  30.         try  
  31.         {  
  32.             // 实际上就是调用MyInvocationHandler的public Object invoke(Object proxy, Method method, Object[] args)方法,第二个问题就解决了  
  33.             super.h.invoke(this, m3, null);  
  34.             return;  
  35.         }  
  36.         catch(Error _ex) { }  
  37.         catch(Throwable throwable)  
  38.         {  
  39.             throw new UndeclaredThrowableException(throwable);  
  40.         }  
  41.     }  
  42.   
  43.     public final int hashCode()  
  44.     {  
  45.         try  
  46.         {  
  47.             return ((Integer)super.h.invoke(this, m0, null)).intValue();  
  48.         }  
  49.         catch(Error _ex) { }  
  50.         catch(Throwable throwable)  
  51.         {  
  52.             throw new UndeclaredThrowableException(throwable);  
  53.         }  
  54.     }  
  55.   
  56.     public final String toString()  
  57.     {  
  58.         try  
  59.         {  
  60.             return (String)super.h.invoke(this, m2, null);  
  61.         }  
  62.         catch(Error _ex) { }  
  63.         catch(Throwable throwable)  
  64.         {  
  65.             throw new UndeclaredThrowableException(throwable);  
  66.         }  
  67.     }  
  68.   
  69.     private static Method m1;  
  70.     private static Method m3;  
  71.     private static Method m0;  
  72.     private static Method m2;  
  73.   
  74.     // 在静态代码块中获取了4个方法:Object中的equals方法、UserService中的add方法、Object中的hashCode方法、Object中toString方法  
  75.     static   
  76.     {  
  77.         try  
  78.         {  
  79.             m1 = Class.forName("java.lang.Object").getMethod("equals"new Class[] {  
  80.                 Class.forName("java.lang.Object")  
  81.             });  
  82.             m3 = Class.forName("dynamic.proxy.UserService").getMethod("add"new Class[0]);  
  83.             m0 = Class.forName("java.lang.Object").getMethod("hashCode"new Class[0]);  
  84.             m2 = Class.forName("java.lang.Object").getMethod("toString"new Class[0]);  
  85.         }  
  86.         catch(NoSuchMethodException nosuchmethodexception)  
  87.         {  
  88.             throw new NoSuchMethodError(nosuchmethodexception.getMessage());  
  89.         }  
  90.         catch(ClassNotFoundException classnotfoundexception)  
  91.         {  
  92.             throw new NoClassDefFoundError(classnotfoundexception.getMessage());  
  93.         }  
  94.     }  
  95. }  
代理类持有被代理类的方法对象。当客户端调用代理类的方法时,代理类将方法对象和方法参数传递给InvocationHandler.invoke(Object proxy, Method method, Object[] args)


InvocationHandler 可以通过Method 对象和方法参数反射调用被代理对象上的方法,可以在方法调用前后插入想切入的逻辑。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值