This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Anyone know of a utility to do a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Anyone know of a utility to do a "deep toString" of an object?" Watch "Anyone know of a utility to do a "deep toString" of an object?" New topic
Author

Anyone know of a utility to do a "deep toString" of an object?

chris lombardi
Greenhorn

Joined: Mar 02, 2011
Posts: 3

I want to do a "toString()" of an object that prints the values of all it's public primitive values, as well as a "recursive deep toString()" print of all the public objects that the object contains.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

Welcome to the Ranch.
What have you tried so far and where are you stuck?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
chris lombardi
Greenhorn

Joined: Mar 02, 2011
Posts: 3

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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Paul Clapham wrote:
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 )
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
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.

chris lombardi
Greenhorn

Joined: Mar 02, 2011
Posts: 3

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anyone know of a utility to do a "deep toString" of an object?
 
Similar Threads
Problem Using toString method from a different class
Question about clone a Hashtable
toString()
ArrayList class
Get Original Object