0
点赞
收藏
分享

微信扫一扫

动态代理生成的类DynamicProxy


资料

JDK动态代理生成的class文件保存到本地失败问题(sun.misc.ProxyGenerator.saveGeneratedFiles)将JDK动态代理生成的类保存为 .class文件System.setProperty(“sun.misc.ProxyGenerator.saveGeneratedFiles“, “true“)无效
javascript:void(0)
从代理模式再出发!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_System

动态代理生成的类DynamicProxy_System_02

package com.flannery;

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

public class main {
//https://www.bilibili.com/video/BV1cz41187Dk?spm_id_from=333.337.search-card.all.click


    // 动态代理的作用
    // 1. 可以拦截方法,动态处理
    // 2。 解耦合(返回相同类型,也就是有一个具体的实现了类:比如Retrofit中的Call)

    public static void main(String[] args) {
        System.getProperties().setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        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");

        ServiceImpl serviceImpl = new ServiceImpl();
        Service service = (Service) Proxy.newProxyInstance(
                serviceImpl.getClass().getClassLoader(),
                new Class<?>[]{Service.class},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        if(method.getName() == "isFine") {
                            return true;
                        }
                        return method.invoke(serviceImpl, args);
                    }
                }
        );
        if (service != null) {
            System.out.println(""+service.isFine());
            service.hello();
        }
    }

}

interface Service {
    boolean isFine();

    void hello();
}

class ServiceImpl implements Service {

    @Override
    public boolean isFine() {
        return false;
    }

    @Override
    public void hello() {
        System.out.println("hello");
    }
}


举报

相关推荐

0 条评论