On Wednesday, March 26, 2003, at 09:29 PM, Victor Langelo wrote:

Dain Sundstrom wrote:

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.

I dumb; I missed that one.



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.

I loaned my copy of Effective Java to a friend so I can't quote. The basic idea is that if a.equals(b) is true b.equals(a) must also be true. This means you must test for the exact type of the related compare to object. You must have code that looks something like this.


public boolean equals(object o)
{
   if(o instanceof MyType)
   {
      return value.equals((MyType).value);
   }
   return false;
}

The important part is the instance of check. I suppose you could do this check with reflection... something like this

if(getClass() == o.getClass())

So I guess you are right, but we know that if one of the super classes (other then Object) we know that the implementation is wrong.

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;
}

That should work.


-dain



-------------------------------------------------------
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

Reply via email to