當前位置: 首頁>>代碼示例>>Java>>正文


Java MethodHandles.collectArguments方法代碼示例

本文整理匯總了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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:44,代碼來源:MethodHandlesTest.java


注:本文中的java.lang.invoke.MethodHandles.collectArguments方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。