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


Java MethodType.changeParameterType方法代碼示例

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

示例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;
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:23,代碼來源:LoopCombinatorTest.java

示例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));
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:23,代碼來源:LoopCombinatorTest.java

示例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;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:11,代碼來源:ScriptFunctionData.java

示例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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:LoopCombinatorTest.java

示例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));
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:29,代碼來源:LoopCombinatorTest.java

示例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"));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:16,代碼來源:LoopCombinatorTest.java

示例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);
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:7,代碼來源:WithObject.java

示例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);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:69,代碼來源:MethodHandlesTest.java

示例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)));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:81,代碼來源:BoundCallableLinker.java


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