我需要转储 Java 对象的所有属性。我发现了一些执行此操作的函数,但没有一个处理自引用,而且我发现所有函数都陷入了无限递归。
//我将在 Android 上运行此功能,但这并不重要。
最佳答案
如果这只是为了调试,或者如果您想要某种形式的基本序列化,请查看 XStream .这是他们网站上的一个例子,特别是关于 self 引用......
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));
输出是...
<list>
<cd>
<id>maria rita</id>
</cd>
<cd>
<id>basement_jaxx_singles</id>
</cd>
<cd reference="../cd[2]"/>
<list reference=".."/>
</list>
关于Java 转储一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4904497/