This post originated from an RSS feed registered with Java Buzz
by Marc Logemann.
Original Post: override toString() reflectively
Feed Title: Marc's Java Blog
Feed URL: http://www.logemann.org/day/index_java.xml
Feed Description: Java related topics for all major areas. So you will see J2ME, J2SE and J2EE issues here.
Sometimes it makes really sense to look at other peoples code, especially when you know that the target code was developed by a skilled developer.
As many others, i am regulary overriding toString() for a more human friendly object representation. Because being lazy, i never invested time to do it cleanly so i handcoded the needed attributes with descriptive text in there. Now i saw that:
public String toString() {
return ReflectionToStringBuilder.reflectionToString(this,
ToStringStyle.MULTI_LINE_STYLE);
}
is easier, faster, better and everything. As allways, its not the question if there are good solutions for certain problems, but where to look for. In this case, you should look for Commons-Lang from Apache to use this piece of code.