开发者

Java dump an object

开发者 https://www.devze.com 2023-02-08 11:24 出处:网络
I need to dump all the attributes of a Java object. I have found a few functions that do this but none of the开发者_StackOverflow中文版m handle self references and all of the functions I have found sp

I need to dump all the attributes of a Java object. I have found a few functions that do this but none of the开发者_StackOverflow中文版m handle self references and all of the functions I have found spiral into infinite recursion.

//I will be running this function on Android but that shouldn't really matter.


If this is just for debugging or if you want some form of basic serialization take a peek at XStream . Here is an example from their site talking about self references in particular...

Cd bj = new Cd("basement_jaxx_singles");

List order = new ArrayList();
// adds the same cd twice (two references to the same object)
order.add(bj);
order.add(bj);

// adds itself (cycle)
order.add(order);

XStream xstream = new XStream();
xstream.alias("cd", Cd.class);
System.out.println(xstream.toXML(order));

And the output is...

<list>
  <cd>
    <id>maria rita</id>
  </cd>
  <cd>
    <id>basement_jaxx_singles</id>
  </cd>
  <cd reference="../cd[2]"/>
  <list reference=".."/>
</list>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号