- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我需要能够遍历我的整个对象图并记录所有成员字段的所有内容。
例如:对象 A 具有对象 B 的集合,对象 B 具有对象 C 的集合,并且 A、B、C 上有附加字段,等等。
Apache Commons ToStringBuilder是不够的,因为它不会遍历对象图或集合的输出内容。
有没有人知道另一个库可以做到这一点或有一个代码片段可以做到这一点?
最佳答案
您可以使用 org.apache.commons.lang.builder.ReflectionToStringBuilder
遍历整个树。诀窍是在 ToStringStyle
你需要遍历值。 ToStringStyle
将处理已处理的值,并且不允许递归。我们开始:
System.out.println(ReflectionToStringBuilder.toString(schema, new RecursiveToStringStyle(5)));
private static class RecursiveToStringStyle extends ToStringStyle {
private static final int INFINITE_DEPTH = -1;
/**
* Setting {@link #maxDepth} to 0 will have the same effect as using original {@link #ToStringStyle}: it will
* print all 1st level values without traversing into them. Setting to 1 will traverse up to 2nd level and so
* on.
*/
private int maxDepth;
private int depth;
public RecursiveToStringStyle() {
this(INFINITE_DEPTH);
}
public RecursiveToStringStyle(int maxDepth) {
setUseShortClassName(true);
setUseIdentityHashCode(false);
this.maxDepth = maxDepth;
}
@Override
protected void appendDetail(StringBuffer buffer, String fieldName, Object value) {
if (value.getClass().getName().startsWith("java.lang.")
|| (maxDepth != INFINITE_DEPTH && depth >= maxDepth)) {
buffer.append(value);
}
else {
depth++;
buffer.append(ReflectionToStringBuilder.toString(value, this));
depth--;
}
}
// another helpful method
@Override
protected void appendDetail(StringBuffer buffer, String fieldName, Collection<?> coll) {
depth++;
buffer.append(ReflectionToStringBuilder.toString(coll.toArray(), this, true, true));
depth--;
}
}
关于Java toString - ToStringBuilder 不够;不会穿越,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3149951/
killer = killer.Replace("(", "\("); 试图在大括号前放置'\',它显示“错误1无法识别的转义序列” 我还尝试在其前面加上“@”,但这会导致两个反斜杠。 最佳答案 使用
我创建了一个包含书籍的小型数据库,并试图在 C# 中使用正则表达式获取书名、作者和书籍年份,但出现错误。 数据库看起来像这样: Eragon // Christopher Paolini // 200
当我尝试编译时 scalala我得到一个 OutOfMemoryError: > compile [info] [info] == compile == [info] Source analysi
这个问题在这里已经有了答案: Regex escape with \ or \\? (5 个答案) 关闭 4 年前。 我遇到了 ) 不够的错误,但据我所知,我已经得到了所有的 )。 行获取错误: s
我正在尝试使用 pthread_create 函数创建一个线程。电话是这样的:res pthread_create(&threadID, &atributte, function, argument)
我需要能够遍历我的整个对象图并记录所有成员字段的所有内容。 例如:对象 A 具有对象 B 的集合,对象 B 具有对象 C 的集合,并且 A、B、C 上有附加字段,等等。 Apache Commons
我的 create.jspx 中有一行,如下所示: 此字段显示一个框,其中的行由 2 个实体之间的关系 @ManyToMany 填充。 现在的问题是,第一个实体只有一个填充此框的 String 属性
ctags -R dirName, vim -t Tags 非常强大,因为在这两个命令之后,你现在可以在该项目的代码之间导航,例如你可以使用 :tag functionName 跳转到某个函数的代码,
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: iphone 5 simulator - Cannot click on bottom of screen?
我有一个庞大的关键字表,一个关键字出现在一个外键中,例如 key=2 word=download key=3 word=download key=4 word=game 目前我有另一个字段称为字母索引
这个问题在这里已经有了答案: How to change stack size for a .NET program? (4 个答案) 关闭 7 年前。 我在 bmp 中有以下用于“blob 填充”
我是一名优秀的程序员,十分优秀!