• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 3
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.
What have you tried so far and where are you stuck?
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
chris lombardi
Greenhorn
Posts: 3
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic