changeset 5218:fe29b12a9fff

Generate classes so as to avoid accessibility checks. This enables compiler to desugar lambda bodies as private methods.
author briangoetz
date Thu, 12 Apr 2012 15:19:23 -0400
parents cd96d66169e4
children 08c12c22393c
files src/share/classes/java/lang/invoke/InnerClassGenerator.java test-ng/tests/org/openjdk/tests/javac/LambdaTranslationTest2.java
diffstat 2 files changed, 28 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/lang/invoke/InnerClassGenerator.java	Wed Apr 11 22:37:07 2012 -0400
+++ b/src/share/classes/java/lang/invoke/InnerClassGenerator.java	Thu Apr 12 15:19:23 2012 -0400
@@ -43,6 +43,7 @@
     private static final String DESCR_METHOD_WRITE_REPLACE = "()Ljava/lang/Object;";
     private static final Type TYPE_VOID = Type.getType(void.class);
     private static final String NAME_OBJECT = "java/lang/Object";
+    private static final String NAME_MAGIC_ACCESSOR_IMPL = "sun/reflect/MagicAccessorImpl";
     private static final String NAME_SERIALIZABLE = "java/io/Serializable";
     private static final String NAME_SERIALIZED_LAMBDA = "com/oracle/java/lang/invoke/SerializedLambdaImpl";
     private static final String NAME_CTOR = "<init>";
@@ -105,7 +106,7 @@
 
         final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); // @@@ To Do: either dump depth computation, or do it right
 
-        cw.visit(CLASSFILE_VERSION, ACC_PUBLIC + ACC_SUPER, wrapperName, null, NAME_OBJECT,
+        cw.visit(CLASSFILE_VERSION, ACC_PUBLIC + ACC_SUPER, wrapperName, null, NAME_MAGIC_ACCESSOR_IMPL,
                  serializable ? new String[]{samName, NAME_SERIALIZABLE} : new String[]{samName});
 
         Type[] argTypes = Type.getArgumentTypes(constructorDesc);
--- a/test-ng/tests/org/openjdk/tests/javac/LambdaTranslationTest2.java	Wed Apr 11 22:37:07 2012 -0400
+++ b/test-ng/tests/org/openjdk/tests/javac/LambdaTranslationTest2.java	Thu Apr 12 15:19:23 2012 -0400
@@ -3,13 +3,12 @@
 import org.testng.annotations.Test;
 
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.functions.Mapper;
 import java.util.functions.Predicate;
 
-import static org.testng.Assert.*;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
 
 /**
  * LambdaTranslationTest2 -- end-to-end smoke tests for lambda evaluation
@@ -135,6 +134,14 @@
         public String m(byte a0, short a1, char a2, int a3, long a4);
     }
 
+    interface Box {
+        public String m(byte a0, short a1, char a2, int a3, long a4, boolean a5, float a6, double a7);
+    }
+
+    static String pb(Byte a0, Short a1, Character a2, Integer 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);
+    }
+
     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);
     }
@@ -171,27 +178,16 @@
         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));
+        // @@@ TODO: clarify spec on widen+box conversion
     }
 
     interface Unbox {
@@ -207,6 +203,11 @@
         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));
     }
 
+    public void testBoxing() {
+        Box b = LambdaTranslationTest2::pb;
+        assertEquals("b1 s2 cA i4 j5 ztrue f6.000000 d7.000000", b.m((byte) 1, (short) 2, 'A', 4, 5L, true, 6.0f, 7.0));
+    }
+
     static boolean cc(Object o) {
         return ((String) o).equals("foo");
     }
@@ -226,11 +227,20 @@
     }
 
     public void testConstructorRef() {
-        Factory<List<String>> lf = ArrayList::new;
+        Factory<List<String>> lf = ArrayList<String>::new;
         List<String> list = lf.make();
         assertTrue(list instanceof ArrayList);
         assertTrue(list != lf.make());
         list.add("a");
         assertEquals("[a]", list.toString());
     }
+
+    private static String privateMethod() {
+        return "private";
+    }
+
+    public void testPrivateMethodRef() {
+        Factory<String> sf = LambdaTranslationTest2::privateMethod;
+        assertEquals("private", sf.make());
+    }
 }