Similarly, I've previously written reflection-based #clone, #equals, and #fillRandomData methods working on POJOs. They were useful especially in testing the data binding codes, ie. X original = new X();, fillRandomData(original); mapper.toGUI( clone(original) ); //Clone object so original data can not be modified X rv = new X(); mapper.fromGUI(rv); assertTrue( equals(original, rv) );
But I can not imagine much use in a production code. If other use cases can be suggested, we could consider putting into Objects class. On Thu, Sep 10, 2009 at 2:25 AM, Kevin Bourrillion <kev...@google.com>wrote: > -1 on reflection-based stuff in the Objects class. > > equal() and hashCode() are the only two things we saw fit to include in our > Objects class, and actually, our hashCode() is identical to > Arrays.hashCode() (but is varargs). > > > http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/base/Objects.html > > Please do add the compare() methods to all the rest of the wrapper types; > we would use those a lot. > > When it comes to cloning, I tried very hard once to provide something > useful -- a "clone any object without knowing its concrete type" method -- > and it was a disaster. A total mess of reflection, and no one wanted to use > it anyway. Cloning in Java is discredited. > > So I'm left with only equal() for your new Objects class. By the way, the > best implementation is a == b || (a != null && a.equals(b)). > > > > > On Wed, Sep 9, 2009 at 2:22 PM, Andrew John Hughes < > gnu_and...@member.fsf.org> wrote: > >> 2009/9/9 Joe Darcy <joe.da...@sun.com>: >> > Hello. >> > >> > For JDK 7, I think it is high-time the platform included a class like >> > java.util.Objects to hold commonly-written utility methods. For >> example, a >> > two-argument static equals method that returned true if both arguments >> are >> > null, returns false is one argument is null, and otherwise returns the >> > result of calling arg1.equals(arg2) (6797535 "Add shared two argument >> > static equals method to the platform"). >> > >> > A static hashCode method returning 0 for null and the value of >> > arg.hashCode() has also been suggested. >> > >> > A set of >> > >> > static int compareTo(int, int) >> > static int compareTo(long, long) >> > .... >> > >> > methods probably belongs somewhere in the platform too. >> > >> > What other utility methods would have broad enough use and applicability >> to >> > go into a common java.util class? >> > >> > -Joe >> > >> >> 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. >> -- >> Andrew :-) >> >> Free Java Software Engineer >> Red Hat, Inc. (http://www.redhat.com) >> >> Support Free Java! >> Contribute to GNU Classpath and the OpenJDK >> http://www.gnu.org/software/classpath >> http://openjdk.java.net >> >> PGP Key: 94EFD9D8 (http://subkeys.pgp.net) >> Fingerprint: F8EF F1EA 401E 2E60 15FA 7927 142C 2591 94EF D9D8 >> > > > > -- > Kevin Bourrillion @ Google > internal: http://go/javalibraries > google-collections.googlecode.com > google-guice.googlecode.com > >