changeset 5215:f8e6099939bb

More lambda translation bugfixes. When adapting argument types, don't forget adaptations for char-to-{long,double,float}.
author briangoetz
date Wed, 11 Apr 2012 17:32:13 -0400
parents 1555376b4099
children 8d263b038064
files src/share/classes/java/lang/invoke/InnerClassGenerator.java test-ng/tests/org/openjdk/tests/javac/LambdaTranslationTest2.java
diffstat 2 files changed, 226 insertions(+), 225 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/lang/invoke/InnerClassGenerator.java	Wed Apr 11 17:18:12 2012 -0400
+++ b/src/share/classes/java/lang/invoke/InnerClassGenerator.java	Wed Apr 11 17:32:13 2012 -0400
@@ -317,6 +317,7 @@
                         switch (argSort) {
                             case Type.BYTE:
                             case Type.SHORT:
+                            case Type.CHAR:
                             case Type.INT:
                                 mv.visitInsn(Opcodes.I2D);
                                 break;
@@ -332,6 +333,7 @@
                         switch (argSort) {
                             case Type.BYTE:
                             case Type.SHORT:
+                            case Type.CHAR:
                             case Type.INT:
                                 mv.visitInsn(Opcodes.I2F);
                                 break;
@@ -347,6 +349,7 @@
                         switch (argSort) {
                             case Type.BYTE:
                             case Type.SHORT:
+                            case Type.CHAR:
                             case Type.INT:
                                 mv.visitInsn(Opcodes.I2L);
                                 break;
--- a/test-ng/tests/org/openjdk/tests/javac/LambdaTranslationTest2.java	Wed Apr 11 17:18:12 2012 -0400
+++ b/test-ng/tests/org/openjdk/tests/javac/LambdaTranslationTest2.java	Wed Apr 11 17:32:13 2012 -0400
@@ -1,225 +1,223 @@
-package org.openjdk.tests.javac;
-
-import org.testng.annotations.Test;
-
-import java.util.functions.Mapper;
-import java.util.functions.Predicate;
-
-import static org.testng.Assert.*;
-
-/**
- * LambdaTranslationTest2 -- end-to-end smoke tests for lambda evaluation
- */
-@Test
-public class LambdaTranslationTest2 {
-
-    final String dummy = "dummy";
-
-    public void testLambdas() {
-        Predicate<String> isEmpty = s -> s.isEmpty();
-        assertTrue(isEmpty.eval(""));
-        assertTrue(!isEmpty.eval("foo"));
-
-        Predicate<Object> oIsEmpty = s -> ((String) s).isEmpty();
-        assertTrue(oIsEmpty.eval(""));
-        assertTrue(!oIsEmpty.eval("foo"));
-
-        Predicate<Object> alwaysTrue = o -> true;
-        assertTrue(alwaysTrue.eval(""));
-        assertTrue(alwaysTrue.eval(null));
-
-        Predicate<Object> alwaysFalse = o -> false;
-        assertTrue(!alwaysFalse.eval(""));
-        assertTrue(!alwaysFalse.eval(null));
-
-        // tests local capture
-        String foo = "foo";
-        Predicate<String> equalsFoo = s -> s.equals(foo);
-        assertTrue(!equalsFoo.eval(""));
-        assertTrue(equalsFoo.eval("foo"));
-
-        // tests instance capture
-        Predicate<String> equalsDummy = s -> s.equals(dummy);
-        assertTrue(!equalsDummy.eval(""));
-        assertTrue(equalsDummy.eval("dummy"));
-
-        Mapper<Object, Object> ident = s -> s;
-
-        assertEquals("blarf", ident.map("blarf"));
-        assertEquals("wooga", ident.map("wooga"));
-        assertTrue("wooga" == ident.map("wooga"));
-
-        // constant capture
-        Mapper<Object, Object> prefixer = s -> "p" + s;
-        assertEquals("pblarf", prefixer.map("blarf"));
-        assertEquals("pwooga", prefixer.map("wooga"));
-
-        // instance capture
-        Mapper<Object, Object> prefixer2 = s -> "dummy" + s;
-        assertEquals("dummyblarf", prefixer2.map("blarf"));
-        assertEquals("dummywooga", prefixer2.map("wooga"));
-    }
-
-    interface Factory<T> {
-        T make();
-    }
-
-    interface StringFactory extends Factory<String> { }
-
-    interface StringFactory2 extends Factory<String> {
-        String make();
-    }
-
-    public void testBridges() {
-        Factory<String> of = () -> "y";
-        Factory<?> ef = () -> "z";
-
-        assertEquals("y", of.make());
-        assertEquals("y", ((Factory<?>) of).make());
-        assertEquals("y", ((Factory) of).make());
-
-        assertEquals("z", ef.make());
-        assertEquals("z", ((Factory) ef).make());
-    }
-
-    public void testBridgesImplicitSpecialization() {
-        StringFactory sf = () -> "x";
-
-        assertEquals("x", sf.make());
-        assertEquals("x", ((Factory<String>) sf).make());
-        assertEquals("x", ((Factory<?>) sf).make());
-        assertEquals("x", ((Factory) sf).make());
-    }
-
-    public void testBridgesExplicitSpecialization() {
-        StringFactory2 sf = () -> "x";
-
-        assertEquals("x", sf.make());
-        assertEquals("x", ((Factory<String>) sf).make());
-        assertEquals("x", ((Factory<?>) sf).make());
-        assertEquals("x", ((Factory) sf).make());
-    }
-
-    public void testSuperCapture() {
-        class A {
-            String make() { return "x"; }
-        }
-
-        class B extends A {
-            void testSuperCapture() {
-                StringFactory sf = () -> super.make();
-                assertEquals("x", sf.make());
-            }
-        }
-
-        new B().testSuperCapture();
-    }
-
-    interface WidenD {
-        public String m(float a0, double a1);
-    }
-
-    interface WidenS {
-        public String m(byte a0, short a1);
-    }
-
-    interface WidenI {
-        public String m(byte a0, short a1, char a2, int a3);
-    }
-
-    interface WidenL {
-        public String m(byte a0, short a1, char a2, int a3, long a4);
-    }
-
-    static String pwI1(int a0, int a1, int a2, int a3) {
-        return String.format("b%d s%d c%d i%d", a0, a1, a2, a3);
-    }
-
-    static String pwI2(Integer a0, Integer a1, Integer a2, Integer a3) {
-        return String.format("b%d s%d c%d i%d", a0, a1, a2, a3);
-    }
-
-    static String pwL1(long a0, long a1, long a2, long a3, long a4) {
-        return String.format("b%d s%d c%d i%d j%d", a0, a1, a2, a3, a4);
-    }
-
-    static String pwL2(Long a0, Long a1, Long a2, Long a3, Long a4) {
-        return String.format("b%d s%d c%d i%d j%d", a0, a1, a2, a3, a4);
-    }
-
-    static String pwS1(short a0, short a1) {
-        return String.format("b%d s%d", a0, a1);
-    }
-
-    static String pwS2(Short a0, Short a1) {
-        return String.format("b%d s%d", a0, a1);
-    }
-
-    static String pwD1(double a0, double a1) {
-        return String.format("f%f d%f", a0, a1);
-    }
-
-    static String pwD2(Double a0, Double a1) {
-        return String.format("f%f d%f", a0, a1);
-    }
-
-    // @@@ currently fails
-    @Test(expectedExceptions = BootstrapMethodError.class)
-    public void testPrimitiveWidening() {
-        WidenS ws1 = LambdaTranslationTest2::pwS1;
-        assertEquals("b1 s2", ws1.m((byte) 1, (short) 2));
-
-        // @@@ Commented out because these won't currently compile; awaiting spec clarification
-//        WidenS ws2 = LambdaTranslationTest2::pwS2;
-//        assertEquals("b1 s2", ws2.m((byte) 1, (short) 2));
-
-        WidenD wd1 = LambdaTranslationTest2::pwD1;
-        assertEquals("f1.000000 d2.000000", wd1.m(1.0f, 2.0));
-
-//        WidenD wd2 = LambdaTranslationTest2::pwD2;
-//        assertEquals("f1.000000 d2.000000", wd2.m(1.0f, 2.0));
-
-        WidenI wi1 = LambdaTranslationTest2::pwI1;
-        assertEquals("b1 s2 c3 i4", wi1.m((byte) 1, (short) 2, (char) 3, 4));
-
-//        WidenI wi2 = LambdaTranslationTest2::pwI2;
-//        assertEquals("b1 s2 c3 i4", wi1.m((byte) 1, (short) 2, (char) 3, 4));
-
-        WidenL wl1 = LambdaTranslationTest2::pwL1;
-        assertEquals("b1 s2 c3 i4 j4", wl1.m((byte) 1, (short) 2, (char) 3, 4, 5L));
-
-//        WidenL wl2 = LambdaTranslationTest2::pwL2;
-//        assertEquals("b1 s2 c3 i4 j4", wl2.m((byte) 1, (short) 2, (char) 3, 4, 5L));
-    }
-
-    interface Unbox {
-        public String m(Byte a0, Short a1, Character a2, Integer a3, Long a4, Boolean a5, Float a6, Double a7);
-    }
-
-    static String pu(byte a0, short a1, char a2, int a3, long a4, boolean a5, float a6, double a7) {
-        return String.format("b%d s%d c%c i%d j%d z%b f%f d%f", a0, a1, a2, a3, a4, a5, a6, a7);
-    }
-
-    public void testUnboxing() {
-        Unbox u = LambdaTranslationTest2::pu;
-        assertEquals("b1 s2 cA i4 j5 ztrue f6.000000 d7.000000", u.m((byte)1, (short) 2, 'A', 4, 5L, true, 6.0f, 7.0));
-    }
-
-    static boolean cc(Object o) {
-        return ((String) o).equals("foo");
-    }
-
-    public void testArgCastingAdaptation() {
-        Predicate<String> p = LambdaTranslationTest2::cc;
-        assertTrue(p.eval("foo"));
-        assertTrue(!p.eval("bar"));
-    }
-
-    interface SonOfPredicate<T> extends Predicate<T> { }
-
-    public void testExtendsSAM() {
-        SonOfPredicate<String> p = s -> s.isEmpty();
-        assertTrue(p.eval(""));
-        assertTrue(!p.eval("foo"));
-    }
-}
+package org.openjdk.tests.javac;
+
+import org.testng.annotations.Test;
+
+import java.util.functions.Mapper;
+import java.util.functions.Predicate;
+
+import static org.testng.Assert.*;
+
+/**
+ * LambdaTranslationTest2 -- end-to-end smoke tests for lambda evaluation
+ */
+@Test
+public class LambdaTranslationTest2 {
+
+    final String dummy = "dummy";
+
+    public void testLambdas() {
+        Predicate<String> isEmpty = s -> s.isEmpty();
+        assertTrue(isEmpty.eval(""));
+        assertTrue(!isEmpty.eval("foo"));
+
+        Predicate<Object> oIsEmpty = s -> ((String) s).isEmpty();
+        assertTrue(oIsEmpty.eval(""));
+        assertTrue(!oIsEmpty.eval("foo"));
+
+        Predicate<Object> alwaysTrue = o -> true;
+        assertTrue(alwaysTrue.eval(""));
+        assertTrue(alwaysTrue.eval(null));
+
+        Predicate<Object> alwaysFalse = o -> false;
+        assertTrue(!alwaysFalse.eval(""));
+        assertTrue(!alwaysFalse.eval(null));
+
+        // tests local capture
+        String foo = "foo";
+        Predicate<String> equalsFoo = s -> s.equals(foo);
+        assertTrue(!equalsFoo.eval(""));
+        assertTrue(equalsFoo.eval("foo"));
+
+        // tests instance capture
+        Predicate<String> equalsDummy = s -> s.equals(dummy);
+        assertTrue(!equalsDummy.eval(""));
+        assertTrue(equalsDummy.eval("dummy"));
+
+        Mapper<Object, Object> ident = s -> s;
+
+        assertEquals("blarf", ident.map("blarf"));
+        assertEquals("wooga", ident.map("wooga"));
+        assertTrue("wooga" == ident.map("wooga"));
+
+        // constant capture
+        Mapper<Object, Object> prefixer = s -> "p" + s;
+        assertEquals("pblarf", prefixer.map("blarf"));
+        assertEquals("pwooga", prefixer.map("wooga"));
+
+        // instance capture
+        Mapper<Object, Object> prefixer2 = s -> "dummy" + s;
+        assertEquals("dummyblarf", prefixer2.map("blarf"));
+        assertEquals("dummywooga", prefixer2.map("wooga"));
+    }
+
+    interface Factory<T> {
+        T make();
+    }
+
+    interface StringFactory extends Factory<String> { }
+
+    interface StringFactory2 extends Factory<String> {
+        String make();
+    }
+
+    public void testBridges() {
+        Factory<String> of = () -> "y";
+        Factory<?> ef = () -> "z";
+
+        assertEquals("y", of.make());
+        assertEquals("y", ((Factory<?>) of).make());
+        assertEquals("y", ((Factory) of).make());
+
+        assertEquals("z", ef.make());
+        assertEquals("z", ((Factory) ef).make());
+    }
+
+    public void testBridgesImplicitSpecialization() {
+        StringFactory sf = () -> "x";
+
+        assertEquals("x", sf.make());
+        assertEquals("x", ((Factory<String>) sf).make());
+        assertEquals("x", ((Factory<?>) sf).make());
+        assertEquals("x", ((Factory) sf).make());
+    }
+
+    public void testBridgesExplicitSpecialization() {
+        StringFactory2 sf = () -> "x";
+
+        assertEquals("x", sf.make());
+        assertEquals("x", ((Factory<String>) sf).make());
+        assertEquals("x", ((Factory<?>) sf).make());
+        assertEquals("x", ((Factory) sf).make());
+    }
+
+    public void testSuperCapture() {
+        class A {
+            String make() { return "x"; }
+        }
+
+        class B extends A {
+            void testSuperCapture() {
+                StringFactory sf = () -> super.make();
+                assertEquals("x", sf.make());
+            }
+        }
+
+        new B().testSuperCapture();
+    }
+
+    interface WidenD {
+        public String m(float a0, double a1);
+    }
+
+    interface WidenS {
+        public String m(byte a0, short a1);
+    }
+
+    interface WidenI {
+        public String m(byte a0, short a1, char a2, int a3);
+    }
+
+    interface WidenL {
+        public String m(byte a0, short a1, char a2, int a3, long a4);
+    }
+
+    static String pwI1(int a0, int a1, int a2, int a3) {
+        return String.format("b%d s%d c%d i%d", a0, a1, a2, a3);
+    }
+
+    static String pwI2(Integer a0, Integer a1, Integer a2, Integer a3) {
+        return String.format("b%d s%d c%d i%d", a0, a1, a2, a3);
+    }
+
+    static String pwL1(long a0, long a1, long a2, long a3, long a4) {
+        return String.format("b%d s%d c%d i%d j%d", a0, a1, a2, a3, a4);
+    }
+
+    static String pwL2(Long a0, Long a1, Long a2, Long a3, Long a4) {
+        return String.format("b%d s%d c%d i%d j%d", a0, a1, a2, a3, a4);
+    }
+
+    static String pwS1(short a0, short a1) {
+        return String.format("b%d s%d", a0, a1);
+    }
+
+    static String pwS2(Short a0, Short a1) {
+        return String.format("b%d s%d", a0, a1);
+    }
+
+    static String pwD1(double a0, double a1) {
+        return String.format("f%f d%f", a0, a1);
+    }
+
+    static String pwD2(Double a0, Double a1) {
+        return String.format("f%f d%f", a0, a1);
+    }
+
+    public void testPrimitiveWidening() {
+        WidenS ws1 = LambdaTranslationTest2::pwS1;
+        assertEquals("b1 s2", ws1.m((byte) 1, (short) 2));
+
+        // @@@ Commented out because these won't currently compile; awaiting spec clarification
+//        WidenS ws2 = LambdaTranslationTest2::pwS2;
+//        assertEquals("b1 s2", ws2.m((byte) 1, (short) 2));
+
+        WidenD wd1 = LambdaTranslationTest2::pwD1;
+        assertEquals("f1.000000 d2.000000", wd1.m(1.0f, 2.0));
+
+//        WidenD wd2 = LambdaTranslationTest2::pwD2;
+//        assertEquals("f1.000000 d2.000000", wd2.m(1.0f, 2.0));
+
+        WidenI wi1 = LambdaTranslationTest2::pwI1;
+        assertEquals("b1 s2 c3 i4", wi1.m((byte) 1, (short) 2, (char) 3, 4));
+
+//        WidenI wi2 = LambdaTranslationTest2::pwI2;
+//        assertEquals("b1 s2 c3 i4", wi1.m((byte) 1, (short) 2, (char) 3, 4));
+
+        WidenL wl1 = LambdaTranslationTest2::pwL1;
+        assertEquals("b1 s2 c3 i4 j5", wl1.m((byte) 1, (short) 2, (char) 3, 4, 5L));
+
+//        WidenL wl2 = LambdaTranslationTest2::pwL2;
+//        assertEquals("b1 s2 c3 i4 j4", wl2.m((byte) 1, (short) 2, (char) 3, 4, 5L));
+    }
+
+    interface Unbox {
+        public String m(Byte a0, Short a1, Character a2, Integer a3, Long a4, Boolean a5, Float a6, Double a7);
+    }
+
+    static String pu(byte a0, short a1, char a2, int a3, long a4, boolean a5, float a6, double a7) {
+        return String.format("b%d s%d c%c i%d j%d z%b f%f d%f", a0, a1, a2, a3, a4, a5, a6, a7);
+    }
+
+    public void testUnboxing() {
+        Unbox u = LambdaTranslationTest2::pu;
+        assertEquals("b1 s2 cA i4 j5 ztrue f6.000000 d7.000000", u.m((byte)1, (short) 2, 'A', 4, 5L, true, 6.0f, 7.0));
+    }
+
+    static boolean cc(Object o) {
+        return ((String) o).equals("foo");
+    }
+
+    public void testArgCastingAdaptation() {
+        Predicate<String> p = LambdaTranslationTest2::cc;
+        assertTrue(p.eval("foo"));
+        assertTrue(!p.eval("bar"));
+    }
+
+    interface SonOfPredicate<T> extends Predicate<T> { }
+
+    public void testExtendsSAM() {
+        SonOfPredicate<String> p = s -> s.isEmpty();
+        assertTrue(p.eval(""));
+        assertTrue(!p.eval("foo"));
+    }
+}