chris lombardi wrote:all it's public primitive values [...] all the public objects that the object contains.
I hope you don't really use public fields... You should consider making them private and adding getters and setters for them.
Anyway, you can easily do this yourself manually. If you don't want to do it manually you can use reflection to get hold of all the fields. As for adding the values, String concatenation or using a StringBuilder will do the conversion for you - adding the primitive values directly, adding "null" for null references and calling toString() on all other references.
thanks for responding.
No, i dont use public fields. i guess i should have said i want to write a deepToString for a java bean, in which all accessible properties are in turn deepToString'ed.
I've heard of reflection, and thiink i understand the concept, but am not sure where to start with that.
You can take a look at XStream to get a quick conversion of an object into either XML or JSON,
If you code it yourself, the trickiest part may be dealing with circular links amongst different objects. E.g. if A has a reference to B, and B has a reference to C, and C has a reference to A, you want to make usre your string representation doesn't go into an infinite loop. I suggest using a IdentityHashMap to keep track of which objects have already been included in the string representation.
Just a comment: I inherited some code in which that style of toString() was implemented manually for all of the JavaBeans in the system. It didn't take long before the output of toString() for some objects ran into the dozens of lines long. It didn't take long before I realized that wasn't all that useful.
Even a relatively simple inventory bean which contained item number, warehouse number, cases on hand, eaches on hand, cases on order, cases due today, cases reserved, eaches reserved, status, and earliest order due date produced something which was really cumbersome to read, and it was worse when I had a bean containing an array of several inventory beans.
So I guess I'm saying: be careful what you ask for, you might get it.
Rob Spoor wrote:Anyway, you can easily do this yourself manually.
True. Which makes me suspect that the people over at Apache Commons must have done it already. Although I haven't gone over there to check.
They have, it's part of the Lang sub-project. They have a ToStringBuilder class with ReflectionToStringBuilder as subclass. I never use it though, as I don't like including a library just to be a bit lazier. I implement my toString methods manually (although it does involve some copy-pasting sometimes )
Joined: Mar 05, 2008
Chris: are you talking about writing the actual toString() method for a class that you control the source code of? Or are you trying to look at the contents of other classes, perhaps including classes that you yourself cannot edit? Some of these answers work better for one use case than another, and I can't really tell which you are dealing with. The "deep copy" idea suggests to me that you are looking at these classes from the outside, not writing toString() for each class individually. My XStream suggestion was intended mostly for an outiside view.
If you are in fact writing toString() for each of the classes, I am a big fan of Pojomatic. Also works for building equals() and hashCode(). I think a helper library like this is a good idea whenever you're dealing with classes with a lot of fields that are maintained by different people - it's not unusual for one person to add a new field and forget to put it in the toString(), equals(), and hashCode() methods. Classes like Pojomatic and ReflectionToStringBuilder make it easy to include all (or most) fields by default.
i am mainly concerned with displaying the contents of other classes i do not have control of. After pondering the problem for a while, i decided to write a custom object display function, and just display what properties i am interested in. I realized that there are many issues that i will face, like circular references and arrays and collections, that i am not up to at the moment dealing with (!). What i really want is a generalized object display function that would return a string akin to what Eclipse shows for object variable display during debug.
I like the XML solution - i may implement that and see if it works for me.
The suggestions for writing a custom toString() function are highly valuable - thanks for all the input.