After some email with Bill, it looks like we can use Class.getDeclaredMethods to find which method the class implements (you learn something new every day). It specifically excludes inherited methods, so we can use it to verify if a primary key has actually implemented hashCode and equals.
Class.getDeclaredMethod("equals", new Class[] { Object.class }) should also do the trick and won't return inherited methods.
Since equals equals is not really inheritable (see Effective Java), I think we should throw a verifier error if a pk does not directly implement it.
I haven't read Effective Java, but this won't work for us. We intentionally create derived primary key classes for each entity. These are derived from generic pk classes when the primary key data is a simple primative type. The super class implements equals, compareTo and hashCode. I don't see any reason these would need to be reimplemented in each derived class.
The purpose of the derived classes is primarly for type safety.
HashCode on the other hand can be inherited (and still be valid), so I think we should only print a warning if they don't directly. We could check the parents until we get to Object to see if they left the default implementation.
Who maintains the verifier?
-dain
Here is the code I wrote in to test this:
public static boolean definesEquals(Class clazz)
{
Method[] method = clazz.getDeclaredMethods();
for(int i=0; i<method.length; i++)
{
if(method[i].getName().equals("equals") &&
method[i].getParameterTypes().length == 1 &&
method[i].getParameterTypes()[0] == Object.class &&
method[i].getReturnType() == Boolean.TYPE)
{
return true;
}
}
return false;
}
How about: (off the cuff and untested)
public static boolean definesEquals(Class clazz) { Class[] params = new Class[] { Object.class };
while (clazz != null && !clazz.equals(Object.class)) { try { Method m = clazz.getDeclaredMethod("equals", params); if (m.getReturnType() == Integer.TYPE) return true; } catch (NoSuchMethodException) { } clazz = clazz.getSuperclass(); } return false; }
--Victor Langelo
------------------------------------------------------- This SF.net email is sponsored by: The Definitive IT and Networking Event. Be There! NetWorld+Interop Las Vegas 2003 -- Register today! http://ads.sourceforge.net/cgi-bin/redirect.pl?keyn0001en _______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-development