其他分享
首页 > 其他分享> > 动态代理

动态代理

作者:互联网

接口类

public interface People {
    void eat(String name);

    void run();
}

实现类:

public class Man  implements People{
    @Override
    public void eat(String name) {
        System.out.println("eat");
    }

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

测试类:

public class Test {
    void test(){
        People o = (People) Proxy.newProxyInstance(People.class.getClassLoader(), new Class[]{People.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                People people = new Man();
                method.invoke(people,args);//方法的调用方式
                System.out.println("11111");
                return null;
            }
        });
        o.eat("aa");
    }
    public static void buildProxy() throws IOException {
        byte[] bytes = ProxyGenerator.generateProxyClass("People$proxy",new Class[]{People.class});
        String fileName = System.getProperty("user.dir")+"\\target\\Man$proxy.class";

        File file = new File(fileName);

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(bytes);
        fileOutputStream.flush();
        fileOutputStream.close();
    }

    public static void main(String[] args) throws IOException {

        new Test().test();
        //buildProxy();
    }
}

 

 

 以上就是动态代理的过程。使用 buildProxy()生成代理类,看代理类是如何调用的。

========================================================================================================================

 

public final class People$proxy extends Proxy implements People {
    private static Method m1;
    private static Method m3;
    private static Method m2;
    private static Method m4;
    private static Method m0;

    public People$proxy(InvocationHandler var1) throws  {
        super(var1);//将实现的InvocationHandler传入父类中的InvocationHandler中赋值
    }

    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 run() 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 void eat(String var1) throws  {
        try {
            super.h.invoke(this, m4, new Object[]{var1});//调用自己的invoke方法,但是Method是不一样的。从这个可以看出,自己的invoke是一定会执行的,但是通过Method执行不同的方法
        } 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"));
            m3 = Class.forName("testtt.People").getMethod("run");
            m2 = Class.forName("java.lang.Object").getMethod("toString");
            m4 = Class.forName("testtt.People").getMethod("eat", Class.forName("java.lang.String"));//生成Method方法,m.invoke(对象,参数)
            m0 = Class.forName("java.lang.Object").getMethod("hashCode");
        } catch (NoSuchMethodException var2) {
            throw new NoSuchMethodError(var2.getMessage());
        } catch (ClassNotFoundException var3) {
            throw new NoClassDefFoundError(var3.getMessage());
        }
    }
}

 

标签:People,Object,代理,throw,catch,new,动态,public
来源: https://www.cnblogs.com/wxynb/p/16427932.html