本文整理匯總了Java中java.lang.invoke.MethodHandles.insertArguments方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodHandles.insertArguments方法的具體用法?Java MethodHandles.insertArguments怎麽用?Java MethodHandles.insertArguments使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.invoke.MethodHandles
的用法示例。
在下文中一共展示了MethodHandles.insertArguments方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: relinkComposableInvoker
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
private static void relinkComposableInvoker(final CallSite cs, final CompiledFunction inv, final boolean constructor) {
final HandleAndAssumptions handleAndAssumptions = inv.getValidOptimisticInvocation(new Supplier<MethodHandle>() {
@Override
public MethodHandle get() {
return inv.getInvokerOrConstructor(constructor);
}
});
final MethodHandle handle = handleAndAssumptions.handle;
final SwitchPoint assumptions = handleAndAssumptions.assumptions;
final MethodHandle target;
if(assumptions == null) {
target = handle;
} else {
final MethodHandle relink = MethodHandles.insertArguments(RELINK_COMPOSABLE_INVOKER, 0, cs, inv, constructor);
target = assumptions.guardWithTest(handle, MethodHandles.foldArguments(cs.dynamicInvoker(), relink));
}
cs.setTarget(target.asType(cs.type()));
}
示例2: testInsertArguments
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
void testInsertArguments(int nargs, int pos, int ins) throws Throwable {
countTest();
MethodHandle target = varargsArray(nargs + ins);
Object[] args = randomArgs(target.type().parameterArray());
List<Object> resList = Arrays.asList(args);
List<Object> argsToPass = new ArrayList<>(resList);
List<Object> argsToInsert = argsToPass.subList(pos, pos + ins);
if (verbosity >= 3)
System.out.println("insert: "+argsToInsert+" @"+pos+" into "+target);
@SuppressWarnings("cast") // cast to spread Object... is helpful
MethodHandle target2 = MethodHandles.insertArguments(target, pos,
(Object[]/*...*/) argsToInsert.toArray());
argsToInsert.clear(); // remove from argsToInsert
Object res2 = target2.invokeWithArguments(argsToPass);
Object res2List = Arrays.asList((Object[])res2);
if (verbosity >= 3)
System.out.println("result: "+res2List);
//if (!resList.equals(res2List))
// System.out.println("*** fail at n/p/i = "+nargs+"/"+pos+"/"+ins+": "+resList+" => "+res2List);
assertEquals(resList, res2List);
}
示例3: createRelinkAndInvokeMethod
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
private MethodHandle createRelinkAndInvokeMethod(final RelinkableCallSite callSite, final int relinkCount) {
// Make a bound MH of invoke() for this linker and call site
final MethodHandle boundRelinker = MethodHandles.insertArguments(RELINK, 0, this, callSite, Integer.valueOf(
relinkCount));
// Make a MH that gathers all arguments to the invocation into an Object[]
final MethodType type = callSite.getDescriptor().getMethodType();
final MethodHandle collectingRelinker = boundRelinker.asCollector(Object[].class, type.parameterCount());
return MethodHandles.foldArguments(MethodHandles.exactInvoker(type), collectingRelinker.asType(
type.changeReturnType(MethodHandle.class)));
}
示例4: unbox
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
private static MethodHandle unbox(Wrapper wrap, int kind) {
// kind 0 -> strongly typed with NPE
// kind 1 -> strongly typed but zero for null,
// kind 2 -> asType rules: accept multiple box types but only widening conversions with NPE
// kind 3 -> explicitCastArguments rules: allow narrowing conversions, zero for null
WrapperCache cache = UNBOX_CONVERSIONS[kind];
MethodHandle mh = cache.get(wrap);
if (mh != null) {
return mh;
}
// slow path
switch (wrap) {
case OBJECT:
case VOID:
throw new IllegalArgumentException("unbox "+wrap);
}
// look up the method
String name = "unbox" + wrap.wrapperSimpleName();
MethodType type = unboxType(wrap, kind);
try {
mh = IMPL_LOOKUP.findStatic(THIS_CLASS, name, type);
} catch (ReflectiveOperationException ex) {
mh = null;
}
if (mh != null) {
if (kind > 0) {
boolean cast = (kind != 2);
mh = MethodHandles.insertArguments(mh, 1, cast);
}
if (kind == 1) { // casting but exact (null -> zero)
mh = mh.asType(unboxType(wrap, 0));
}
return cache.put(wrap, mh);
}
throw new IllegalArgumentException("cannot find unbox adapter for " + wrap
+ (kind <= 1 ? " (exact)" : kind == 3 ? " (cast)" : ""));
}
示例5: getGuardedInvocation
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) {
final Object receiver = linkRequest.getReceiver();
if(!(receiver instanceof DynamicMethod)) {
return null;
}
final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
if(desc.getNameTokenCount() != 2 && desc.getNameToken(CallSiteDescriptor.SCHEME) != "dyn") {
return null;
}
final String operator = desc.getNameToken(CallSiteDescriptor.OPERATOR);
final DynamicMethod dynMethod = (DynamicMethod)receiver;
final boolean constructor = dynMethod.isConstructor();
final MethodHandle invocation;
if (operator == "call" && !constructor) {
invocation = dynMethod.getInvocation(
CallSiteDescriptorFactory.dropParameterTypes(desc, 0, 1), linkerServices);
} else if (operator == "new" && constructor) {
final MethodHandle ctorInvocation = dynMethod.getInvocation(desc, linkerServices);
if(ctorInvocation == null) {
return null;
}
// Insert null for StaticClass parameter
invocation = MethodHandles.insertArguments(ctorInvocation, 0, (Object)null);
} else {
return null;
}
if (invocation != null) {
return new GuardedInvocation(MethodHandles.dropArguments(invocation, 0,
desc.getMethodType().parameterType(0)), Guards.getIdentityGuard(receiver));
}
return null;
}
示例6: makePruneAndInvokeMethod
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
/**
* Creates a method that rebuilds our call chain, pruning it of any invalidated switchpoints, and then invokes that
* chain.
* @param relink the ultimate fallback for the chain (the {@code DynamicLinker}'s relink).
* @return a method handle for prune-and-invoke
*/
private MethodHandle makePruneAndInvokeMethod(final MethodHandle relink, final MethodHandle prune) {
// Bind prune to (this, relink)
final MethodHandle boundPrune = MethodHandles.insertArguments(prune, 0, this, relink);
// Make it ignore all incoming arguments
final MethodHandle ignoreArgsPrune = MethodHandles.dropArguments(boundPrune, 0, type().parameterList());
// Invoke prune, then invoke the call site target with original arguments
return MethodHandles.foldArguments(MethodHandles.exactInvoker(type()), ignoreArgsPrune);
}
示例7: makePruneAndInvokeMethod
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
/**
* Creates a method that rebuilds our call chain, pruning it of any invalidated switchpoints, and then invokes that
* chain.
* @param relinkAndInvoke the ultimate fallback for the chain passed from the dynamic linker.
* @return a method handle for prune-and-invoke
*/
private MethodHandle makePruneAndInvokeMethod(final MethodHandle relinkAndInvoke, final MethodHandle prune) {
// Bind prune to (this, relink)
final MethodHandle boundPrune = MethodHandles.insertArguments(prune, 0, this, relinkAndInvoke);
// Make it ignore all incoming arguments
final MethodHandle ignoreArgsPrune = MethodHandles.dropArguments(boundPrune, 0, type().parameterList());
// Invoke prune, then invoke the call site target with original arguments
return MethodHandles.foldArguments(MethodHandles.exactInvoker(type()), ignoreArgsPrune);
}
示例8: snip
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
static MethodHandle snip(MethodHandle mh, int argPos) {
if (argPos < 0) return null; // special case for optional args
Class<?> argType = mh.type().parameterType(argPos);
Object zero;
try {
zero = MethodHandles.zero(argType).invoke();
} catch (Throwable ex) {
throw new AssertionError(ex);
}
return MethodHandles.insertArguments(mh, argPos, zero);
}
示例9: getGuardedInvocation
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) {
final Object receiver = linkRequest.getReceiver();
if(!(receiver instanceof DynamicMethod)) {
return null;
}
final DynamicMethod dynMethod = (DynamicMethod)receiver;
final boolean constructor = dynMethod.isConstructor();
final MethodHandle invocation;
final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
final Operation op = NamedOperation.getBaseOperation(desc.getOperation());
if (op == StandardOperation.CALL && !constructor) {
invocation = dynMethod.getInvocation(desc.changeMethodType(
desc.getMethodType().dropParameterTypes(0, 1)), linkerServices);
} else if (op == StandardOperation.NEW && constructor) {
final MethodHandle ctorInvocation = dynMethod.getInvocation(desc, linkerServices);
if(ctorInvocation == null) {
return null;
}
// Insert null for StaticClass parameter
invocation = MethodHandles.insertArguments(ctorInvocation, 0, (Object)null);
} else {
return null;
}
if (invocation != null) {
return new GuardedInvocation(MethodHandles.dropArguments(invocation, 0,
desc.getMethodType().parameterType(0)), Guards.getIdentityGuard(receiver));
}
return null;
}
示例10: insertArguments
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Override
public MethodHandle insertArguments(final MethodHandle target, final int pos, final Object... values) {
final MethodHandle mh = MethodHandles.insertArguments(target, pos, values);
return debug(mh, "insertArguments", target, pos, values);
}
示例11: getPropertySetter
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
private GuardedInvocationComponent getPropertySetter(final CallSiteDescriptor callSiteDescriptor,
final LinkerServices linkerServices, final List<String> operations) throws Exception {
switch(callSiteDescriptor.getNameTokenCount()) {
case 2: {
// Must have three arguments: target object, property name, and property value.
assertParameterCount(callSiteDescriptor, 3);
// We want setters that conform to "Object(O, V)". Note, we aren't doing "R(O, V)" as it might not be
// valid for us to convert return values proactively. Also, since we don't know what setters will be
// invoked, we'll conservatively presume Object return type. The one exception is void return.
final MethodType origType = callSiteDescriptor.getMethodType();
final MethodType type = origType.returnType() == void.class ? origType : origType.changeReturnType(Object.class);
// What's below is basically:
// foldArguments(guardWithTest(isNotNull, invoke, null|nextComponent.invocation),
// get_setter_handle(type, linkerServices))
// only with a bunch of method signature adjustments. Basically, retrieve method setter
// MethodHandle; if it is non-null, invoke it, otherwise either return null, or delegate to next
// component's invocation.
// Call site type is "ret_type(object_type,property_name_type,property_value_type)", which we'll
// abbreviate to R(O, N, V) going forward, although we don't really use R here (see above about using
// Object return type).
final MethodType setterType = type.dropParameterTypes(1, 2);
// Bind property setter handle to the expected setter type and linker services. Type is
// MethodHandle(Object, String, Object)
final MethodHandle boundGetter = MethodHandles.insertArguments(getPropertySetterHandle, 0,
callSiteDescriptor.changeMethodType(setterType), linkerServices);
// Cast getter to MethodHandle(O, N, V)
final MethodHandle typedGetter = linkerServices.asType(boundGetter, type.changeReturnType(
MethodHandle.class));
// Handle to invoke the setter R(MethodHandle, O, V)
final MethodHandle invokeHandle = MethodHandles.exactInvoker(setterType);
// Handle to invoke the setter, dropping unnecessary fold arguments R(MethodHandle, O, N, V)
final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandle, 2, type.parameterType(
1));
final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
linkerServices, operations);
final MethodHandle fallbackFolded;
if(nextComponent == null) {
// Object(MethodHandle)->Object(MethodHandle, O, N, V); returns constant null
fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_METHOD_HANDLE, 1,
type.parameterList()).asType(type.insertParameterTypes(0, MethodHandle.class));
} else {
// Object(O, N, V)->Object(MethodHandle, O, N, V); adapts the next component's invocation to drop the
// extra argument resulting from fold
fallbackFolded = MethodHandles.dropArguments(nextComponent.getGuardedInvocation().getInvocation(),
0, MethodHandle.class);
}
// fold(R(MethodHandle, O, N, V), MethodHandle(O, N, V))
final MethodHandle compositeSetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(
IS_METHOD_HANDLE_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter);
if(nextComponent == null) {
return getClassGuardedInvocationComponent(compositeSetter, type);
}
return nextComponent.compose(compositeSetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
}
case 3: {
// Must have two arguments: target object and property value
assertParameterCount(callSiteDescriptor, 2);
final GuardedInvocation gi = createGuardedDynamicMethodInvocation(callSiteDescriptor, linkerServices,
callSiteDescriptor.getNameToken(CallSiteDescriptor.NAME_OPERAND), propertySetters);
// If we have a property setter with this name, this composite operation will always stop here
if(gi != null) {
return new GuardedInvocationComponent(gi, clazz, ValidationType.EXACT_CLASS);
}
// If we don't have a property setter with this name, always fall back to the next operation in the
// composite (if any)
return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, operations);
}
default: {
// More than two name components; don't know what to do with it.
return null;
}
}
}
示例12: bindToFixedKey
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
private MethodHandle bindToFixedKey(final MethodHandle handle) {
return fixedKey == null ? handle : MethodHandles.insertArguments(handle, 1, fixedKey);
}
示例13: runTest
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
private void runTest() {
if (Helper.IS_VERBOSE) {
System.out.printf("CatchException(%s, isVararg=%b argsCount=%d "
+ "dropped=%d)%n",
testCase, thrower.isVarargsCollector(),
argsCount, dropped);
}
Helper.clear();
Object[] args = Helper.randomArgs(
argsCount, thrower.type().parameterArray());
Object arg0 = Helper.MISSING_ARG;
Object arg1 = testCase.thrown;
if (argsCount > 0) {
arg0 = args[0];
}
if (argsCount > 1) {
args[1] = arg1;
}
Asserts.assertEQ(nargs, thrower.type().parameterCount());
if (argsCount < nargs) {
Object[] appendArgs = {arg0, arg1};
appendArgs = Arrays.copyOfRange(appendArgs, argsCount, nargs);
thrower = MethodHandles.insertArguments(
thrower, argsCount, appendArgs);
}
Asserts.assertEQ(argsCount, thrower.type().parameterCount());
MethodHandle target = MethodHandles.catchException(
testCase.filter(thrower), testCase.throwableClass,
testCase.filter(catcher));
Asserts.assertEQ(thrower.type(), target.type());
Asserts.assertEQ(argsCount, target.type().parameterCount());
Object returned;
try {
returned = target.invokeWithArguments(args);
} catch (Throwable ex) {
if (CodeCacheOverflowProcessor.isThrowableCausedByVME(ex)) {
// This error will be treated by CodeCacheOverflowProcessor
// to prevent the test from failing because of code cache overflow.
throw new Error(ex);
}
testCase.assertCatch(ex);
returned = ex;
}
testCase.assertReturn(returned, arg0, arg1, dropped, args);
}
示例14: testGuardWithTest
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
void testGuardWithTest(int nargs, int testDrops, Class<?> argClass) throws Throwable {
countTest();
int nargs1 = Math.min(3, nargs);
MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.methodType(boolean.class, Object.class));
MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.genericMethodType(nargs1));
MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.genericMethodType(nargs1));
while (test.type().parameterCount() > nargs)
// 0: test = constant(MISSING_ARG.equals(MISSING_ARG))
// 1: test = lambda (_) MISSING_ARG.equals(_)
test = MethodHandles.insertArguments(test, 0, MISSING_ARG);
if (argClass != Object.class) {
test = changeArgTypes(test, argClass);
target = changeArgTypes(target, argClass);
fallback = changeArgTypes(fallback, argClass);
}
int testArgs = nargs - testDrops;
assert(testArgs >= 0);
test = addTrailingArgs(test, Math.min(testArgs, nargs), argClass);
target = addTrailingArgs(target, nargs, argClass);
fallback = addTrailingArgs(fallback, nargs, argClass);
Object[][] argLists = {
{ },
{ "foo" }, { MISSING_ARG },
{ "foo", "foo" }, { "foo", "bar" },
{ "foo", "foo", "baz" }, { "foo", "bar", "baz" }
};
for (Object[] argList : argLists) {
Object[] argList1 = argList;
if (argList.length != nargs) {
if (argList.length != nargs1) continue;
argList1 = Arrays.copyOf(argList, nargs);
Arrays.fill(argList1, nargs1, nargs, MISSING_ARG_2);
}
MethodHandle test1 = test;
if (test1.type().parameterCount() > testArgs) {
int pc = test1.type().parameterCount();
test1 = MethodHandles.insertArguments(test, testArgs, Arrays.copyOfRange(argList1, testArgs, pc));
}
MethodHandle mh = MethodHandles.guardWithTest(test1, target, fallback);
assertEquals(target.type(), mh.type());
boolean equals;
switch (nargs) {
case 0: equals = true; break;
case 1: equals = MISSING_ARG.equals(argList[0]); break;
default: equals = argList[0].equals(argList[1]); break;
}
String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals");
if (verbosity >= 3)
System.out.println(logEntry(willCall, argList));
Object result = mh.invokeWithArguments(argList1);
assertCalled(willCall, argList);
}
}
示例15: bindOperand
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
private static MethodHandle bindOperand(final MethodHandle handle, final String operand) {
return operand == null ? handle : MethodHandles.insertArguments(handle, 1, operand);
}