首页 > 代码库 > 基于Instrumentation的JAVA代码热替换
基于Instrumentation的JAVA代码热替换
理类用来获取 Instrumentation 实例
加agent参数 运行launch
package com.codeconch.util; import java.lang.instrument.Instrumentation; public class Monitor { private static Instrumentation instrumentation; public static void premain(String args, Instrumentation inst) { instrumentation = inst; } public static Instrumentation getInstrumentation(){ return instrumentation; } }
将这个类打成AgentJAR包monitor.jar
配置MANIFEST.MF
Manifest-Version: 1.0
Premain-Class: com.codeconch.util.Monitor
Can-Redefine-Classes: true
/** * * @author 赵聪慧 * 2014-3-31下午5:21:12 */ public class Test { <span style="white-space:pre"> </span>static int i=5; <span style="white-space:pre"> </span>private int a=2; <span style="white-space:pre"> </span>public int geta(){ <span style="white-space:pre"> </span>return a+i; <span style="white-space:pre"> </span>} }
将Test类中的代码改为
/** * * @author 赵聪慧 * 2014-3-31下午5:21:12 */ public class Test { <span style="white-space:pre"> </span>static int i=5; <span style="white-space:pre"> </span>private int a=2; <span style="white-space:pre"> </span>public int geta(){ <span style="white-space:pre"> </span>return a*i; <span style="white-space:pre"> </span>} }
加agent参数 运行launch
-javaagent:D:\monitor.jar
public static void main(String args[]) throws InvalidProtocolBufferException{ Test test=new Test(); while(true){ try { System.out.println(test.geta()); byte[] bytesFromFile = FileUtil.getBytesFromFile("D:/Test.class"); Monitor.getInstrumentation().redefineClasses(new ClassDefinition(Test.class,bytesFromFile)); Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnmodifiableClassException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
输出
10 7 7 7 ...
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。