I am trying to prettyprint in JSON format objects I retrieve from the Java Debug Interface. These objects are the arguments of the method where my breakpoint hit.
My code inside the BreakpointEvent handler is as follows :
// event is the current BreakpointEvent
StackFrame sf = event.thread().frame(0);
for (Value v : sf.getArgumentValues()) {
try {
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(v));
} catch (Exception e) {
e.printStackTrace();
}
}
However, the only output that I'm getting out of that is a bunch of {"collected":false}, which is not helpful at all...
I can do the inspection manually, by first checking the precise interface implemented by each v (interfaces like ArrayReference, StringReference, ...), then using the coresponding methods (v.getValues() for an object implementing ArrayReference for instance), and doing the same recursively for all values I get.
But manual inspection is more prone to error, and much more heavy in term of code and maintenance. I am confident that Jackson could do the trick here but I'm not getting it to work...
The way I ended up doing is as follows :
I created a wrapper class MyValue, with one public attribute
value, and a gettergetValue(), which I customize to return either:I also had to take care of infinite reference (cyclic graphs, for example an object A having a ref to object B, and object B having a ref to object A), so the MyValue class also contains an ancestors list, to avoid loops. Hope this will be helpful to someone in the future.