当前位置: 首页>>代码示例>>Java>>正文


Java MethodHandle.invokeWithArguments方法代码示例

本文整理汇总了Java中java.lang.invoke.MethodHandle.invokeWithArguments方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandle.invokeWithArguments方法的具体用法?Java MethodHandle.invokeWithArguments怎么用?Java MethodHandle.invokeWithArguments使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.lang.invoke.MethodHandle的用法示例。


在下文中一共展示了MethodHandle.invokeWithArguments方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testAsCollector

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
public void testAsCollector(Class<?> argType, int pos, int nargs) throws Throwable {
        countTest();
        // fake up a MH with the same type as the desired adapter:
        MethodHandle fake = varargsArray(nargs);
        fake = changeArgTypes(fake, argType);
        MethodType newType = fake.type();
        Object[] args = randomArgs(newType.parameterArray());
        // here is what should happen:
        Object[] collectedArgs = Arrays.copyOfRange(args, 0, pos+1);
        collectedArgs[pos] = Arrays.copyOfRange(args, pos, args.length);
        // here is the MH which will witness the collected argument tail:
        MethodHandle target = varargsArray(pos+1);
        target = changeArgTypes(target, 0, pos, argType);
        target = changeArgTypes(target, pos, pos+1, Object[].class);
        if (verbosity >= 3)
            System.out.println("collect from "+Arrays.asList(args)+" ["+pos+".."+nargs+"]");
        MethodHandle result = target.asCollector(Object[].class, nargs-pos).asType(newType);
        Object[] returnValue = (Object[]) result.invokeWithArguments(args);
//        assertTrue(returnValue.length == pos+1 && returnValue[pos] instanceof Object[]);
//        returnValue[pos] = Arrays.asList((Object[]) returnValue[pos]);
//        collectedArgs[pos] = Arrays.asList((Object[]) collectedArgs[pos]);
        assertArrayEquals(collectedArgs, returnValue);
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:MethodHandlesTest.java

示例2: invoke

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
/**
 * 调用指定对象的方法
 * @param method    被调用的method对象
 * @param target    被调用的对象
 * @param args      方法的参数值
 * @return
 */
static Object invoke(Object target, Method method, Object... args) {
    try {
        MethodHandle mh = lookup.unreflect(method);
        if (args == null || args.length == 0) {
            return mh.invoke(target);
        }
        Object[] argsVal = new Object[args.length + 1];
        System.arraycopy(args, 0, argsVal, 1, args.length);
        argsVal[0] = target;
        return mh.invokeWithArguments(argsVal);
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return null;
}
 
开发者ID:NymphWeb,项目名称:nymph,代码行数:23,代码来源:Dynamics.java

示例3: testFilterArguments

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
void testFilterArguments(int nargs, int pos) throws Throwable {
    countTest();
    MethodHandle target = varargsList(nargs);
    MethodHandle filter = varargsList(1);
    filter = filter.asType(filter.type().generic());
    Object[] argsToPass = randomArgs(nargs, Object.class);
    if (verbosity >= 3)
        System.out.println("filter "+target+" at "+pos+" with "+filter);
    MethodHandle target2 = MethodHandles.filterArguments(target, pos, filter);
    // Simulate expected effect of filter on arglist:
    Object[] filteredArgs = argsToPass.clone();
    filteredArgs[pos] = filter.invokeExact(filteredArgs[pos]);
    List<Object> expected = Arrays.asList(filteredArgs);
    Object result = target2.invokeWithArguments(argsToPass);
    if (verbosity >= 3)
        System.out.println("result: "+result);
    if (!expected.equals(result))
        System.out.println("*** fail at n/p = "+nargs+"/"+pos+": "+Arrays.asList(argsToPass)+" => "+result+" != "+expected);
    assertEquals(expected, result);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:MethodHandlesTest.java

示例4: testUserClassInSignature0

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
public void testUserClassInSignature0() throws Throwable {
    if (CAN_SKIP_WORKING)  return;
    startTest("testUserClassInSignature");
    Lookup lookup = MethodHandles.lookup();
    String name; MethodType mt; MethodHandle mh;
    Object[] args;

    // Try a static method.
    name = "userMethod";
    mt = MethodType.methodType(Example.class, Object.class, String.class, int.class);
    mh = lookup.findStatic(lookup.lookupClass(), name, mt);
    assertEquals(mt, mh.type());
    assertEquals(Example.class, mh.type().returnType());
    args = randomArgs(mh.type().parameterArray());
    mh.invokeWithArguments(args);
    assertCalled(name, args);

    // Try a virtual method.
    name = "v2";
    mt = MethodType.methodType(Object.class, Object.class, int.class);
    mh = lookup.findVirtual(Example.class, name, mt);
    assertEquals(mt, mh.type().dropParameterTypes(0,1));
    assertTrue(mh.type().parameterList().contains(Example.class));
    args = randomArgs(mh.type().parameterArray());
    mh.invokeWithArguments(args);
    assertCalled(name, args);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:28,代码来源:MethodHandlesTest.java

示例5: invoke

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
/**
 * Execute this script function.
 *
 * @param self  Target object.
 * @param arguments  Call arguments.
 * @return ScriptFunction result.
 *
 * @throws Throwable if there is an exception/error with the invocation or thrown from it
 */
Object invoke(final ScriptFunction fn, final Object self, final Object... arguments) throws Throwable {
    final MethodHandle mh      = getGenericInvoker(fn.getScope());
    final Object       selfObj = convertThisObject(self);
    final Object[]     args    = arguments == null ? ScriptRuntime.EMPTY_ARRAY : arguments;

    DebuggerSupport.notifyInvoke(mh);

    if (isVarArg(mh)) {
        if (needsCallee(mh)) {
            return mh.invokeExact(fn, selfObj, args);
        }
        return mh.invokeExact(selfObj, args);
    }

    final int paramCount = mh.type().parameterCount();
    if (needsCallee(mh)) {
        switch (paramCount) {
        case 2:
            return mh.invokeExact(fn, selfObj);
        case 3:
            return mh.invokeExact(fn, selfObj, getArg(args, 0));
        case 4:
            return mh.invokeExact(fn, selfObj, getArg(args, 0), getArg(args, 1));
        case 5:
            return mh.invokeExact(fn, selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2));
        case 6:
            return mh.invokeExact(fn, selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3));
        case 7:
            return mh.invokeExact(fn, selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4));
        case 8:
            return mh.invokeExact(fn, selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4), getArg(args, 5));
        default:
            return mh.invokeWithArguments(withArguments(fn, selfObj, paramCount, args));
        }
    }

    switch (paramCount) {
    case 1:
        return mh.invokeExact(selfObj);
    case 2:
        return mh.invokeExact(selfObj, getArg(args, 0));
    case 3:
        return mh.invokeExact(selfObj, getArg(args, 0), getArg(args, 1));
    case 4:
        return mh.invokeExact(selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2));
    case 5:
        return mh.invokeExact(selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3));
    case 6:
        return mh.invokeExact(selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4));
    case 7:
        return mh.invokeExact(selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4), getArg(args, 5));
    default:
        return mh.invokeWithArguments(withArguments(null, selfObj, paramCount, args));
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:65,代码来源:ScriptFunctionData.java

示例6: testVarargsCollector

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
/**
 * Tests that MHs.explicitCastArguments does incorrect type checks for
 * VarargsCollector. Bug 8066746.
 *
 * @throws java.lang.Throwable
 */
public static void testVarargsCollector() throws Throwable {
    MethodType mt = MethodType.methodType(String[].class, String[].class);
    MethodHandle mh = MethodHandles.publicLookup()
            .findStatic(THIS_CLASS, "f", mt);
    mh = MethodHandles.explicitCastArguments(mh,
            MethodType.methodType(Object.class, Object.class));
    mh.invokeWithArguments((Object) (new String[]{"str1", "str2"}));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:ExplicitCastArgumentsTest.java

示例7: testMultipleArgs

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
/**
 * Tests that MHs.eCA method works correctly with MHs with multiple arguments.
 * @throws Throwable
 */
public static void testMultipleArgs() throws Throwable {
    int arity = 1 + RNG.nextInt(Helper.MAX_ARITY / 2 - 2);
    int arityMinus = RNG.nextInt(arity);
    int arityPlus = arity + RNG.nextInt(Helper.MAX_ARITY / 2 - arity) + 1;
    MethodType mType = Helper.randomMethodTypeGenerator(arity);
    MethodType mTypeNew = Helper.randomMethodTypeGenerator(arity);
    MethodType mTypeNewMinus = Helper.randomMethodTypeGenerator(arityMinus);
    MethodType mTypeNewPlus = Helper.randomMethodTypeGenerator(arityPlus);
    Class<?> rType = mType.returnType();
    MethodHandle original;
    if (rType.equals(void.class)) {
        MethodType mt = MethodType.methodType(void.class);
        original = MethodHandles.publicLookup()
                .findStatic(THIS_CLASS, "retVoid", mt);
    } else {
        Object rValue = Helper.castToWrapper(1, rType);
        original = MethodHandles.constant(rType, rValue);
    }
    original = Helper.addTrailingArgs(original, arity, mType.parameterList());
    MethodHandle target = MethodHandles
                .explicitCastArguments(original, mTypeNew);
    Object[] parList = Helper.randomArgs(mTypeNew.parameterList());
    for (int i = 0; i < parList.length; i++) {
        if (parList[i] instanceof String) {
            parList[i] = null; //getting rid of Stings produced by randomArgs
        }
    }
    target.invokeWithArguments(parList);
    checkForWrongMethodTypeException(original, mTypeNewMinus);
    checkForWrongMethodTypeException(original, mTypeNewPlus);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:36,代码来源:ExplicitCastArgumentsTest.java

示例8: testArities

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
private void testArities(Class<? extends Object[]> cls,
                         int minArity,
                         int maxArity,
                         int iterations) throws Throwable {
    boolean verbose = (cls == Object[].class);
    for (int arity = minArity; arity <= maxArity; arity++) {
        if (verbose)  System.out.println("arity="+arity);
        MethodHandle mh = MH_hashArguments(cls, arity);
        MethodHandle mh_VA = mh.asSpreader(cls, arity);
        assert(mh_VA.type().parameterType(0) == cls);
        testArities(cls, arity, iterations, verbose, mh, mh_VA);
        // mh_CA will collect arguments of a particular type and pass them to mh_VA
        MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
        MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
        assert(mh_CA.type().equals(mh.type()));
        assert(mh_VA2.type().equals(mh_VA.type()));
        if (cls != Object[].class) {
            try {
                mh_VA2.invokeWithArguments(new Object[arity]);
                throw new AssertionError("should not reach");
            } catch (ClassCastException | WrongMethodTypeException ex) {
            }
        }
        int iterations_VA = iterations / 100;
        testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2);
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:28,代码来源:BigArityTest.java

示例9: testFindStatic

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
void testFindStatic(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
    countTest(positive);
    String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
    MethodType type = MethodType.methodType(ret, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        target = maybeMoveIn(lookup, defc).findStatic(defc, methodName, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            :   IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findStatic "+lookup+": "+defc.getName()+"."+name+"/"+type+" => "+target
                +(noAccess == null ? "" : " !! "+noAccess));
    if (positive && noAccess != null)  throw noAccess;
    assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
    if (!positive)  return; // negative test failed as expected
    assertEquals(type, target.type());
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(params);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
    if (verbosity >= 1)
        System.out.print(':');
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:MethodHandlesTest.java

示例10: testFindConstructor

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
void testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(noAccess == null ? "" : " !! "+noAccess));
    if (positive && noAccess != null)  throw noAccess;
    assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
    if (!positive)  return; // negative test failed as expected
    assertEquals(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:29,代码来源:MethodHandlesTest.java

示例11: testBind

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
void testBind(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
    countTest(positive);
    String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
    MethodType type = MethodType.methodType(ret, params);
    Object receiver = randomArg(defc);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        target = maybeMoveIn(lookup, defc).bind(receiver, methodName, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            :   IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("bind "+receiver+"."+name+"/"+type+" => "+target
                +(noAccess == null ? "" : " !! "+noAccess));
    if (positive && noAccess != null)  throw noAccess;
    assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
    if (!positive)  return; // negative test failed as expected
    assertEquals(type, target.type());
    Object[] args = randomArgs(params);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    Object[] argsWithReceiver = cat(array(Object[].class, receiver), args);
    assertCalled(name, argsWithReceiver);
    if (verbosity >= 1)
        System.out.print(':');
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:35,代码来源:MethodHandlesTest.java

示例12: testGuardWithTest

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
void testGuardWithTest(int nargs, int testDrops, Class<?> argClass) throws Throwable {
    countTest();
    int nargs1 = Math.min(3, nargs);
    MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.methodType(boolean.class, Object.class));
    MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.genericMethodType(nargs1));
    MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.genericMethodType(nargs1));
    while (test.type().parameterCount() > nargs)
        // 0: test = constant(MISSING_ARG.equals(MISSING_ARG))
        // 1: test = lambda (_) MISSING_ARG.equals(_)
        test = MethodHandles.insertArguments(test, 0, MISSING_ARG);
    if (argClass != Object.class) {
        test = changeArgTypes(test, argClass);
        target = changeArgTypes(target, argClass);
        fallback = changeArgTypes(fallback, argClass);
    }
    int testArgs = nargs - testDrops;
    assert(testArgs >= 0);
    test = addTrailingArgs(test, Math.min(testArgs, nargs), argClass);
    target = addTrailingArgs(target, nargs, argClass);
    fallback = addTrailingArgs(fallback, nargs, argClass);
    Object[][] argLists = {
        { },
        { "foo" }, { MISSING_ARG },
        { "foo", "foo" }, { "foo", "bar" },
        { "foo", "foo", "baz" }, { "foo", "bar", "baz" }
    };
    for (Object[] argList : argLists) {
        Object[] argList1 = argList;
        if (argList.length != nargs) {
            if (argList.length != nargs1)  continue;
            argList1 = Arrays.copyOf(argList, nargs);
            Arrays.fill(argList1, nargs1, nargs, MISSING_ARG_2);
        }
        MethodHandle test1 = test;
        if (test1.type().parameterCount() > testArgs) {
            int pc = test1.type().parameterCount();
            test1 = MethodHandles.insertArguments(test, testArgs, Arrays.copyOfRange(argList1, testArgs, pc));
        }
        MethodHandle mh = MethodHandles.guardWithTest(test1, target, fallback);
        assertEquals(target.type(), mh.type());
        boolean equals;
        switch (nargs) {
        case 0:   equals = true; break;
        case 1:   equals = MISSING_ARG.equals(argList[0]); break;
        default:  equals = argList[0].equals(argList[1]); break;
        }
        String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals");
        if (verbosity >= 3)
            System.out.println(logEntry(willCall, argList));
        Object result = mh.invokeWithArguments(argList1);
        assertCalled(willCall, argList);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:54,代码来源:MethodHandlesTest.java

示例13: construct

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
Object construct(final ScriptFunction fn, final Object... arguments) throws Throwable {
    final MethodHandle mh   = getGenericConstructor(fn.getScope());
    final Object[]     args = arguments == null ? ScriptRuntime.EMPTY_ARRAY : arguments;

    DebuggerSupport.notifyInvoke(mh);

    if (isVarArg(mh)) {
        if (needsCallee(mh)) {
            return mh.invokeExact(fn, args);
        }
        return mh.invokeExact(args);
    }

    final int paramCount = mh.type().parameterCount();
    if (needsCallee(mh)) {
        switch (paramCount) {
        case 1:
            return mh.invokeExact(fn);
        case 2:
            return mh.invokeExact(fn, getArg(args, 0));
        case 3:
            return mh.invokeExact(fn, getArg(args, 0), getArg(args, 1));
        case 4:
            return mh.invokeExact(fn, getArg(args, 0), getArg(args, 1), getArg(args, 2));
        case 5:
            return mh.invokeExact(fn, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3));
        case 6:
            return mh.invokeExact(fn, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4));
        case 7:
            return mh.invokeExact(fn, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4), getArg(args, 5));
        default:
            return mh.invokeWithArguments(withArguments(fn, paramCount, args));
        }
    }

    switch (paramCount) {
    case 0:
        return mh.invokeExact();
    case 1:
        return mh.invokeExact(getArg(args, 0));
    case 2:
        return mh.invokeExact(getArg(args, 0), getArg(args, 1));
    case 3:
        return mh.invokeExact(getArg(args, 0), getArg(args, 1), getArg(args, 2));
    case 4:
        return mh.invokeExact(getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3));
    case 5:
        return mh.invokeExact(getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4));
    case 6:
        return mh.invokeExact(getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4), getArg(args, 5));
    default:
        return mh.invokeWithArguments(withArguments(null, paramCount, args));
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:55,代码来源:ScriptFunctionData.java

示例14: testSpreads

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@Test
public void testSpreads() throws Throwable {
    System.out.println("testing asSpreader on arity=3");
    Object[] args = testArgs(3);
    int r0 = Objects.hash(args);
    MethodHandle mh = MH_hashArguments(3);
    Object r;
    r = mh.invokeExact(args[0], args[1], args[2]);
    assertEquals(r0, r);
    r = mh.invoke(args[0], args[1], args[2]);
    assertEquals(r0, r);
    r = mh.invoke((Comparable) args[0], (Integer) args[1], (Number) args[2]);
    assertEquals(r0, r);
    r = mh.invokeWithArguments(args);
    assertEquals(r0, r);
    for (Class<?> cls0 : new Class<?>[] {
        Object[].class, Number[].class, Integer[].class, Comparable[].class
    }) {
        @SuppressWarnings("unchecked")
        Class<? extends Object[]> cls = (Class<? extends Object[]>) cls0;
        //Class<? extends Object[]> cls = Object[].class.asSubclass(cls0);
        int nargs = args.length, skip;
        MethodHandle smh = mh.asSpreader(cls, nargs - (skip = 0));
        Object[] tail = Arrays.copyOfRange(args, skip, nargs, cls);
        if (cls == Object[].class)
            r = smh.invokeExact(tail);
        else if (cls == Integer[].class)
            r = smh.invokeExact((Integer[]) tail); //warning OK, see 8019340
        else
            r = smh.invoke(tail);
        assertEquals(r0, r);
        smh = mh.asSpreader(cls, nargs - (skip = 1));
        tail = Arrays.copyOfRange(args, skip, nargs, cls);
        if (cls == Object[].class)
            r = smh.invokeExact(args[0], tail);
        else if (cls == Integer[].class)
            r = smh.invokeExact(args[0], (Integer[]) tail);
        else
            r = smh.invoke(args[0], tail);
        assertEquals(r0, r);
        smh = mh.asSpreader(cls, nargs - (skip = 2));
        tail = Arrays.copyOfRange(args, skip, nargs, cls);
        if (cls == Object[].class)
            r = smh.invokeExact(args[0], args[1], tail);
        else if (cls == Integer[].class)
            r = smh.invokeExact(args[0], args[1], (Integer[]) tail);
        else
            r = smh.invoke(args[0], args[1], tail);
        assertEquals(r0, r);
        smh = mh.asSpreader(cls, nargs - (skip = 3));
        tail = Arrays.copyOfRange(args, skip, nargs, cls);
        if (cls == Object[].class)
            r = smh.invokeExact(args[0], args[1], args[2], tail);
        else if (cls == Integer[].class)
            r = smh.invokeExact(args[0], args[1], args[2], (Integer[]) tail);
        else
            r = smh.invoke(args[0], args[1], args[2], tail);
        assertEquals(r0, r);
        // Try null array in addition to zero-length array:
        tail = null;
        if (cls == Object[].class)
            r = smh.invokeExact(args[0], args[1], args[2], tail);
        else if (cls == Integer[].class)
            r = smh.invokeExact(args[0], args[1], args[2], (Integer[]) tail);
        else
            r = smh.invoke(args[0], args[1], args[2], tail);
        assertEquals(r0, r);
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:70,代码来源:BigArityTest.java

示例15: runTest

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
private void runTest() {
    if (Helper.IS_VERBOSE) {
        System.out.printf("CatchException(%s, isVararg=%b argsCount=%d " +
                        "dropped=%d)%n",
                testCase, thrower.isVarargsCollector(), argsCount, dropped);
    }

    Helper.clear();

    Object[] args = Helper.randomArgs(
            argsCount, thrower.type().parameterArray());
    Object arg0 = Helper.MISSING_ARG;
    Object arg1 = testCase.thrown;
    if (argsCount > 0) {
        arg0 = args[0];
    }
    if (argsCount > 1) {
        args[1] = arg1;
    }
    Asserts.assertEQ(nargs, thrower.type().parameterCount());
    if (argsCount < nargs) {
        Object[] appendArgs = {arg0, arg1};
        appendArgs = Arrays.copyOfRange(appendArgs, argsCount, nargs);
        thrower = MethodHandles.insertArguments(
                thrower, argsCount, appendArgs);
    }
    Asserts.assertEQ(argsCount, thrower.type().parameterCount());

    MethodHandle target = MethodHandles.catchException(
            testCase.filter(thrower), testCase.throwableClass,
            testCase.filter(catcher));

    Asserts.assertEQ(thrower.type(), target.type());
    Asserts.assertEQ(argsCount, target.type().parameterCount());

    Object returned;
    try {
        returned = target.invokeWithArguments(args);
    } catch (Throwable ex) {
        if (CodeCacheOverflowProcessor.isThrowableCausedByVME(ex)) {
            // This error will be treated by CodeCacheOverflowProcessor
            // to prevent the test from failing because of code cache overflow.
            throw new Error(ex);
        }
        testCase.assertCatch(ex);
        returned = ex;
    }

    testCase.assertReturn(returned, arg0, arg1, dropped, args);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:51,代码来源:CatchExceptionTest.java


注:本文中的java.lang.invoke.MethodHandle.invokeWithArguments方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。