Request for review: Make MethodHandleProxies.isSingleMethod() agnostic of ordering of methods returned from Class.getMethods()

Neil Richards neil.richards at ngmr.net
Wed Jul 20 10:51:59 UTC 2011


java.lang.invoke.MethodHandleProxies.isSingleMethod(Class<?> intf)
returns a boolean indicating whether the provided interface has only one
defined method upon it.

When making this consideration, it tries not count any method defined on
the interface that is also defined by java.lang.Object (and so will
always be there for all implementations of the interface).

Because of this, for example, isSingleMethod should return 'true' for
java.util.Comparator, which has two defined methods - compare() and
equals() - as all but one of these methods are also defined by
java.lang.Object.

isSingleMethod() makes the consideration my iterating through the array
of Method objects returned from Class.getMethods().

The API javadoc for Class.getMethods() [1] says (amongst other things):
"The elements in the array returned are not sorted and are not in any
particular order." 

However, isSingleMethod() currently relies upon any methods defined on
java.lang.Object appearing in the list before any others.

I suppose this happens to be the case currently (at least, when using
hotspot), but it is a generally brittle assumption for this code to rely
upon.

Please find below a suggested change which removes this assumption by
making the algorithm agnostic to the ordering returned by
Class.getMethods().

Please consider this change for committal.

Thanks,
Neil

[1] http://download.oracle.com/javase/7/docs/api/java/lang/Class.html#getMethods%28%29

-- 
Unless stated above:
IBM email: neil_richards at uk.ibm.com
IBM United Kingdom Limited - Registered in England and Wales with number 741598.
Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU


# HG changeset patch
# User Neil Richards <neil.richards at ngmr.net>, <neil_richards at uk.ibm.com>
# Date 1311156497 -3600
# Branch ojdk-131
# Node ID 1062362b6e7a39d9bc3f468cfd33ae100371413e
# Parent  8bbea505b060fc7c97d9c241f8531a2a758cbe20
Summary: Make isSingleMethod() agnostic of ordering of methods returned from Class.getMethods()
Contributed-by: <neil.richards at ngmr.net>

diff -r 8bbea505b060 -r 1062362b6e7a src/share/classes/java/lang/invoke/MethodHandleProxies.java
--- a/src/share/classes/java/lang/invoke/MethodHandleProxies.java	Mon Jul 18 22:25:58 2011 +0100
+++ b/src/share/classes/java/lang/invoke/MethodHandleProxies.java	Wed Jul 20 11:08:17 2011 +0100
@@ -246,8 +246,8 @@
         Method sm = null;
         for (Method m : intfc.getMethods()) {
             int mod = m.getModifiers();
-            if (Modifier.isAbstract(mod)) {
-                if (sm != null && !isObjectMethod(sm))
+            if (Modifier.isAbstract(mod) && !isObjectMethod(m)) {
+                if (sm != null)
                     return null;  // too many abstract methods
                 sm = m;
             }





More information about the core-libs-dev mailing list