本文整理汇总了Java中java.lang.invoke.MethodHandles.permuteArguments方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandles.permuteArguments方法的具体用法?Java MethodHandles.permuteArguments怎么用?Java MethodHandles.permuteArguments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodHandles
的用法示例。
在下文中一共展示了MethodHandles.permuteArguments方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testReturnOnStack
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public void testReturnOnStack() throws Throwable {
MethodHandles.Lookup l = MethodHandles.lookup();
MethodHandle consumeIdentity = l.findStatic(
PermuteArgsReturnVoidTest.class, "consumeIdentity",
MethodType.methodType(String.class, String.class, int.class, int.class));
MethodHandle consumeVoid = l.findStatic(
PermuteArgsReturnVoidTest.class, "consumeVoid",
MethodType.methodType(void.class, String.class, int.class, int.class));
MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);
MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);
String s = (String) p.invoke("IN", 0, 0);
Assert.assertEquals(s.getClass(), String.class);
Assert.assertEquals(s, "IN");
}
示例2: testReturnFromArg
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public void testReturnFromArg() throws Throwable {
MethodHandles.Lookup l = MethodHandles.lookup();
MethodHandle consumeIdentity = dropArguments(
identity(String.class), 1, int.class, int.class);
MethodHandle consumeVoid = l.findStatic(
PermuteArgsReturnVoidTest.class, "consumeVoid",
MethodType.methodType(void.class, String.class, int.class, int.class));
MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);
MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);
String s = (String) p.invoke("IN", 0, 0);
Assert.assertEquals(s.getClass(), String.class);
Assert.assertEquals(s, "IN");
}
示例3: or
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
public Pattern or(Pattern match) {
return new Pattern(form.or(match.form),
uberForm -> MethodHandles.permuteArguments(
MethodHandles.filterArguments(
guardWithTest(
dropArguments(
uberForm.createAs(methodType(void.class, int.class, boolean.class)).component(1),
0, Object.class),
dropArguments(identity(Object.class), 0, Object.class),
dropArguments(match.mh.apply(uberForm), 1, Object.class)),
1, mh.apply(uberForm)),
methodType(Object.class, Object.class), new int[] { 0, 0}));
}
示例4: permute
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
static MethodHandle permute(MethodHandle h) {
// The handles representing virtual methods need to be rearranged to match the required order of arguments
// (loop-local state comes first, then loop arguments). As the receiver comes first in the signature but is
// a loop argument, it must be moved to the appropriate position in the signature.
return MethodHandles.permuteArguments(h,
methodType(h.type().returnType(), int.class, int.class, LOOP_WITH_VIRTUALS, int.class), 2, 0, 1, 3);
}
示例5: constantBoolean
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
private static MethodHandle constantBoolean(final Boolean value, final MethodType type) {
return MethodHandles.permuteArguments(MethodHandles.constant(Boolean.TYPE, value),
type.changeReturnType(Boolean.TYPE));
}
示例6: testPermuteArguments
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
void testPermuteArguments(Object[] args, Class<?>[] types, int[] reorder) throws Throwable {
countTest();
if (args == null && types == null) {
int max = 0;
for (int j : reorder) {
if (max < j) max = j;
}
args = randomArgs(max+1, Integer.class);
}
if (args == null) {
args = randomArgs(types);
}
if (types == null) {
types = new Class<?>[args.length];
for (int i = 0; i < args.length; i++)
types[i] = args[i].getClass();
}
int inargs = args.length, outargs = reorder.length;
assertTrue(inargs == types.length);
if (verbosity >= 3)
System.out.println("permuteArguments "+Arrays.toString(reorder));
Object[] permArgs = new Object[outargs];
Class<?>[] permTypes = new Class<?>[outargs];
for (int i = 0; i < outargs; i++) {
permArgs[i] = args[reorder[i]];
permTypes[i] = types[reorder[i]];
}
if (verbosity >= 4) {
System.out.println("in args: "+Arrays.asList(args));
System.out.println("out args: "+Arrays.asList(permArgs));
System.out.println("in types: "+Arrays.asList(types));
System.out.println("out types: "+Arrays.asList(permTypes));
}
MethodType inType = MethodType.methodType(Object.class, types);
MethodType outType = MethodType.methodType(Object.class, permTypes);
MethodHandle target = varargsList(outargs).asType(outType);
MethodHandle newTarget = MethodHandles.permuteArguments(target, inType, reorder);
if (verbosity >= 5) System.out.println("newTarget = "+newTarget);
Object result = newTarget.invokeWithArguments(args);
Object expected = Arrays.asList(permArgs);
if (!expected.equals(result)) {
System.out.println("*** failed permuteArguments "+Arrays.toString(reorder)+" types="+Arrays.asList(types));
System.out.println("in args: "+Arrays.asList(args));
System.out.println("out args: "+expected);
System.out.println("bad args: "+result);
}
assertEquals(expected, result);
}