嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 3 元微信扫码支付:3 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
Java变量值变化监控
维护老系统时,不想修改任何老代码的情况下,想监控一个变量的值变化事件,可以使用该代码,主要原理是使用了java的jdi接口
获取全部可运行的代码请下载附件,使用说明请看压缩包中的readme.md
//要调试的目标程序的监听地址
public static final String HOST = "127.0.0.1";
//要调试的目标程序的监听端口
public static final String PORT = "20020";
//要调试的目标程序的类全名
public static final String CLS_NAME = "Main";
//类中要监控的变量字段名
public static final String CLS_FIELD = "value";
//监测到 watchCount 次变量修改事件后退出程序
public static int watchCount = 3;
......
Event event = eventIterator.next();
// 发生了变量值被修改的事件
if (event instanceof ModificationWatchpointEvent) {
ModificationWatchpointEvent e = (ModificationWatchpointEvent) event;
// 获得事件线程引用
ThreadReference thread = e.thread();
// 打印被监控变量当前值
System.out.println(e.field().toString() "发生变化:" e.valueToBe().toString());
// 打印线程调用堆栈
System.out.println("stack trace in thread \"" thread.name() "\"");
for (int i = thread.frameCount() - 1 ; i >= 0 ; i --) {
Location location = thread.frame(i).location();
System.out.println("\tat " location.method() " -> (" location.sourceName() ":" location.lineNumber() ")");
}
// 工作结束后,切记一定要让线程恢复运行!!!
thread.resume();
eventSet.resume();
watchCount --;
if(watchCount <= 0){
break eventLoop;
}
}
.
├── Debugger.java
├── Java变量值变化监控.zip
├── Main.java
└── readme.md
0 directories, 4 files