Monday, November 30, 2009

toString

Ever thought how nice it would be if the method toString(), defined in Java's Object class, was more intelligent?  By default it shows the name of the class, followed by "@", followed by the hash code of the class (presented in hex).  Wouldn't it be nice if it would actually show some of its property values, relationships with other objects, etc. without you having to do all that coding

Well, now you can use my latest project at SourceForge: toString (http://sourceforge.net/projects/tostring0/).  By default, if your object extends the abstract class AToString (which in turn extends Object), toString() will return all of the exposed bean properties of the object (the getters, in other words).  If you want to add or omit fields or methods, then you use the @ToString annotation.  You never need to actually write anything yourself to override the toString() method (although of course you still can do so).  If you can't (or don't want to) extend AToString, then you can have your objects implement the interface IToString, which adds another more complex toString(...) signature which is used by the toString utitilities.  If you don't want to have anything to do with the abstract class or interface, you can still invoke the class methods in ToStringUtils.

No comments:

Post a Comment