I'm looking for a library function (ideally from a commonly used framework e.g. Spring, Guava, Apache Commons etc.) that will nicely print the values of any Java object.
This is a general question rather than a specific one. Have seen similar questions on StackOverflow for which a common answer is "implement your own toString()
method on the class" but this option isn't always practical - am looking for a general way of doing this with any object I come across, which may originate from third party code. Another suggestion is to use RefectionToStringBuilder from Apache Commons, e.g:
new ReflectionToStringBuilder(complexObject, new RecursiveToStringStyle()).toString()
But this has limited use - e.g. when it comes across a collection it tends to output something like this:
java.util.ArrayList@fcc7ab1[size=1]
An actual use case example is to log an Iterable<PushResult>
returned from JGit's pushCommand.call()
method - if posting an answer please make sure it would work with this as well as any other complex object.
ReflectionToStringBuilder
before, so I won't post this as an answer, but it looks like you might be able to do something like override thegetValue
method to return an array when aCollection
is given as the field (or even just your ownString
version of that collection). – Avaavadavat