RSS Entries RSS
RSS Subscribe by Email

Easy Java Bean toString() using BeanUtils

I often want to have a String description of my beans for debugging or logging purposes, but hate having to manually concatenate the fields in my class to create a toString() method.  This code snippet using Apache Commons (a.k.a. Jakarta Commons) is very helpful for just such occasions:

	public String toString() {
		try {
			return BeanUtils.describe(this).toString();
		} catch (Exception e) {
			Logger.getLogger(this.getClass()).error("Error converting object to String", e);
		}
		return super.toString();
	}

Buy Me a Beer

Share and Enjoy:
  • description
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Furl
  • Slashdot
  • Digg
  • Google
  • Technorati
  • Facebook

Tags:

RSS feed for comments on this post · TrackBack URL

Leave a Comment