【问题标题】:JDI:How to iterate over HashMapJDI:如何遍历 HashMap
【发布时间】: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);

            }
        }
    }

【问题讨论】:

    标签: java jdi


    【解决方案1】:
    for (T key : hashMap.keySet()) {
        V value = hashMap.get(key);
        // rest of your code here
    } // Can you get to where you need to go from here?
    

    假设 hashMap 的类型是 HashMap

    【讨论】:

      【解决方案2】:
      mapConfig.forEach((k, v) -> {
      
      });
      

      【讨论】:

        猜你喜欢
        • 2011-10-23
        • 2012-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-07
        • 1970-01-01
        • 1970-01-01
        • 2010-12-22
        相关资源
        最近更新 更多