I’d a method that should read all public methods (including superclass) and for each non-null attribute store it´s hashCode.
private static String reflect(Object o) {
StringBuilder key = new StringBuilder();
try {
for (Method m : o.getClass().getMethods()) {
if (m.getName().startsWith("get") || m.getName().startsWith("is")) {
Object result = m.invoke(o, new Object[]{});
key.append(result != null ? result.hashCode() : "-");
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return key.toString();
}
Is this code Ok or I´ve forgetting something important in relation to my needs?
The only obvious thing I can see is that if you’re using the hashcodes to identify changes in the object, you should know that the order of methods returned by getMethods() is “not in any particular order”, so there is no guarantee that they’ll be returned in the same order in another JVM, or even from call to call.