Java动态代理-代理类的生成

1,生成过程。

1)全限定名:com.sun.proxy.$Proxy0~n


Java动态代理-代理类的生成_第1张图片
image.png

2)生成的类都继承了Proxy类,并且实现了interfaces接口。

2,Proxy类的具体内容

1)类加载后,并初始化时。通过反射获取equals、toString、hashCode的Method对象。以及MyInterface的doA Method对象。
2)代理类本身实现了MyInterface接口,重写了Object的几个方法。
3)Proxy类拥有InvocationHandler对象,所有对proxy 方法的调用,都会转为对InvocationHandler的invoke方法的调用。super.h.invoke(this, m3, (Object[])null);
4)InvocationHandler拥有target真实对象,会转为对target真实对象method方法的调用。

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return method.invoke(realObj, args);
            }
import com.hzq.MyInterface;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

public final class Proxy01 extends Proxy implements MyInterface {
    private static Method m1;
    private static Method m3;
    private static Method m2;
    private static Method m0;

    public Proxy01(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 void doA() throws  {
        try {
            super.h.invoke(this, m3, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    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 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"));
            m3 = Class.forName("com.hzq.MyInterface").getMethod("doA");
            m2 = Class.forName("java.lang.Object").getMethod("toString");
            m0 = Class.forName("java.lang.Object").getMethod("hashCode");
        } catch (NoSuchMethodException var2) {
            throw new NoSuchMethodError(var2.getMessage());
        } catch (ClassNotFoundException var3) {
            throw new NoClassDefFoundError(var3.getMessage());
        }
    }
}

3)模拟生成代理类ProxyGenerator.generateProxyClass

public static byte [] saveGenerateProxyClass(String name, Class [] interfaces) {
        byte[] classFile = ProxyGenerator.generateProxyClass(name, interfaces);
        FileOutputStream out = null;
        try {
            String filePath = "/Users/hzq/gitsrc/my-quick-start" + "/" + name + ".class";
            out = new FileOutputStream(filePath);
            out.write(classFile);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(out != null) out.close();
            } catch (IOException e) {
            }
        }
        return classFile;
    }

你可能感兴趣的:(Java动态代理-代理类的生成)