动态代理生成的类DynamicProxy

资料

JDK动态代理生成的class文件保存到本地失败问题(sun.misc.ProxyGenerator.saveGeneratedFiles)
将JDK动态代理生成的类保存为 .class文件System.setProperty(“sun.misc.ProxyGenerator.saveGeneratedFiles“, “true“)无效
https://www.cnblogs.com/qinggege/p/5288182.html
从代理模式再出发!Proxy.newProxyInstance的秘密
JDK动态代理之 字节码生成流程
JAVA动态代理
Java 动态代理作用是什么?

代码

package com.proxy;

import java.lang.reflect.*;

public class ProxyTest {

    public interface HelloInterface {
        void sayHello();
    }

    public static void main(String[] args)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        System.out.println("Hello World!");
        System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        //System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        System.getProperties().put("jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true");
        System.setProperty("jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true");


        //jdk.proxy.ProxyGenerator.saveGeneratedFiles

        // 得到代理
        Class<?> hi = Proxy.getProxyClass(HelloInterface.class.getClassLoader(), HelloInterface.class);
        // 得到Proxy0
        Constructor constructor = hi.getConstructor(InvocationHandler.class);
        // 反射创建代理实例
        HelloInterface hiImpl = (HelloInterface) constructor.newInstance(new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                return null;
            }
        });
        hiImpl.sayHello();
    }

}

自动生成的代码

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.sun.proxy;

import com.proxy.ProxyTest.HelloInterface;
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 HelloInterface {
    private static Method m1;
    private static Method m3;
    private static Method m2;
    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 void sayHello() 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.proxy.ProxyTest$HelloInterface").getMethod("sayHello");
            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());
        }
    }
}

动态代理生成的类DynamicProxy

动态代理生成的类DynamicProxy

上一篇:LVS(DR模式)的realserver配置(windows)


下一篇:PHP抛出简单说明