JDI: How to get the ObjectReference value?
Asked Answered
O

1

2

I am using the JDI to recode the variable status in the method. According to the tutorial, I am not found how to get objectReference value, like List, Map or my custom class. It just can get 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());
                        }
}

if LocalVariable is PrimtiveValue type, like int a = 10;, then it will print

console->> a = 10

if LocalVariable is ObjectReference type, like Map data = new HashMap();data.pull("a",10), then it will print

console->> data = instance of java.util.HashMap(id=101)

But I want to get result like below

console->> data = {a:10} // as long as get the data of reference value

Thanks!

Oligarch answered 23/11, 2019 at 17:58 Comment(1)
FYI: the visibleVariables() business will give you nothing unless the code was compiled with javac -g, which most production code won't be.Eldaelden
S
3

There is no 'value' of an ObjectReference. It is itself an instance of Value.

What you probably want is to get a string representation of an object referenced by this ObjectReference. In this case you need to call toString() method on that object.

Call ObjectReference.invokeMethod passing a Method for toString(). As a result, you'll get a StringReference instance, on which you then call value() to obtain the desired string representation.

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);
}

Sanatorium answered 23/11, 2019 at 22:29 Comment(1)
"on which you then call value()". Well, it (somehow) works to call toString() directly as you're doing on the last line. However, that will add some quotes, e.g. when decapsulating a Boolean I get "true", including the quotes printed.Eldaelden

© 2022 - 2024 — McMap. All rights reserved.