您可以使用遍历整个树
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; 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--; }}


