I wrote a little utility class to print out all the fields from a Java bean:
import java.lang.reflect.Method;
public class Stringifier {
public static String toString(Object bean)
{
StringBuffer buffer=new StringBuffer();
Class objClass=bean.getClass();
Method[] methods=objClass.getMethods();
buffer.append(bean.getClass().getName()+"[");
for (Method method : methods)
{
if (method.getName().startsWith("get") &&
method.getParameterTypes().length==0)
{
String name=method.getName();
buffer.append(name);
buffer.append("=");
try
{
buffer.append(method.invoke(bean));
}
catch(Exception exp)
{
exp.printStackTrace();
}
buffer.append(" ");
}
}
buffer.append("]");
return buffer.toString();
}
}
From your bean's toString() method you can then call "Stringifier.toString(this)" and get a String representation. It is not perfect and obviously may require changes to fit your specific needs, but it's a useful starting point.
