代码改变世界

java 获取动态代理生成的 Class 文件

2020-05-07 14:17  Tony、  阅读(756)  评论(0编辑  收藏  举报

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

  1.1  System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");此方式只能在爱main函数中使用

  1.2

    
       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();
                }
            }

二 获取 Cglib 动态代理生成的 Class 文件

System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "e:\\class");