【发布时间】:2022-01-16 15:17:35
【问题描述】:
我通过 JDI 得到com.sun.jdi.ObjectReference 是类型java.util.HashMap,然后我得到了字段值“entrySet”,为什么它总是为空?
如何使用 java debug api 遍历 HashMap keyValues?
public static void displayVariables(LocatableEvent event) throws IncompatibleThreadStateException, AbsentInformationException {
StackFrame stackFrame = event.thread().frame(0);
for (LocalVariable visibleVariable : stackFrame.visibleVariables()) {
Value value = stackFrame.getValue(visibleVariable);
if(value instanceof ObjectReference){
ObjectReference objectReference = (ObjectReference) value;//is type java.util.HashMap
Field entrySet = objectReference.referenceType().fieldByName("entrySet");// is java.util.HashMap.entrySet
Value entrySetValue = objectReference.getValue(entrySet);// is null
System.out.println(entrySet);
}
}
}
【问题讨论】: