I need to be able to traverse through my entire object graph and log all contents of all member fields.
For example: Object A has a collection of Object B’s which has a collection of Object C’s and A, B, C have additional fields on them, etc.
Apache Commons ToStringBuilder is not sufficient since it won’t traverse down an object graph or output contents of a collection.
Does anyone know of another library that will do this or have a code snippet that does this?
You can traverse the whole tree using
org.apache.commons.lang.builder.ReflectionToStringBuilder. The trick is that inToStringStyleyou need to traverse into the value.ToStringStylewill take care of values, already processed, and will not allow recursion. Here we go: