Agent agentmain-运行期恢复类修改,热替换

本文介绍了一种在Java中恢复类原始状态的方法,通过重新从源码文件读取并加载类信息到JVM,实现了运行时类修改的擦除与热替换功能,特别适用于取消先前添加的类监控。

前文讲过运行期修改类,但是有的时候想去除以前的修改,比如曾经添加了类监控,但是现在不想监控了,希望恢复成原生代码。

使用以下代码,重新从源码文件中读取类信息,然后加载到jvm中。

这既可以擦除原来的修改,也可以实现热替换功能。 这里只贴出了关键代码,其它部分和前文一致。

public class RestoreTransformer implements ClassFileTransformer {
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
        try {
            if (className.contains(UdAgent.paths));
                return readStream(ClassLoader.getSystemResourceAsStream(className.replace('.', '/') + ".class"), true);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    private static byte[] readStream(InputStream inputStream, boolean close) throws IOException {
        if(inputStream == null) {
            throw new IOException("Class not found");
        } else {
            try {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                byte[] data = new byte[4096];

                int bytesRead;
                while((bytesRead = inputStream.read(data, 0, data.length)) != -1) {
                    outputStream.write(data, 0, bytesRead);
                }

                outputStream.flush();
                byte[] var5 = outputStream.toByteArray();
                return var5;
            } finally {
                if(close) {
                    inputStream.close();
                }

            }
        }
    }
}
VMware_View问题汇总指南 1、启连接服务器提示无法验证服务器 ............................................................................. 3 2、部署linked clone虚拟桌面失败,提示如下错误提示:“View Composer agent initialization state error (18): Failed to join the domain” ................................................ 4 3、PCoIP连接时不能跨两个显示器显示 ........................................................................... 5 4、确认桌面资源池Desktop Pool存在 .............................................................................. 7 5、访问View Security服务器失败:Give final block not properly padded ...................... 8 6、为View Composer创建一个QuickPrep账户的步骤 ................................................... 8 7、登录到虚拟桌面时,提示“Login in as current user”选项失败 .................................... 9 8、View4.5安装View Agent报错:28051 Shared Access错误 ..................................... 10 9、删除VIEW的孤立的persistent diskVMware .............................................................. 11 10、ThinApp打包和运维最佳实践 ................................................................................... 11 11、手动从VMware View Manager删除linked clones或陈旧的virtual desktop ........ 12 12、尝试在VMware View 5.0的Session Manager对话框里尝试删除Session提示:clock skewed .................................................................................................................. 13 13、VMware View 5.0的限制和Maximum说明 ............................................................. 14 14、远程登录桌面后,黑屏后登出的问题 ..................................................................... 14 15、PCoIP连接View 5.0虚拟桌面超时断开导致无法再次登录的问题 ....................... 15 16、关于View 5.x中Adobe Flash Throttling的参数详解 ............................................... 16 17、解决:Unable to retrieve information from Active Directory for domain null .......... 16 18、2008安装View Connection Server失败:There was an error creating a MS Direct17 19、安装View Connection Server replica失败:Error 28018 ......................................... 18 20、激活了自动登陆 View Client 导致虚拟桌面连接失败的解决方案 ....................... 18 21、删除掉 VMware View Composer(Link
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值