本文整理汇总了Java中java.lang.invoke.MethodType.changeParameterType方法的典型用法代码示例。如果您正苦于以下问题:Java MethodType.changeParameterType方法的具体用法?Java MethodType.changeParameterType怎么用?Java MethodType.changeParameterType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodType
的用法示例。
在下文中一共展示了MethodType.changeParameterType方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testInvokers0
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
public void testInvokers0() throws Throwable {
if (CAN_SKIP_WORKING) return;
startTest("exactInvoker, genericInvoker, varargsInvoker, dynamicInvoker");
// exactInvoker, genericInvoker, varargsInvoker[0..N], dynamicInvoker
Set<MethodType> done = new HashSet<>();
for (int i = 0; i <= 6; i++) {
if (CAN_TEST_LIGHTLY && i > 3) break;
MethodType gtype = MethodType.genericMethodType(i);
for (Class<?> argType : new Class<?>[]{Object.class, Integer.class, int.class}) {
for (int j = -1; j < i; j++) {
MethodType type = gtype;
if (j < 0)
type = type.changeReturnType(argType);
else if (argType == void.class)
continue;
else
type = type.changeParameterType(j, argType);
if (done.add(type))
testInvokersWithCatch(type);
MethodType vtype = type.changeReturnType(void.class);
if (done.add(vtype))
testInvokersWithCatch(vtype);
}
}
}
}
示例2: testIterateLength
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
@Test(dataProvider = "iteratorInits")
public static void testIterateLength(MethodHandle iterator) throws Throwable {
MethodHandle body = Iterate.MH_lengthStep;
MethodHandle init = Iterate.MH_lengthInit;
MethodType expectedType = Iterate.MT_length;
int barity = body.type().parameterCount();
Class<?> iteratorSource = iterator == null ? null : iterator.type().parameterType(0);
if (iterator != null && iteratorSource != body.type().parameterType(barity-1)) {
// adjust body to accept the other type
body = body.asType(body.type().changeParameterType(barity-1, iteratorSource));
init = init.asType(init.type().changeParameterType(0, iteratorSource));
expectedType = expectedType.changeParameterType(0, iteratorSource);
}
for (;; init = snip(init)) {
System.out.println("testIterateLength.init = "+init);
MethodHandle loop = MethodHandles.iteratedLoop(iterator, init, body);
assertEquals(expectedType, loop.type());
List<Double> list = Arrays.asList(23.0, 148.0, 42.0);
assertEquals(list.size(), (int) loop.invoke(list));
if (init == null) break;
}
}
示例3: testIterateMap
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
@Test(dataProvider = "iteratorInits")
public static void testIterateMap(MethodHandle iterator) throws Throwable {
MethodHandle body = Iterate.MH_mapStep;
MethodHandle init = Iterate.MH_mapInit;
MethodType expectedType = Iterate.MT_map;
int barity = body.type().parameterCount();
Class<?> iteratorSource = iterator == null ? null : iterator.type().parameterType(0);
if (iterator != null && iteratorSource != body.type().parameterType(barity-1)) {
// adjust body to accept the other type
body = body.asType(body.type().changeParameterType(barity-1, iteratorSource));
init = init.asType(init.type().changeParameterType(0, iteratorSource));
expectedType = expectedType.changeParameterType(0, iteratorSource);
}
for (; init != null; init = snip(init)) {
System.out.println("testIterateMap.init = "+init);
MethodHandle loop = MethodHandles.iteratedLoop(iterator, init, body);
assertEquals(expectedType, loop.type());
List<String> list = Arrays.asList("Hello", "world", "!");
List<String> upList = Arrays.asList("HELLO", "WORLD", "!");
assertEquals(upList, (List<String>) loop.invoke(list));
}
}
示例4: makeGenericType
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
private static MethodType makeGenericType(final MethodType type) {
MethodType newType = type.generic();
if (isVarArg(type)) {
newType = newType.changeParameterType(type.parameterCount() - 1, Object[].class);
}
if (needsCallee(type)) {
newType = newType.changeParameterType(0, ScriptFunction.class);
}
return newType;
}
示例5: tweak
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
static MethodHandle tweak(MethodHandle mh, int argPos, Class<?> type) {
MethodType mt = mh.type();
if (argPos == -1)
mt = mt.changeReturnType(type);
else
mt = mt.changeParameterType(argPos, type);
return MethodHandles.explicitCastArguments(mh, mt);
}
示例6: testIterateReverse
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
@Test(dataProvider = "iteratorInits")
public static void testIterateReverse(MethodHandle iterator) throws Throwable {
// this test uses List as its loop state type; don't try to change that
if (iterator != null)
iterator = iterator.asType(iterator.type().changeParameterType(0, List.class));
for (int i = 0; i < 4; i++) {
MethodHandle init = Iterate.MH_reverseInit, body = Iterate.MH_reverseStep;
boolean snipInit = (i & 1) != 0, snipBody = (i & 2) != 0;
if (snipInit) init = snip(init);
if (snipBody) body = snip(body);
if (!snipInit && snipBody && iterator == null) {
// Body does not determine (A...), so the default guy just picks Iterable.
// If body insisted on (List), the default guy would adjust himself.
// Init has no authority to change the (A...), so must patch init.
// All according to plan!
init = slap(snip(init), Iterable.class);
}
System.out.println("testIterateReverse i="+i+" : "+Arrays.asList(iterator, init, body));
MethodHandle loop = MethodHandles.iteratedLoop(iterator, init, body);
MethodType expectedType = Iterate.MT_reverse;
if (iterator == null && i >= 2)
expectedType = expectedType.changeParameterType(0, Iterable.class);
assertEquals(expectedType, loop.type());
List<String> list = Arrays.asList("a", "b", "c", "d", "e");
List<String> reversedList = Arrays.asList("e", "d", "c", "b", "a");
assertEquals(reversedList, (List<String>) loop.invoke(list));
}
}
示例7: testIteratePrint
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
@Test(dataProvider = "iteratorInits")
public static void testIteratePrint(MethodHandle iterator) throws Throwable {
MethodHandle body = Iterate.MH_printStep;
MethodType expectedType = Iterate.MT_print;
int barity = body.type().parameterCount();
Class<?> iteratorSource = iterator == null ? null : iterator.type().parameterType(0);
if (iterator != null && iteratorSource != body.type().parameterType(barity-1)) {
// adjust body to accept the other type
body = body.asType(body.type().changeParameterType(barity-1, iteratorSource));
expectedType = expectedType.changeParameterType(0, iteratorSource);
}
MethodHandle loop = MethodHandles.iteratedLoop(iterator, null, body);
assertEquals(expectedType, loop.type());
loop.invoke(Arrays.asList("hello", "world"));
}
示例8: fixReceiverType
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
private static GuardedInvocation fixReceiverType(final GuardedInvocation link, final MethodHandle filter) {
// The receiver may be an Object or a ScriptObject.
final MethodType invType = link.getInvocation().type();
final MethodType newInvType = invType.changeParameterType(0, filter.type().returnType());
return link.asType(newInvType);
}
示例9: testInterfaceCast
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
public void testInterfaceCast(MethodHandle mh, Class<?> ctype,
boolean doret, boolean docast) throws Throwable {
MethodHandle mh0 = mh;
if (verbosity > 1)
System.out.println("mh="+mh+", ctype="+ctype.getName()+", doret="+doret+", docast="+docast);
String normalRetVal = "normal return value";
MethodType mt = mh.type();
MethodType mt0 = mt;
if (doret) mt = mt.changeReturnType(ctype);
else mt = mt.changeParameterType(0, ctype);
if (docast) mh = MethodHandles.explicitCastArguments(mh, mt);
else mh = mh.asType(mt);
assertEquals(mt, mh.type());
MethodType mt1 = mt;
// this bit is needed to make the interface types disappear for invokeWithArguments:
mh = MethodHandles.explicitCastArguments(mh, mt.generic());
Class<?>[] step = {
mt1.parameterType(0), // param as passed to mh at first
mt0.parameterType(0), // param after incoming cast
mt0.returnType(), // return value before cast
mt1.returnType(), // return value after outgoing cast
};
// where might a checkCast occur?
boolean[] checkCast = new boolean[step.length];
// the string value must pass each step without causing an exception
if (!docast) {
if (!doret) {
if (step[0] != step[1])
checkCast[1] = true; // incoming value is cast
} else {
if (step[2] != step[3])
checkCast[3] = true; // outgoing value is cast
}
}
boolean expectFail = false;
for (int i = 0; i < step.length; i++) {
Class<?> c = step[i];
if (!checkCast[i]) c = i2o(c);
if (!c.isInstance(normalRetVal)) {
if (verbosity > 3)
System.out.println("expect failure at step "+i+" in "+Arrays.toString(step)+Arrays.toString(checkCast));
expectFail = true;
break;
}
}
countTest(!expectFail);
if (verbosity > 2)
System.out.println("expectFail="+expectFail+", mt="+mt);
Object res;
try {
res = mh.invokeWithArguments(normalRetVal);
} catch (Exception ex) {
res = ex;
}
boolean sawFail = !(res instanceof String);
if (sawFail != expectFail) {
System.out.println("*** testInterfaceCast: mh0 = "+mh0);
System.out.println(" retype using "+(docast ? "explicitCastArguments" : "asType")+" to "+mt+" => "+mh);
System.out.println(" call returned "+res);
System.out.println(" expected "+(expectFail ? "an exception" : normalRetVal));
}
if (!expectFail) {
assertFalse(res.toString(), sawFail);
assertEquals(normalRetVal, res);
} else {
assertTrue(res.toString(), sawFail);
}
}
示例10: getGuardedInvocation
import java.lang.invoke.MethodType; //导入方法依赖的package包/类
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
final Object objBoundCallable = linkRequest.getReceiver();
if(!(objBoundCallable instanceof BoundCallable)) {
return null;
}
final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
final Operation operation = NamedOperation.getBaseOperation(descriptor.getOperation());
// We need to distinguish NEW from CALL because CALL sites have parameter list of the form
// "callee, this, args", while NEW sites have "callee, args" -- they lack the "this" parameter.
final boolean isCall;
if (operation == StandardOperation.NEW) {
isCall = false;
} else if (operation == StandardOperation.CALL) {
isCall = true;
} else {
// Only CALL and NEW are supported.
return null;
}
final BoundCallable boundCallable = (BoundCallable)objBoundCallable;
final Object callable = boundCallable.getCallable();
final Object boundThis = boundCallable.getBoundThis();
// We need to ask the linker services for a delegate invocation on the target callable.
// Replace arguments (boundCallable[, this], args) => (callable[, boundThis], boundArgs, args) when delegating
final Object[] args = linkRequest.getArguments();
final Object[] boundArgs = boundCallable.getBoundArgs();
final int argsLen = args.length;
final int boundArgsLen = boundArgs.length;
final Object[] newArgs = new Object[argsLen + boundArgsLen];
newArgs[0] = callable;
final int firstArgIndex;
if (isCall) {
newArgs[1] = boundThis;
firstArgIndex = 2;
} else {
firstArgIndex = 1;
}
System.arraycopy(boundArgs, 0, newArgs, firstArgIndex, boundArgsLen);
System.arraycopy(args, firstArgIndex, newArgs, firstArgIndex + boundArgsLen, argsLen - firstArgIndex);
// Use R(T0, T1, T2, ...) => R(callable.class, boundThis.class, boundArg0.class, ..., boundArgn.class, T2, ...)
// call site type when delegating to underlying linker (for NEW, there's no this).
final MethodType type = descriptor.getMethodType();
// Use R(T0, ...) => R(callable.class, ...)
MethodType newMethodType = descriptor.getMethodType().changeParameterType(0, callable.getClass());
if (isCall) {
// R(callable.class, T1, ...) => R(callable.class, boundThis.class, ...)
newMethodType = newMethodType.changeParameterType(1, boundThis == null? Object.class : boundThis.getClass());
}
// R(callable.class[, boundThis.class], T2, ...) => R(callable.class[, boundThis.class], boundArg0.class, ..., boundArgn.class, T2, ...)
for(int i = boundArgs.length; i-- > 0;) {
newMethodType = newMethodType.insertParameterTypes(firstArgIndex, boundArgs[i] == null ? Object.class : boundArgs[i].getClass());
}
final CallSiteDescriptor newDescriptor = descriptor.changeMethodType(newMethodType);
// Delegate to target's linker
final GuardedInvocation inv = linkerServices.getGuardedInvocation(linkRequest.replaceArguments(newDescriptor, newArgs));
if(inv == null) {
return null;
}
// Bind (callable[, boundThis], boundArgs) to the delegate handle
final MethodHandle boundHandle = MethodHandles.insertArguments(inv.getInvocation(), 0,
Arrays.copyOf(newArgs, firstArgIndex + boundArgs.length));
final Class<?> p0Type = type.parameterType(0);
final MethodHandle droppingHandle;
if (isCall) {
// Ignore incoming boundCallable and this
droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type, type.parameterType(1));
} else {
// Ignore incoming boundCallable
droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type);
}
// Identity guard on boundCallable object
final MethodHandle newGuard = Guards.getIdentityGuard(boundCallable);
return inv.replaceMethods(droppingHandle, newGuard.asType(newGuard.type().changeParameterType(0, p0Type)));
}