Le 09/09/2009 23:22, Andrew John Hughes a écrit :
Given you've listed utility methods for two Object methods, equals and
hashCode, toString seems an obvious one to handle as well:

public static String toString(Object o)
   throws IllegalAccessException
{
   Class<?>  c = o.getClass();
   StringBuilder b = new StringBuilder(c.getName());
   b.append("[");
   for (Field f : c.getDeclaredFields())
   {
     f.setAccessible(true);
     b.append(f.getName() + "=" + f.get());
     b.append(",");
   }
   b.replace(b.length() - 1, b.length(), "]");
   return b.toString();
}

Maybe there's also a useful utility implementation of clone too, but I
can't think of one offhand.

Arghhh,

b.append(f.getName() + "=" + f.get());

shoud be

b.append(f.getName()).append("=").append(f.get(o));


And I think there is a problem if the object has no field.

Rémi

Reply via email to