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");