changeset 12:d6e69f64cd3e

meth: guardWithTest typing bug
author jrose
date Wed, 27 May 2009 13:00:00 -0700
parents 16e8d2a2f373
children 083732b6300a
files netbeans/meth/test/jdk/java/dyn/MethodHandlesTest.java
diffstat 1 files changed, 85 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/netbeans/meth/test/jdk/java/dyn/MethodHandlesTest.java	Wed May 27 02:15:28 2009 -0700
+++ b/netbeans/meth/test/jdk/java/dyn/MethodHandlesTest.java	Wed May 27 13:00:00 2009 -0700
@@ -48,6 +48,12 @@
  */
 public class MethodHandlesTest {
 
+    // current failures
+    @Test @Ignore("failure in call to makeRawRetypeOnly in ToGeneric")
+    public void testFail_1() {
+        testSpreadArguments(int.class, 6);
+    }
+
     public MethodHandlesTest() {
     }
 
@@ -641,36 +647,52 @@
     public void testSpreadArguments() {
         if (CAN_SKIP_WORKING)  return;
         System.out.println("spreadArguments");
-        for (int i = 0; i < 10; i++) {
-            Object[] args = new Object[i];
-            for (int j = 0; j < args.length; j++)
-                args[j] = randomArg(Integer.class);
-            MethodHandle target = ValueConversions.varargsArray(i);
-            // make sure the target does what we think it does:
-            if (i < 5) {
-                Object[] check = (Object[]) MethodHandles.invoke(target, args);
-                assertArrayEquals(args, check);
-                switch (i) {
-                    case 0:
-                        check = target.<Object[]>invoke();
-                        assertArrayEquals(args, check);
-                        break;
-                    case 1:
-                        check = target.<Object[]>invoke(args[0]);
-                        assertArrayEquals(args, check);
-                        break;
-                    case 2:
-                        check = target.<Object[]>invoke(args[0], args[1]);
-                        assertArrayEquals(args, check);
-                        break;
-                }
+        for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
+            System.out.println("spreadArguments "+argType);
+            for (int i = 0; i < 10; i++) {
+                if (argType == int.class && i >= 6)  continue; // FIXME Fail_1
+                testSpreadArguments(argType, i);
             }
-            MethodType newType = MethodType.make(Object.class, Object[].class);
-            MethodHandle result = MethodHandles.spreadArguments(target, newType);
-            Object[] returnValue = (Object[]) result.invoke((Object)args);
-            assertArrayEquals(args, returnValue);
         }
     }
+    public void testSpreadArguments(Class<?> argType, int i) {
+        Object[] args = new Object[i];
+        for (int j = 0; j < args.length; j++)
+            args[j] = randomArg(Integer.class);
+        MethodHandle target = ValueConversions.varargsArray(i);
+        // make sure the target does what we think it does:
+        if (i < 5) {
+            Object[] check = (Object[]) MethodHandles.invoke(target, args);
+            assertArrayEquals(args, check);
+            switch (i) {
+                case 0:
+                    check = target.<Object[]>invoke();
+                    assertArrayEquals(args, check);
+                    break;
+                case 1:
+                    check = target.<Object[]>invoke(args[0]);
+                    assertArrayEquals(args, check);
+                    break;
+                case 2:
+                    check = target.<Object[]>invoke(args[0], args[1]);
+                    assertArrayEquals(args, check);
+                    break;
+            }
+        }
+        MethodHandle target2 = changeArgTypes(target, argType);
+        MethodType newType = MethodType.make(Object.class, Object[].class);
+        MethodHandle result = MethodHandles.spreadArguments(target2, newType);
+        Object[] returnValue = (Object[]) result.invoke((Object)args);
+        assertArrayEquals(args, returnValue);
+    }
+
+    static MethodHandle changeArgTypes(MethodHandle target, Class<?> argType) {
+        ArrayList<Class<?>> argTypes = new ArrayList<Class<?>>();
+        for (int j = 0; j < target.type().parameterCount(); j++)
+            argTypes.add(argType);
+        MethodType ttype2 = MethodType.make(target.type().returnType(), argTypes);
+        return MethodHandles.convertArguments(target, ttype2);
+    }
 
     @Test @Ignore("unimplemented")
     public void testCollectArguments() {
@@ -767,33 +789,43 @@
         System.out.println("guardWithTest");
         for (int nargs = 0; nargs <= 3; nargs++) {
             if (nargs != 2)  continue;  // FIXME: test more later
-            MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.make(boolean.class, Object.class));
-            MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.makeGeneric(nargs));
-            MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.makeGeneric(nargs));
-            while (test.type().parameterCount() < nargs)
-                test = MethodHandles.dropArguments(test, test.type().parameterCount()-1, Object.class);
-            while (test.type().parameterCount() > nargs)
-                test = MethodHandles.insertArgument(test, 0, MISSING_ARG);
-            MethodHandle mh = MethodHandles.guardWithTest(test, target, fallback);
-            Object[][] argLists = {
-                { },
-                { "foo" }, { MISSING_ARG },
-                { "foo", "foo" }, { "foo", "bar" },
-                { "foo", "foo", "baz" }, { "foo", "bar", "baz" }
-            };
-            for (Object[] argList : argLists) {
-                if (argList.length != nargs)  continue;
-                boolean equals;
-                switch (nargs) {
-                case 0:   equals = true; break;
-                case 1:   equals = MISSING_ARG.equals(argList[0]); break;
-                default:  equals = argList[0].equals(argList[1]); break;
-                }
-                String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals");
-                System.out.println(logEntry(willCall, argList));
-                Object result = MethodHandles.invoke(mh, argList);
-                assertCalled(willCall, argList);
+            testGuardWithTest(nargs, Object.class);
+            testGuardWithTest(nargs, String.class);
+        }
+    }
+    void testGuardWithTest(int nargs, Class<?> argClass) {
+        MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.make(boolean.class, Object.class));
+        MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.makeGeneric(nargs));
+        MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.makeGeneric(nargs));
+        while (test.type().parameterCount() < nargs)
+            test = MethodHandles.dropArguments(test, test.type().parameterCount()-1, Object.class);
+        while (test.type().parameterCount() > nargs)
+            test = MethodHandles.insertArgument(test, 0, MISSING_ARG);
+        if (argClass != Object.class) {
+            test = changeArgTypes(test, argClass);
+            target = changeArgTypes(target, argClass);
+            fallback = changeArgTypes(fallback, argClass);
+        }
+        MethodHandle mh = MethodHandles.guardWithTest(test, target, fallback);
+        assertEquals(target.type(), mh.type());
+        Object[][] argLists = {
+            { },
+            { "foo" }, { MISSING_ARG },
+            { "foo", "foo" }, { "foo", "bar" },
+            { "foo", "foo", "baz" }, { "foo", "bar", "baz" }
+        };
+        for (Object[] argList : argLists) {
+            if (argList.length != nargs)  continue;
+            boolean equals;
+            switch (nargs) {
+            case 0:   equals = true; break;
+            case 1:   equals = MISSING_ARG.equals(argList[0]); break;
+            default:  equals = argList[0].equals(argList[1]); break;
             }
+            String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals");
+            System.out.println(logEntry(willCall, argList));
+            Object result = MethodHandles.invoke(mh, argList);
+            assertCalled(willCall, argList);
         }
     }