Author: pcl
Date: Thu Jan 31 11:01:45 2008
New Revision: 617191

URL: http://svn.apache.org/viewvc?rev=617191&view=rev
Log:
OPENJPA-251, OPENJPA-329. svn merge -c 616961 ../branches/1.0.x/

Added:
    openjpa/trunk/openjpa-kernel-5/src/test/
      - copied from r616961, openjpa/branches/1.0.x/openjpa-kernel-5/src/test/
    openjpa/trunk/openjpa-kernel-5/src/test/java/
      - copied from r616961, 
openjpa/branches/1.0.x/openjpa-kernel-5/src/test/java/
    openjpa/trunk/openjpa-kernel-5/src/test/java/org/
      - copied from r616961, 
openjpa/branches/1.0.x/openjpa-kernel-5/src/test/java/org/
    openjpa/trunk/openjpa-kernel-5/src/test/java/org/apache/
      - copied from r616961, 
openjpa/branches/1.0.x/openjpa-kernel-5/src/test/java/org/apache/
    openjpa/trunk/openjpa-kernel-5/src/test/java/org/apache/openjpa/
      - copied from r616961, 
openjpa/branches/1.0.x/openjpa-kernel-5/src/test/java/org/apache/openjpa/
    openjpa/trunk/openjpa-kernel-5/src/test/java/org/apache/openjpa/enhance/
      - copied from r616961, 
openjpa/branches/1.0.x/openjpa-kernel-5/src/test/java/org/apache/openjpa/enhance/
    
openjpa/trunk/openjpa-kernel-5/src/test/java/org/apache/openjpa/enhance/TestGetDeclaredMethod.java
      - copied unchanged from r616961, 
openjpa/branches/1.0.x/openjpa-kernel-5/src/test/java/org/apache/openjpa/enhance/TestGetDeclaredMethod.java
Modified:
    
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/Reflection.java
    
openjpa/trunk/openjpa-kernel/src/main/resources/org/apache/openjpa/enhance/localizer.properties

Modified: 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/Reflection.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/Reflection.java?rev=617191&r1=617190&r2=617191&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/Reflection.java
 (original)
+++ 
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/Reflection.java
 Thu Jan 31 11:01:45 2008
@@ -112,27 +112,62 @@
      * Invokes <code>cls.getDeclaredMethods()</code>, and returns the method
      * that matches the <code>name</code> and <code>param</code> arguments.
      * Avoids the exception thrown by <code>Class.getDeclaredMethod()</code>
-     * for performance reasons. <code>param</code> may be null.
+     * for performance reasons. <code>param</code> may be null. Additionally,
+     * if there are multiple methods with different return types, this will
+     * return the method defined in the least-derived class.
      *
      * @since 0.9.8
      */
-    private static Method getDeclaredMethod(Class cls, String name,
+    static Method getDeclaredMethod(Class cls, String name,
         Class param) {
         Method[] methods = (Method[]) AccessController.doPrivileged(
             J2DoPrivHelper.getDeclaredMethodsAction(cls));
+        Method candidate = null;
         for (int i = 0 ; i < methods.length; i++) {
            if (name.equals(methods[i].getName())) {
                 Class[] methodParams = methods[i].getParameterTypes();
                 if (param == null && methodParams.length == 0)
-                    return methods[i];
-                if (param != null && methodParams.length == 1
+                    candidate = mostDerived(methods[i], candidate);
+                else if (param != null && methodParams.length == 1
                     && param.equals(methodParams[0]))
-                    return methods[i];
+                    candidate = mostDerived(methods[i], candidate);
             }
         }
-        return null;
+        return candidate;
     }
-    
+
+    static Method mostDerived(Method meth1, Method meth2) {
+        if (meth1 == null)
+            return meth2;
+        if (meth2 == null)
+            return meth1;
+        
+        Class cls2 = meth2.getDeclaringClass();
+        Class cls1 = meth1.getDeclaringClass();
+
+        if (cls1.equals(cls2)) {
+            Class ret1 = meth1.getReturnType();
+            Class ret2 = meth2.getReturnType();
+            if (ret1.isAssignableFrom(ret2))
+                return meth2;
+            else if (ret2.isAssignableFrom(ret1))
+                return meth1;
+            else
+                throw new IllegalArgumentException(
+                    _loc.get("most-derived-unrelated-same-type", meth1, meth2)
+                        .getMessage());
+        } else {
+            if (cls1.isAssignableFrom(cls2))
+                return meth2;
+            else if (cls2.isAssignableFrom(cls1))
+                return meth1;
+            else
+                throw new IllegalArgumentException(
+                    _loc.get("most-derived-unrelated", meth1, meth2)
+                        .getMessage());
+        }
+    }
+
     /**
      * Return the field with the given name, optionally throwing an exception
      * if none.

Modified: 
openjpa/trunk/openjpa-kernel/src/main/resources/org/apache/openjpa/enhance/localizer.properties
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/resources/org/apache/openjpa/enhance/localizer.properties?rev=617191&r1=617190&r2=617191&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-kernel/src/main/resources/org/apache/openjpa/enhance/localizer.properties
 (original)
+++ 
openjpa/trunk/openjpa-kernel/src/main/resources/org/apache/openjpa/enhance/localizer.properties
 Thu Jan 31 11:01:45 2008
@@ -199,3 +199,8 @@
     configuration: {1}. One or more of the classes that were registered with \
     the system had references to these classes. Registered types: {0}
 enhance-error: An error occurred while enhancing {0}. Exception message: {1}
+most-derived-unrelated: Methods "{0}" and "{1}" are defined in types that do \
+    not have an interface or superclass inheritance relationship.
+most-derived-unrelated-same-type: Methods "{0}" and "{1}" are defined in the 
same \
+    type, but the method return types do not have an interface or superclass \
+    inheritance relationship.


Reply via email to