1

I need to dump all the attributes of a Java object. I have found a few functions that do this but none of them 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.

James Andino
  • 22,903
  • 15
  • 52
  • 75

1 Answers1

1

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>
Andrew White
  • 51,542
  • 18
  • 111
  • 135
  • I tried this and will use xstream but it does not support private fields :/ – James Andino Feb 05 '11 at 03:32
  • That can't be right, check this page http://xstream.codehaus.org/tutorial.html There is even the statement "XStream doesn't care about the visibility of the fields." – Andrew White Feb 05 '11 at 03:44