JDK动态代理

背景

静态代理的弊端是对于被代理类的方法要逐个实现,比较繁琐。此外在被代理对象方法变更时也要对应的变更,维护也不方便。动态代理可以方便的解决该问题。

实现步骤

  1. 声明接口: 为了方便使用JDK的UpdateCheckListener接口
  2. 接口实现类
import com.oracle.deploy.update.UpdateCheckListener;

/**
 * 接口实现类
 * @author lixiaowen
 * @date 2019/2/11
 */
public class UpdateCheckListenerImpl implements UpdateCheckListener {

    @Override
    public void updateCheckStateChanged(int i) {
        System.out.println("original i = " +  i);
    }
}
  1. JDK代理类实现
import com.oracle.deploy.update.UpdateCheckListener;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * jdk 动态代理实现{@link UpdateCheckListener} 
 * @author lixiaowen
 * @date 2019/2/11
 */
public class UpdateCheckListenerJdkProxy implements InvocationHandler {

    //被代理的对象
    private UpdateCheckListener source;

    public UpdateCheckListenerJdkProxy(UpdateCheckListener source) {
        this.source = source;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("JDK实现代理..." + method);
        return method.invoke(source, args);
    }
}
  1. 客户端测试
    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        //代理类写入文件
        System.getProperties().setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles","true");
        UpdateCheckListenerJdkProxy jdkProxy = new UpdateCheckListenerJdkProxy(UpdateCheckListenerImpl.class.newInstance());
        UpdateCheckListener proxy = (UpdateCheckListener) Proxy.newProxyInstance(UpdateCheckListener.class.getClassLoader(),
                new Class[]{UpdateCheckListener.class},
                jdkProxy);
        proxy.updateCheckStateChanged(666);
    }

原理

生成的UpdateCheckListenerImpl代理类class文件

package com.sun.proxy;

import com.oracle.deploy.update.UpdateCheckListener;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

public final class $Proxy0 extends Proxy implements UpdateCheckListener {
    private static Method m1;
    private static Method m2;
    private static Method m3;
    private static Method m0;

    public $Proxy0(InvocationHandler var1) throws  {
        super(var1);
    }

    public final boolean equals(Object var1) throws  {
        try {
            return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
        } catch (RuntimeException | Error var3) {
            throw var3;
        } catch (Throwable var4) {
            throw new UndeclaredThrowableException(var4);
        }
    }

    public final String toString() throws  {
        try {
            return (String)super.h.invoke(this, m2, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final void updateCheckStateChanged(int var1) throws  {
        try {
            super.h.invoke(this, m3, new Object[]{var1});
        } catch (RuntimeException | Error var3) {
            throw var3;
        } catch (Throwable var4) {
            throw new UndeclaredThrowableException(var4);
        }
    }

    public final int hashCode() throws  {
        try {
            return (Integer)super.h.invoke(this, m0, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    static {
        try {
            m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
            m2 = Class.forName("java.lang.Object").getMethod("toString");
            m3 = Class.forName("com.oracle.deploy.update.UpdateCheckListener").getMethod("updateCheckStateChanged", Integer.TYPE);
            m0 = Class.forName("java.lang.Object").getMethod("hashCode");
        } catch (NoSuchMethodException var2) {
            throw new NoSuchMethodError(var2.getMessage());
        } catch (ClassNotFoundException var3) {
            throw new NoClassDefFoundError(var3.getMessage());
        }
    }
}

从代码中可以看出,JDK生成了新的类$Proxy0,,该类实现了UpdateCheckListener接口, 并且在构造器的时候传递InvocationHandler对象,并赋值给super.h。
updateCheckStateChanged方法调用的时候,运行期间通过super.h调用invoker方法实现的。
super.h是构造器的var1传递过来的,该对象是通过反射生成实例的时候传递的,也就是jdkProxy对象。

  • $Proxy0的class文件生成过程

Proxy.newProxyInstance() -> Prxoy.getProxyClass0() -> Proxy.proxyClassCache -> Proxy.ProxyClassFactory.apply() ->ProxyGenerator.generateProxyClass()

你可能感兴趣的:(JDK动态代理)