8198831: Lazy initialization of ValueConversions MethodHandles

Reviewed-by: shade
This commit is contained in:
Claes Redestad 2018-02-28 15:28:46 +01:00
parent 9093953813
commit 16a61139e2

View File

@ -377,7 +377,7 @@ public class ValueConversions {
MethodType type = MethodType.methodType(wrap.primitiveType()); MethodType type = MethodType.methodType(wrap.primitiveType());
switch (wrap) { switch (wrap) {
case VOID: case VOID:
mh = EMPTY; mh = Handles.EMPTY;
break; break;
case OBJECT: case OBJECT:
case INT: case LONG: case FLOAT: case DOUBLE: case INT: case LONG: case FLOAT: case DOUBLE:
@ -400,26 +400,28 @@ public class ValueConversions {
throw new IllegalArgumentException("cannot find zero constant for " + wrap); throw new IllegalArgumentException("cannot find zero constant for " + wrap);
} }
private static final MethodHandle CAST_REFERENCE, IGNORE, EMPTY; private static class Handles {
static { static final MethodHandle CAST_REFERENCE, IGNORE, EMPTY;
try { static {
MethodType idType = MethodType.genericMethodType(1); try {
MethodType ignoreType = idType.changeReturnType(void.class); MethodType idType = MethodType.genericMethodType(1);
CAST_REFERENCE = IMPL_LOOKUP.findVirtual(Class.class, "cast", idType); MethodType ignoreType = idType.changeReturnType(void.class);
IGNORE = IMPL_LOOKUP.findStatic(THIS_CLASS, "ignore", ignoreType); CAST_REFERENCE = IMPL_LOOKUP.findVirtual(Class.class, "cast", idType);
EMPTY = IMPL_LOOKUP.findStatic(THIS_CLASS, "empty", ignoreType.dropParameterTypes(0, 1)); IGNORE = IMPL_LOOKUP.findStatic(THIS_CLASS, "ignore", ignoreType);
} catch (NoSuchMethodException | IllegalAccessException ex) { EMPTY = IMPL_LOOKUP.findStatic(THIS_CLASS, "empty", ignoreType.dropParameterTypes(0, 1));
throw newInternalError("uncaught exception", ex); } catch (NoSuchMethodException | IllegalAccessException ex) {
throw newInternalError("uncaught exception", ex);
}
} }
} }
public static MethodHandle ignore() { public static MethodHandle ignore() {
return IGNORE; return Handles.IGNORE;
} }
/** Return a method that casts its second argument (an Object) to the given type (a Class). */ /** Return a method that casts its second argument (an Object) to the given type (a Class). */
public static MethodHandle cast() { public static MethodHandle cast() {
return CAST_REFERENCE; return Handles.CAST_REFERENCE;
} }
/// Primitive conversions. /// Primitive conversions.