src/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java

Print this page
rev 7978 : force early generated class initialization from metafactory and join subsequent doPrivelegedActions

@@ -170,29 +170,29 @@
         if (invokedType.parameterCount() == 0) {
             final Constructor[] ctrs = AccessController.doPrivileged(
                     new PrivilegedAction<Constructor[]>() {
                 @Override
                 public Constructor[] run() {
-                    return innerClass.getDeclaredConstructors();
+                    Constructor<?>[] ctrs = innerClass.getDeclaredConstructors();
+                    if (ctrs.length == 1) {
+                        // The lambda implementing inner class constructor is private, set
+                        // it accessible (by us) before creating the constant sole instance
+                        ctrs[0].setAccessible(true);
+                    }
+                    return ctrs;
                 }
             });
             if (ctrs.length != 1) {
                 throw new ReflectiveOperationException("Expected one lambda constructor for "
                         + innerClass.getCanonicalName() + ", got " + ctrs.length);
             }
-            // The lambda implementing inner class constructor is private, set
-            // it accessible (by us) before creating the constant sole instance
-            AccessController.doPrivileged(new PrivilegedAction<Void>() {
-                @Override
-                public Void run() {
-                    ctrs[0].setAccessible(true);
-                    return null;
-                }
-            });
             Object inst = ctrs[0].newInstance();
             return new ConstantCallSite(MethodHandles.constant(samBase, inst));
         } else {
+            if (UNSAFE.shouldBeInitialized(innerClass)) {
+                UNSAFE.ensureClassInitialized(innerClass);
+            }
             return new ConstantCallSite(
                     MethodHandles.Lookup.IMPL_LOOKUP
                          .findConstructor(innerClass, constructorType)
                          .asType(constructorType.changeReturnType(samBase)));
         }