Archive for February, 2012

Using Reflection to print out an object

This is really old news but I always forget which class to use when I need to provide a toString method that uses reflection to print out all the classes values

ToStringBuilder.reflectionToString in the apache commons library gives this functionality.

e.g.


@Override
public String toString() {
    return ToStringBuilder.reflectionToString(this);
}

We are currently starting to use Guava as a Utility library and the class Objects.ToStringHelper provides nice utilities for printing out an objects values but unfortunately doesn’t support reflection yet. The support for reflection is discussed here