阿尔萨斯查看jdk动态代理生成类

文章展示了如何使用JDK动态代理创建代理对象,通过一个`Foo`和`Bar`接口的实现类`FooImpl`,并利用`Proxy.newProxyInstance`生成代理对象。代理对象在调用方法时会执行`InvocationHandler`的`invoke`方法,实现了对原对象方法的增强。同时提到了阿尔萨斯工具用于查看和分析代理类的字节码,以及通过系统属性保存生成的代理类文件到磁盘的方法。
摘要由CSDN通过智能技术生成

1.先准备一份jdk代码实现类

public class JdkProxyTest {
    interface Foo {
        void eat(String food, int num);

        void drink(String water);
    }

    interface Bar {
        int say(String say);
    }


    static class FooImpl implements Foo, Bar {

        @Override
        public void eat(String food, int num) {
            System.out.println("call FoolImpl eat() " + food + " " + num);
        }

        @Override
        public void drink(String water) {
            System.out.println("call FoodImpl drink() " + water);
        }

        @Override
        public int say(String say) {
            System.out.println("call FoodImpl say() " + say);
            return 3;
        }
    }

    public static void main(String[] args) {
        //目标对象
        FooImpl f = new FooImpl();
        //代理对象
        Bar foo = (Bar) Proxy.newProxyInstance(f.getClass().getClassLoader(), new Class[]{Foo.class, Bar.class}, (proxy, method, args1) -> {
            System.out.println("proxy.getClass().getName() = " + proxy.getClass().getName());
            System.out.println("我是代理类的invoke方法之前");
            Object result = method.invoke(f, args1);
            System.out.println("我是代理类的invoke方法之后");
            return result;
        });
        //foo.eat("瓜子", 250);
       // foo.drink("可乐");
        int ok = foo.say("ok");
        System.out.println("excute result "+ok);
        System.out.println(foo.getClass());

        while(true){}
    }
}

执行结果:
注意上图的蓝色部分

2.下载一份阿尔萨斯代码

阿尔萨斯地址链接:https://gitee.com/arthas/arthas
cmd进入jar目录: 启动jar包 java -jar arthas-boot.jar
执行结果
在这里插入图片描述
这里我们的类代号为5 我们按5然后按enter
在这里插入图片描述
然后从我们idea中打印的 System.out.println(foo.getClass());的结果中找到代理类的名称
在这里插入图片描述
在阿尔萨斯控制台执行 上面的class名称 jad develop.$Proxy0
在这里插入图片描述
执行结果

ClassLoader:
+-sun.misc.Launcher$AppClassLoader@18b4aac2
  +-sun.misc.Launcher$ExtClassLoader@294e68b4

Location:

/*
 * Decompiled with CFR.
 */
package develop;

import develop.JdkProxyTest;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

final class $Proxy0
extends Proxy
implements JdkProxyTest.Foo,
JdkProxyTest.Bar {
    private static Method m1;
    private static Method m2;
    private static Method m4;
    private static Method m5;
    private static Method m3;
    private static Method m0;

    public $Proxy0(InvocationHandler invocationHandler) {
        super(invocationHandler);
    }

    static {
        try {
            m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
            m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
            m4 = Class.forName("develop.JdkProxyTest$Foo").getMethod("drink", Class.forName("java.lang.String"));
            m5 = Class.forName("develop.JdkProxyTest$Bar").getMethod("say", Class.forName("java.lang.String"));
            m3 = Class.forName("develop.JdkProxyTest$Foo").getMethod("eat", Class.forName("java.lang.String"), Integer.TYPE);
            m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
            return;
        }
        catch (NoSuchMethodException noSuchMethodException) {
            throw new NoSuchMethodError(noSuchMethodException.getMessage());
        }
        catch (ClassNotFoundException classNotFoundException) {
            throw new NoClassDefFoundError(classNotFoundException.getMessage());
        }
    }

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

    public final String toString() {
        try {
            return (String)this.h.invoke(this, m2, null);
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final int hashCode() {
        try {
            return (Integer)this.h.invoke(this, m0, null);
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final int say(String string) {
        try {
            return (Integer)this.h.invoke(this, m5, new Object[]{string});
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final void eat(String string, int n) {
        try {
            this.h.invoke(this, m3, new Object[]{string, n});
            return;
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final void drink(String string) {
        try {
            this.h.invoke(this, m4, new Object[]{string});
            return;
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }
}

上述代码就是动态生成的代理对象代码

如果不想用阿尔萨斯:可以直接设置系统属性 在main方法中添加添加如下代码,最后会在项目根路径下保存生成的动态代理类


//JDK1.8及以前的版本
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");

//JDK1.8以后的版本
System.getProperties().put("jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true");


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值