1:动态代理$Proxy0.class文件源码
要想得到内存动态代码对象源码:需要在先通过下面语句来保存下来
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
2:动态代理基本示例
2.1 :定义一个Animal 接口
public interface Animal {
public void eat();
public void sleep();
}
2.2:实现 InvocationHandler
public class InterfaceInvocationHandler implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) {
if (method.getName().equals("sleep")) {
System.out.println("睡觉盖被子");
} else if (method.getName().equals("eat")) {
System.out.println("吃肉喝酒");
}
return null;
}
}
2.3 : 编写测试类
public class TestProxy {
public static void main(String[] args) {
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
Class<Animal> animalClass = null;
try {
animalClass = (Class<Animal>) Class.forName("io.reactivex.rxjava3.android.samples.proxy.Animal");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Class<?>[] classes = {animalClass};
Object instance = Proxy.newProxyInstance(Animal.class.getClassLoader(),classes,new InterfaceInvocationHandler());
Animal animal = (Animal) instance;
animal.eat();
animal.sleep();
}
}
3:动态代理对象源码
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.sun.proxy;
import io.reactivex.rxjava3.android.samples.proxy.Animal;
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 Animal {
private static Method m1;
private static Method m4;
private static Method m2;
private static Method m3;
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 eat() throws {
try {
super.h.invoke(this, m4, (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 sleep() throws {
try {
super.h.invoke(this, m3, (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"));
m4 = Class.forName("io.reactivex.rxjava3.android.samples.proxy.Animal").getMethod("eat");
m2 = Class.forName("java.lang.Object").getMethod("toString");
m3 = Class.forName("io.reactivex.rxjava3.android.samples.proxy.Animal").getMethod("sleep");
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
}
|