我正在使用JDI对方法中的变量状态进行重新编码。根据本教程,我没有找到如何获得objectReference值,如List、Map或我的自定义类。它只会让PrimtiveValue。
StackFrame stackFrame = ((BreakpointEvent) event).thread().frame(0);
Map<LocalVariable, Value> visibleVariables = (Map<LocalVariable, Value>) stackFrame
.getValues(stackFrame.visibleVariables());
for (Map.Entry<LocalVariable, Value> entry : visibleVariables.entrySet()) {
System.out.println("console->>" + entry.getKey().name() + " = " + entry.getValue());
}
}如果LocalVariable是PrimtiveValue类型,如int a = 10;,那么它将打印
console->> a = 10如果LocalVariable是ObjectReference类型,如Map data = new HashMap();data.pull("a",10),那么它将打印
console->> data = instance of java.util.HashMap(id=101)但我想得到如下的结果
console->> data = {a:10} // as long as get the data of reference value谢谢!
发布于 2019-11-23 22:29:50
没有ObjectReference的“值”。它本身就是Value的一个实例。
您可能需要的是获取这个ObjectReference引用的对象的字符串表示形式。在这种情况下,您需要对该对象调用toString()方法。
调用ObjectReference.invokeMethod,为toString()传递一个Method。因此,您将得到一个StringReference实例,然后调用value()以获得所需的字符串表示形式。
for (Map.Entry<LocalVariable, Value> entry : visibleVariables.entrySet()) {
String name = entry.getKey().name();
Value value = entry.getValue();
if (value instanceof ObjectReference) {
ObjectReference ref = (ObjectReference) value;
Method toString = ref.referenceType()
.methodsByName("toString", "()Ljava/lang/String;").get(0);
try {
value = ref.invokeMethod(thread, toString, Collections.emptyList(), 0);
} catch (Exception e) {
// Handle error
}
}
System.out.println(name + " : " + value);
}https://stackoverflow.com/questions/59010599
复制相似问题