本文整理汇总了Java中java.lang.invoke.MethodHandles.collectArguments方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandles.collectArguments方法的具体用法?Java MethodHandles.collectArguments怎么用?Java MethodHandles.collectArguments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodHandles
的用法示例。
在下文中一共展示了MethodHandles.collectArguments方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testFoldOrCollectArguments
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
void testFoldOrCollectArguments(List<Class<?>> argTypes, // argument types minus the inserted combineType
int pos, int fold, // position and length of the folded arguments
Class<?> combineType, // type returned from the combiner
Class<?> lastType, // type returned from the target
boolean isCollect,
boolean withFoldPos) throws Throwable {
int nargs = argTypes.size();
if (pos != 0 && !isCollect && !withFoldPos) return; // test MethodHandles.foldArguments(MH,MH) only for pos=0
countTest();
List<Class<?>> combineArgTypes = argTypes.subList(pos, pos + fold);
List<Class<?>> targetArgTypes = new ArrayList<>(argTypes);
if (isCollect) // does target see arg[pos..pos+cc-1]?
targetArgTypes.subList(pos, pos + fold).clear();
if (combineType != void.class)
targetArgTypes.add(pos, combineType);
MethodHandle target = varargsList(targetArgTypes, lastType);
MethodHandle combine = varargsList(combineArgTypes, combineType);
List<Object> argsToPass = Arrays.asList(randomArgs(argTypes));
if (verbosity >= 3)
System.out.println((isCollect ? "collect" : "fold")+" "+target+" with "+combine);
MethodHandle target2;
if (isCollect)
target2 = MethodHandles.collectArguments(target, pos, combine);
else
target2 = withFoldPos ? MethodHandles.foldArguments(target, pos, combine) : MethodHandles.foldArguments(target, combine);
// Simulate expected effect of combiner on arglist:
List<Object> expectedList = new ArrayList<>(argsToPass);
List<Object> argsToFold = expectedList.subList(pos, pos + fold);
if (verbosity >= 3)
System.out.println((isCollect ? "collect" : "fold")+": "+argsToFold+" into "+target2);
Object foldedArgs = combine.invokeWithArguments(argsToFold);
if (isCollect)
argsToFold.clear();
if (combineType != void.class)
argsToFold.add(0, foldedArgs);
Object result = target2.invokeWithArguments(argsToPass);
if (verbosity >= 3)
System.out.println("result: "+result);
Object expected = target.invokeWithArguments(expectedList);
if (!expected.equals(result))
System.out.println("*** fail at n/p/f = "+nargs+"/"+pos+"/"+fold+": "+argsToPass+" => "+result+" != "+expected);
assertEquals(expected, result);
}