Java转储对象 [英] Java dump an object

查看:80
本文介绍了Java转储对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要转储Java对象的所有属性。我发现有几个函数可以执行此操作,但是它们都不处理自我引用,并且我发现所有这些函数都螺旋成无限递归。

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.

//我将运行

推荐答案

如果这仅是为了调试或您想要某种形式的基本序列化请看 XStream 。这是他们网站上的一个示例,该示例特别谈论自我参考...

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

输出为...

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

这篇关于Java转储对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆