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

编程入门 行业动态 更新时间:2024-10-17 17:16:36

<a href=https://www.elefans.com/category/jswz/34/1755944.html style=阿尔萨斯查看jdk动态代理生成类"/>

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

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 {@Overridepublic void eat(String food, int num) {System.out.println("call FoolImpl eat() " + food + " " + num);}@Overridepublic void drink(String water) {System.out.println("call FoodImpl drink() " + water);}@Overridepublic 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.下载一份阿尔萨斯代码

阿尔萨斯地址链接:
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@294e68b4Location:/** 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");

更多推荐

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

本文发布于:2024-02-07 09:50:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1755782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阿尔萨斯   动态   jdk

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!