保存Java代理框架生成的类文件。

Java的代理框架经常接触,但实际中我们看不到运行时生成的代理类,使用的代理框架是默认不会保存,如果想要查看,需要显式打开开关,可以通过以下2段代码做到。

1、Java动态代理。让下面代码在代理类执行前执行,然后刷新项目,在其根目录下可以看到形如"$Proxy0.class" 文件,再使用反编译工具解析即可。

/**
* 设置保存Java动态代理生成的类文件。
*
* @throws Exception
*/
public static void saveGeneratedJdkProxyFiles() throws Exception {
     Field field = System.class.getDeclaredField("props");
    field.setAccessible(true);
    Properties props = (Properties) field.get(null);
    props.put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
}

 

2、Cglib代理。让下面代码在代理类执行前执行,然后刷新项目,在指定目录dir下可以看到形如 "类名$$EnhancerByCGLIB$$数字.class" 文件,再使用反编译工具解析即可。

/**
* 设置保存Cglib代理生成的类文件。
*
* @throws Exception
*/
public static void saveGeneratedCGlibProxyFiles() throws Exception {
    Field field = System.class.getDeclaredField("props");
    field.setAccessible(true);
     Properties props = (Properties) field.get(null);
    System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, dir);//dir为保存文件路径
    props.put("net.sf.cglib.core.DebuggingClassWriter.traceEnabled", "true");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值