8232806: Introduce a system property to disable eager lambda initialization
Reviewed-by: briangoetz, mr, psandoz, forax
This commit is contained in:
parent
4fcd28bf54
commit
29f2d74173
@ -29,6 +29,7 @@ import jdk.internal.org.objectweb.asm.*;
|
|||||||
import sun.invoke.util.BytecodeDescriptor;
|
import sun.invoke.util.BytecodeDescriptor;
|
||||||
import jdk.internal.misc.Unsafe;
|
import jdk.internal.misc.Unsafe;
|
||||||
import sun.security.action.GetPropertyAction;
|
import sun.security.action.GetPropertyAction;
|
||||||
|
import sun.security.action.GetBooleanAction;
|
||||||
|
|
||||||
import java.io.FilePermission;
|
import java.io.FilePermission;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@ -87,10 +88,15 @@ import static jdk.internal.org.objectweb.asm.Opcodes.*;
|
|||||||
// For dumping generated classes to disk, for debugging purposes
|
// For dumping generated classes to disk, for debugging purposes
|
||||||
private static final ProxyClassesDumper dumper;
|
private static final ProxyClassesDumper dumper;
|
||||||
|
|
||||||
|
private static final boolean disableEagerInitialization;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
final String key = "jdk.internal.lambda.dumpProxyClasses";
|
final String dumpProxyClassesKey = "jdk.internal.lambda.dumpProxyClasses";
|
||||||
String path = GetPropertyAction.privilegedGetProperty(key);
|
String dumpPath = GetPropertyAction.privilegedGetProperty(dumpProxyClassesKey);
|
||||||
dumper = (null == path) ? null : ProxyClassesDumper.getInstance(path);
|
dumper = (null == dumpPath) ? null : ProxyClassesDumper.getInstance(dumpPath);
|
||||||
|
|
||||||
|
final String disableEagerInitializationKey = "jdk.internal.lambda.disableEagerInitialization";
|
||||||
|
disableEagerInitialization = GetBooleanAction.privilegedGetProperty(disableEagerInitializationKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
// See context values in AbstractValidatingLambdaMetafactory
|
// See context values in AbstractValidatingLambdaMetafactory
|
||||||
@ -187,7 +193,9 @@ import static jdk.internal.org.objectweb.asm.Opcodes.*;
|
|||||||
@Override
|
@Override
|
||||||
CallSite buildCallSite() throws LambdaConversionException {
|
CallSite buildCallSite() throws LambdaConversionException {
|
||||||
final Class<?> innerClass = spinInnerClass();
|
final Class<?> innerClass = spinInnerClass();
|
||||||
if (invokedType.parameterCount() == 0) {
|
if (invokedType.parameterCount() == 0 && !disableEagerInitialization) {
|
||||||
|
// In the case of a non-capturing lambda, we optimize linkage by pre-computing a single instance,
|
||||||
|
// unless we've suppressed eager initialization
|
||||||
final Constructor<?>[] ctrs = AccessController.doPrivileged(
|
final Constructor<?>[] ctrs = AccessController.doPrivileged(
|
||||||
new PrivilegedAction<>() {
|
new PrivilegedAction<>() {
|
||||||
@Override
|
@Override
|
||||||
@ -215,7 +223,9 @@ import static jdk.internal.org.objectweb.asm.Opcodes.*;
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
UNSAFE.ensureClassInitialized(innerClass);
|
if (!disableEagerInitialization) {
|
||||||
|
UNSAFE.ensureClassInitialized(innerClass);
|
||||||
|
}
|
||||||
return new ConstantCallSite(
|
return new ConstantCallSite(
|
||||||
MethodHandles.Lookup.IMPL_LOOKUP
|
MethodHandles.Lookup.IMPL_LOOKUP
|
||||||
.findStatic(innerClass, NAME_FACTORY, invokedType));
|
.findStatic(innerClass, NAME_FACTORY, invokedType));
|
||||||
@ -273,7 +283,7 @@ import static jdk.internal.org.objectweb.asm.Opcodes.*;
|
|||||||
|
|
||||||
generateConstructor();
|
generateConstructor();
|
||||||
|
|
||||||
if (invokedType.parameterCount() != 0) {
|
if (invokedType.parameterCount() != 0 || disableEagerInitialization) {
|
||||||
generateFactory();
|
generateFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,8 +26,11 @@
|
|||||||
* @bug 8003280
|
* @bug 8003280
|
||||||
* @summary Add lambda tests
|
* @summary Add lambda tests
|
||||||
* Test bridge methods for certain SAM conversions
|
* Test bridge methods for certain SAM conversions
|
||||||
|
* Tests that jdk.internal.lambda.disableEagerInitialization=true creates a
|
||||||
|
* get$Lambda method for non-capturing lambdas
|
||||||
* @compile LambdaTest6.java
|
* @compile LambdaTest6.java
|
||||||
* @run main LambdaTest6
|
* @run main LambdaTest6
|
||||||
|
* @run main/othervm -Djdk.internal.lambda.disableEagerInitialization=true LambdaTest6
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@ -60,18 +63,37 @@ public class LambdaTest6<T> {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Set<String> allowedMethods() {
|
||||||
|
Set<String> s = new HashSet<>();
|
||||||
|
s.add("m");
|
||||||
|
if (Boolean.getBoolean("jdk.internal.lambda.disableEagerInitialization")) {
|
||||||
|
s.add("get$Lambda");
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean matchingMethodNames(Method[] methods) {
|
||||||
|
Set<String> methodNames = new HashSet<>();
|
||||||
|
for (Method m : methods) {
|
||||||
|
methodNames.add(m.getName());
|
||||||
|
}
|
||||||
|
return methodNames.equals(allowedMethods());
|
||||||
|
}
|
||||||
|
|
||||||
private void test1()
|
private void test1()
|
||||||
{
|
{
|
||||||
L la = s -> { };
|
L la = s -> { };
|
||||||
la.m("hi");
|
la.m("hi");
|
||||||
Class<? extends L> c1 = la.getClass();
|
Class<? extends L> c1 = la.getClass();
|
||||||
Method[] methods = c1.getDeclaredMethods();
|
Method[] methods = c1.getDeclaredMethods();
|
||||||
|
assertTrue(matchingMethodNames(methods));
|
||||||
Set<String> types = setOfStringObject();
|
Set<String> types = setOfStringObject();
|
||||||
for(Method m : methods) {
|
for(Method m : methods) {
|
||||||
assertTrue(m.getName().equals("m"));
|
if ("m".equals(m.getName())) {
|
||||||
Class[] parameterTypes = m.getParameterTypes();
|
Class[] parameterTypes = m.getParameterTypes();
|
||||||
assertTrue(parameterTypes.length == 1);
|
assertTrue(parameterTypes.length == 1);
|
||||||
assertTrue(types.remove(parameterTypes[0].getName()));
|
assertTrue(types.remove(parameterTypes[0].getName()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String")));
|
assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String")));
|
||||||
}
|
}
|
||||||
@ -82,12 +104,14 @@ public class LambdaTest6<T> {
|
|||||||
//km.m("hi");
|
//km.m("hi");
|
||||||
Class<? extends KM> c2 = km.getClass();
|
Class<? extends KM> c2 = km.getClass();
|
||||||
Method[] methods = c2.getDeclaredMethods();
|
Method[] methods = c2.getDeclaredMethods();
|
||||||
|
assertTrue(matchingMethodNames(methods));
|
||||||
Set<String> types = setOfStringObject();
|
Set<String> types = setOfStringObject();
|
||||||
for(Method m : methods) {
|
for(Method m : methods) {
|
||||||
assertTrue(m.getName().equals("m"));
|
if ("m".equals(m.getName())) {
|
||||||
Class[] parameterTypes = m.getParameterTypes();
|
Class[] parameterTypes = m.getParameterTypes();
|
||||||
assertTrue(parameterTypes.length == 1);
|
assertTrue(parameterTypes.length == 1);
|
||||||
assertTrue(types.remove(parameterTypes[0].getName()));
|
assertTrue(types.remove(parameterTypes[0].getName()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
assertTrue(types.isEmpty());
|
assertTrue(types.isEmpty());
|
||||||
}
|
}
|
||||||
@ -99,11 +123,13 @@ public class LambdaTest6<T> {
|
|||||||
assertTrue( ((H)na).m().equals("hi") );
|
assertTrue( ((H)na).m().equals("hi") );
|
||||||
Class<? extends N> c3 = na.getClass();
|
Class<? extends N> c3 = na.getClass();
|
||||||
Method[] methods = c3.getDeclaredMethods();
|
Method[] methods = c3.getDeclaredMethods();
|
||||||
|
assertTrue(matchingMethodNames(methods));
|
||||||
Set<String> types = setOfStringObject();
|
Set<String> types = setOfStringObject();
|
||||||
for(Method m : methods) {
|
for(Method m : methods) {
|
||||||
assertTrue(m.getName().equals("m"));
|
if ("m".equals(m.getName())) {
|
||||||
Class returnType = m.getReturnType();
|
Class returnType = m.getReturnType();
|
||||||
assertTrue(types.remove(returnType.getName()));
|
assertTrue(types.remove(returnType.getName()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
assertTrue(types.size() == 1); //there's a bridge
|
assertTrue(types.size() == 1); //there's a bridge
|
||||||
}
|
}
|
||||||
|
@ -26,11 +26,15 @@
|
|||||||
* @bug 8003280
|
* @bug 8003280
|
||||||
* @summary Add lambda tests
|
* @summary Add lambda tests
|
||||||
* Test bridge methods in certain SAM conversion
|
* Test bridge methods in certain SAM conversion
|
||||||
|
* Tests that jdk.internal.lambda.disableEagerInitialization=true creates a
|
||||||
|
* get$Lambda method for non-capturing lambdas
|
||||||
* @compile BridgeMethod.java
|
* @compile BridgeMethod.java
|
||||||
* @run main BridgeMethod
|
* @run main BridgeMethod
|
||||||
|
* @run main/othervm -Djdk.internal.lambda.disableEagerInitialization=true BridgeMethod
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@ -68,19 +72,38 @@ public class BridgeMethod {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Set<String> allowedMethods() {
|
||||||
|
Set<String> s = new HashSet<>();
|
||||||
|
s.add("m");
|
||||||
|
if (Boolean.getBoolean("jdk.internal.lambda.disableEagerInitialization")) {
|
||||||
|
s.add("get$Lambda");
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean matchingMethodNames(Method[] methods) {
|
||||||
|
Set<String> methodNames = new HashSet<>();
|
||||||
|
for (Method m : methods) {
|
||||||
|
methodNames.add(m.getName());
|
||||||
|
}
|
||||||
|
return methodNames.equals(allowedMethods());
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
L la = BridgeMethod::bar; //static reference
|
L la = BridgeMethod::bar; //static reference
|
||||||
la.m("hi");
|
la.m("hi");
|
||||||
Class<? extends L> c1 = la.getClass();
|
Class<? extends L> c1 = la.getClass();
|
||||||
Method[] methods = c1.getDeclaredMethods();
|
Method[] methods = c1.getDeclaredMethods();
|
||||||
|
assertTrue(matchingMethodNames(methods));
|
||||||
Set<String> types = setOfStringObject();
|
Set<String> types = setOfStringObject();
|
||||||
System.out.println("methods in SAM conversion of L:");
|
System.out.println("methods in SAM conversion of L:");
|
||||||
for(Method m : methods) {
|
for(Method m : methods) {
|
||||||
System.out.println(m.toGenericString());
|
if (m.getName().equals("m")) {
|
||||||
assertTrue(m.getName().equals("m"));
|
System.out.println(m.toGenericString());
|
||||||
Class[] parameterTypes = m.getParameterTypes();
|
Class[] parameterTypes = m.getParameterTypes();
|
||||||
assertTrue(parameterTypes.length == 1);
|
assertTrue(parameterTypes.length == 1);
|
||||||
assertTrue(types.remove(parameterTypes[0].getName()));
|
assertTrue(types.remove(parameterTypes[0].getName()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String")));
|
assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String")));
|
||||||
|
|
||||||
@ -88,14 +111,16 @@ public class BridgeMethod {
|
|||||||
//km.m("hi"); //will be uncommented when CR7028808 fixed
|
//km.m("hi"); //will be uncommented when CR7028808 fixed
|
||||||
Class<? extends KM> c2 = km.getClass();
|
Class<? extends KM> c2 = km.getClass();
|
||||||
methods = c2.getDeclaredMethods();
|
methods = c2.getDeclaredMethods();
|
||||||
|
assertTrue(matchingMethodNames(methods));
|
||||||
types = setOfStringObject();
|
types = setOfStringObject();
|
||||||
System.out.println("methods in SAM conversion of KM:");
|
System.out.println("methods in SAM conversion of KM:");
|
||||||
for(Method m : methods) {
|
for(Method m : methods) {
|
||||||
System.out.println(m.toGenericString());
|
if (m.getName().equals("m")) {
|
||||||
assertTrue(m.getName().equals("m"));
|
System.out.println(m.toGenericString());
|
||||||
Class<?>[] parameterTypes = m.getParameterTypes();
|
Class<?>[] parameterTypes = m.getParameterTypes();
|
||||||
assertTrue(parameterTypes.length == 1);
|
assertTrue(parameterTypes.length == 1);
|
||||||
assertTrue(types.remove(parameterTypes[0].getName()));
|
assertTrue(types.remove(parameterTypes[0].getName()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
assertTrue(types.isEmpty());
|
assertTrue(types.isEmpty());
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user