8187826: Avoid using reflection to bootstrap NamedFunctions

Reviewed-by: psandoz
This commit is contained in:
Claes Redestad 2017-09-27 17:56:00 +02:00
parent 2e46c1508a
commit 0355224df8
3 changed files with 48 additions and 36 deletions

View File

@ -28,6 +28,7 @@ package java.lang.invoke;
import java.util.Arrays; import java.util.Arrays;
import static java.lang.invoke.LambdaForm.*; import static java.lang.invoke.LambdaForm.*;
import static java.lang.invoke.LambdaForm.Kind.*; import static java.lang.invoke.LambdaForm.Kind.*;
import static java.lang.invoke.MethodHandleNatives.Constants.REF_invokeVirtual;
import static java.lang.invoke.MethodHandleStatics.*; import static java.lang.invoke.MethodHandleStatics.*;
/** /**
@ -158,8 +159,11 @@ abstract class DelegatingMethodHandle extends MethodHandle {
static final NamedFunction NF_getTarget; static final NamedFunction NF_getTarget;
static { static {
try { try {
NF_getTarget = new NamedFunction(DelegatingMethodHandle.class MemberName member = new MemberName(DelegatingMethodHandle.class, "getTarget",
.getDeclaredMethod("getTarget")); MethodType.methodType(MethodHandle.class), REF_invokeVirtual);
NF_getTarget = new NamedFunction(
MemberName.getFactory()
.resolveOrFail(REF_invokeVirtual, member, DelegatingMethodHandle.class, NoSuchMethodException.class));
} catch (ReflectiveOperationException ex) { } catch (ReflectiveOperationException ex) {
throw newInternalError(ex); throw newInternalError(ex);
} }

View File

@ -753,42 +753,38 @@ class DirectMethodHandle extends MethodHandle {
return nf; return nf;
} }
private static final MethodType OBJ_OBJ_TYPE = MethodType.methodType(Object.class, Object.class);
private static final MethodType LONG_OBJ_TYPE = MethodType.methodType(long.class, Object.class);
private static NamedFunction createFunction(byte func) { private static NamedFunction createFunction(byte func) {
try { try {
switch (func) { switch (func) {
case NF_internalMemberName: case NF_internalMemberName:
return new NamedFunction(DirectMethodHandle.class return getNamedFunction("internalMemberName", OBJ_OBJ_TYPE);
.getDeclaredMethod("internalMemberName", Object.class));
case NF_internalMemberNameEnsureInit: case NF_internalMemberNameEnsureInit:
return new NamedFunction(DirectMethodHandle.class return getNamedFunction("internalMemberNameEnsureInit", OBJ_OBJ_TYPE);
.getDeclaredMethod("internalMemberNameEnsureInit", Object.class));
case NF_ensureInitialized: case NF_ensureInitialized:
return new NamedFunction(DirectMethodHandle.class return getNamedFunction("ensureInitialized", MethodType.methodType(void.class, Object.class));
.getDeclaredMethod("ensureInitialized", Object.class));
case NF_fieldOffset: case NF_fieldOffset:
return new NamedFunction(DirectMethodHandle.class return getNamedFunction("fieldOffset", LONG_OBJ_TYPE);
.getDeclaredMethod("fieldOffset", Object.class));
case NF_checkBase: case NF_checkBase:
return new NamedFunction(DirectMethodHandle.class return getNamedFunction("checkBase", OBJ_OBJ_TYPE);
.getDeclaredMethod("checkBase", Object.class));
case NF_staticBase: case NF_staticBase:
return new NamedFunction(DirectMethodHandle.class return getNamedFunction("staticBase", OBJ_OBJ_TYPE);
.getDeclaredMethod("staticBase", Object.class));
case NF_staticOffset: case NF_staticOffset:
return new NamedFunction(DirectMethodHandle.class return getNamedFunction("staticOffset", LONG_OBJ_TYPE);
.getDeclaredMethod("staticOffset", Object.class));
case NF_checkCast: case NF_checkCast:
return new NamedFunction(DirectMethodHandle.class return getNamedFunction("checkCast", MethodType.methodType(Object.class, Object.class, Object.class));
.getDeclaredMethod("checkCast", Object.class, Object.class));
case NF_allocateInstance: case NF_allocateInstance:
return new NamedFunction(DirectMethodHandle.class return getNamedFunction("allocateInstance", OBJ_OBJ_TYPE);
.getDeclaredMethod("allocateInstance", Object.class));
case NF_constructorMethod: case NF_constructorMethod:
return new NamedFunction(DirectMethodHandle.class return getNamedFunction("constructorMethod", OBJ_OBJ_TYPE);
.getDeclaredMethod("constructorMethod", Object.class));
case NF_UNSAFE: case NF_UNSAFE:
return new NamedFunction(new MemberName(MethodHandleStatics.class MemberName member = new MemberName(MethodHandleStatics.class, "UNSAFE", Unsafe.class, REF_getField);
.getDeclaredField("UNSAFE"))); return new NamedFunction(
MemberName.getFactory()
.resolveOrFail(REF_getField, member, DirectMethodHandle.class, NoSuchMethodException.class));
default: default:
throw newInternalError("Unknown function: " + func); throw newInternalError("Unknown function: " + func);
} }
@ -797,6 +793,15 @@ class DirectMethodHandle extends MethodHandle {
} }
} }
private static NamedFunction getNamedFunction(String name, MethodType type)
throws ReflectiveOperationException
{
MemberName member = new MemberName(DirectMethodHandle.class, name, type, REF_invokeStatic);
return new NamedFunction(
MemberName.getFactory()
.resolveOrFail(REF_invokeStatic, member, DirectMethodHandle.class, NoSuchMethodException.class));
}
static { static {
// The Holder class will contain pre-generated DirectMethodHandles resolved // The Holder class will contain pre-generated DirectMethodHandles resolved
// speculatively using MemberName.getFactory().resolveOrNull. However, that // speculatively using MemberName.getFactory().resolveOrNull. However, that

View File

@ -611,23 +611,17 @@ class Invokers {
try { try {
switch (func) { switch (func) {
case NF_checkExactType: case NF_checkExactType:
return new NamedFunction(Invokers.class return getNamedFunction("checkExactType", MethodType.methodType(void.class, MethodHandle.class, MethodType.class));
.getDeclaredMethod("checkExactType", MethodHandle.class, MethodType.class));
case NF_checkGenericType: case NF_checkGenericType:
return new NamedFunction(Invokers.class return getNamedFunction("checkGenericType", MethodType.methodType(MethodHandle.class, MethodHandle.class, MethodType.class));
.getDeclaredMethod("checkGenericType", MethodHandle.class, MethodType.class));
case NF_getCallSiteTarget: case NF_getCallSiteTarget:
return new NamedFunction(Invokers.class return getNamedFunction("getCallSiteTarget", MethodType.methodType(MethodHandle.class, CallSite.class));
.getDeclaredMethod("getCallSiteTarget", CallSite.class));
case NF_checkCustomized: case NF_checkCustomized:
return new NamedFunction(Invokers.class return getNamedFunction("checkCustomized", MethodType.methodType(void.class, MethodHandle.class));
.getDeclaredMethod("checkCustomized", MethodHandle.class));
case NF_checkVarHandleGenericType: case NF_checkVarHandleGenericType:
return new NamedFunction(Invokers.class return getNamedFunction("checkVarHandleGenericType", MethodType.methodType(MethodHandle.class, VarHandle.class, VarHandle.AccessDescriptor.class));
.getDeclaredMethod("checkVarHandleGenericType", VarHandle.class, VarHandle.AccessDescriptor.class));
case NF_checkVarHandleExactType: case NF_checkVarHandleExactType:
return new NamedFunction(Invokers.class return getNamedFunction("checkVarHandleExactType", MethodType.methodType(MethodHandle.class, VarHandle.class, VarHandle.AccessDescriptor.class));
.getDeclaredMethod("checkVarHandleExactType", VarHandle.class, VarHandle.AccessDescriptor.class));
default: default:
throw newInternalError("Unknown function: " + func); throw newInternalError("Unknown function: " + func);
} }
@ -636,6 +630,15 @@ class Invokers {
} }
} }
private static NamedFunction getNamedFunction(String name, MethodType type)
throws ReflectiveOperationException
{
MemberName member = new MemberName(Invokers.class, name, type, REF_invokeStatic);
return new NamedFunction(
MemberName.getFactory()
.resolveOrFail(REF_invokeStatic, member, Invokers.class, NoSuchMethodException.class));
}
private static class Lazy { private static class Lazy {
private static final MethodHandle MH_asSpreader; private static final MethodHandle MH_asSpreader;