JAVA-获取 JDK 动态代理生成的 Class 文件

时间:2021-11-29 09:46:31

可指定路径

import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy; interface Dao {
void save();
} class DaoImpl implements Dao {
@Override
public void save() {
System.out.println("save...");
}
} public class DynamicProxyTest { /**
* 保存 JDK 动态代理生产的类
* @param filePath 保存路径,默认在项目路径下生成 $Proxy0.class 文件
*/
private static void saveProxyFile(String... filePath) {
if (filePath.length == 0) {
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
} else {
FileOutputStream out = null;
try {
byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy0", DaoImpl.class.getInterfaces());
out = new FileOutputStream(filePath[0] + "$Proxy0.class");
out.write(classFile);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public static void main(String[] args) throws Exception {
saveProxyFile(); Object target = new DaoImpl(); /**
* loader:业务对象的类加载器
* interfaces:业务对象实现的所有接口
* public static Class<?> getProxyClass(ClassLoader loader, Class<?>... interfaces)
*/
Class<?> proxyClass = Proxy.getProxyClass(DaoImpl.class.getClassLoader(), DaoImpl.class.getInterfaces());
InvocationHandler handler = new InvocationHandler() {
/**
* @param proxy 代理对象
* @param method 代理的方法对象
* @param args 方法调用时参数
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = null;
if (method.getName().equals("save")) {
System.out.println("before...");
result = method.invoke(target, args);
System.out.println("after...");
}
return result;
}
};
Dao userDao = (Dao) proxyClass.getConstructor(InvocationHandler.class).newInstance(handler);
userDao.save();
}
}

生成的代理类,方法调用会经过 InvocationHandler 对象

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 Dao {
private static Method m1;
private static Method m3;
private static Method m2;
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 save() 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 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("javabase.dynamic.Dao").getMethod("save");
m2 = Class.forName("java.lang.Object").getMethod("toString");
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
}

https://rejoy.iteye.com/blog/1627405

https://blog.csdn.net/lh513828570/article/details/74078773

https://blog.csdn.net/bestkilly/article/details/82141802