Java Code Examples for java.lang.invoke.MethodHandle#asCollector()

The following examples show how to use java.lang.invoke.MethodHandle#asCollector() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private static MethodHandle buildVarargsArray(MethodHandle newArray, MethodHandle finisher, int nargs) {
    // Build up the result mh as a sequence of fills like this:
    //   finisher(fill(fill(newArrayWA(23,x1..x10),10,x11..x20),20,x21..x23))
    // The various fill(_,10*I,___*[J]) are reusable.
    int leftLen = Math.min(nargs, LEFT_ARGS);  // absorb some arguments immediately
    int rightLen = nargs - leftLen;
    MethodHandle leftCollector = newArray.bindTo(nargs);
    leftCollector = leftCollector.asCollector(Object[].class, leftLen);
    MethodHandle mh = finisher;
    if (rightLen > 0) {
        MethodHandle rightFiller = fillToRight(LEFT_ARGS + rightLen);
        if (mh == ARRAY_IDENTITY)
            mh = rightFiller;
        else
            mh = MethodHandles.collectArguments(mh, 0, rightFiller);
    }
    if (mh == ARRAY_IDENTITY)
        mh = leftCollector;
    else
        mh = MethodHandles.collectArguments(mh, 0, leftCollector);
    return mh;
}
 
Example 2
private static MethodHandle buildVarargsArray(MethodHandle newArray, MethodHandle finisher, int nargs) {
    // Build up the result mh as a sequence of fills like this:
    //   finisher(fill(fill(newArrayWA(23,x1..x10),10,x11..x20),20,x21..x23))
    // The various fill(_,10*I,___*[J]) are reusable.
    int leftLen = Math.min(nargs, LEFT_ARGS);  // absorb some arguments immediately
    int rightLen = nargs - leftLen;
    MethodHandle leftCollector = newArray.bindTo(nargs);
    leftCollector = leftCollector.asCollector(Object[].class, leftLen);
    MethodHandle mh = finisher;
    if (rightLen > 0) {
        MethodHandle rightFiller = fillToRight(LEFT_ARGS + rightLen);
        if (mh == ARRAY_IDENTITY)
            mh = rightFiller;
        else
            mh = MethodHandles.collectArguments(mh, 0, rightFiller);
    }
    if (mh == ARRAY_IDENTITY)
        mh = leftCollector;
    else
        mh = MethodHandles.collectArguments(mh, 0, leftCollector);
    return mh;
}
 
Example 3
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);
    }
}
 
Example 4
private MethodHandle createRelinkAndInvokeMethod(final RelinkableCallSite callSite, final int relinkCount) {
    // Make a bound MH of invoke() for this linker and call site
    final MethodHandle boundRelinker = MethodHandles.insertArguments(RELINK, 0, this, callSite, Integer.valueOf(
            relinkCount));
    // Make a MH that gathers all arguments to the invocation into an Object[]
    final MethodType type = callSite.getDescriptor().getMethodType();
    final MethodHandle collectingRelinker = boundRelinker.asCollector(Object[].class, type.parameterCount());
    return MethodHandles.foldArguments(MethodHandles.exactInvoker(type), collectingRelinker.asType(
            type.changeReturnType(MethodHandle.class)));
}
 
Example 5
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);
    }
}
 
Example 6
private MethodHandle createRelinkAndInvokeMethod(final RelinkableCallSite callSite, final int relinkCount) {
    // Make a bound MH of invoke() for this linker and call site
    final MethodHandle boundRelinker = MethodHandles.insertArguments(RELINK, 0, this, callSite, Integer.valueOf(
            relinkCount));
    // Make a MH that gathers all arguments to the invocation into an Object[]
    final MethodType type = callSite.getDescriptor().getMethodType();
    final MethodHandle collectingRelinker = boundRelinker.asCollector(Object[].class, type.parameterCount());
    return MethodHandles.foldArguments(MethodHandles.exactInvoker(type), collectingRelinker.asType(
            type.changeReturnType(MethodHandle.class)));
}
 
Example 7
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);
    }
}
 
Example 8
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);
    }
}
 
Example 9
private MethodHandle createRelinkAndInvokeMethod(final RelinkableCallSite callSite, final int relinkCount) {
    // Make a bound MH of invoke() for this linker and call site
    final MethodHandle boundRelinker = MethodHandles.insertArguments(RELINK, 0, this, callSite, Integer.valueOf(
            relinkCount));
    // Make a MH that gathers all arguments to the invocation into an Object[]
    final MethodType type = callSite.getDescriptor().getMethodType();
    final MethodHandle collectingRelinker = boundRelinker.asCollector(Object[].class, type.parameterCount());
    return MethodHandles.foldArguments(MethodHandles.exactInvoker(type), collectingRelinker.asType(
            type.changeReturnType(MethodHandle.class)));
}
 
Example 10
private MethodHandle createRelinkAndInvokeMethod(final RelinkableCallSite callSite, final int relinkCount) {
    // Make a bound MH of invoke() for this linker and call site
    final MethodHandle boundRelinker = MethodHandles.insertArguments(RELINK, 0, this, callSite, Integer.valueOf(
            relinkCount));
    // Make a MH that gathers all arguments to the invocation into an Object[]
    final MethodType type = callSite.getDescriptor().getMethodType();
    final MethodHandle collectingRelinker = boundRelinker.asCollector(Object[].class, type.parameterCount());
    return MethodHandles.foldArguments(MethodHandles.exactInvoker(type), collectingRelinker.asType(
            type.changeReturnType(MethodHandle.class)));
}
 
Example 11
@Test
public static void testAsCollectorExample() throws Throwable {
    // test the JavaDoc asCollector-with-pos example
    StringWriter swr = new StringWriter();
    MethodHandle swWrite = LOOKUP.
            findVirtual(StringWriter.class, "write", methodType(void.class, char[].class, int.class, int.class)).
            bindTo(swr);
    MethodHandle swWrite4 = swWrite.asCollector(0, char[].class, 4);
    swWrite4.invoke('A', 'B', 'C', 'D', 1, 2);
    assertEquals("BC", swr.toString());
    swWrite4.invoke('P', 'Q', 'R', 'S', 0, 4);
    assertEquals("BCPQRS", swr.toString());
    swWrite4.invoke('W', 'X', 'Y', 'Z', 3, 1);
    assertEquals("BCPQRSZ", swr.toString());
}
 
Example 12
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);
        MethodHandle mh_VA_h = mh.asSpreader(0, cls, arity-1);
        assert(mh_VA.type().parameterType(0) == cls);
        assert(mh_VA_h.type().parameterType(0) == cls);
        testArities(cls, arity, iterations, verbose, mh, mh_VA, mh_VA_h);
        // 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);
        MethodHandle mh_VA2_h = mh_CA.asSpreader(0, cls, arity-1);
        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, mh_VA2_h);
    }
}
 
Example 13
@Override
public MethodHandle asCollector(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
    return handle.asCollector(arrayType, arrayLength);
}
 
Example 14
@Override
public MethodHandle asCollector(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
    final MethodHandle mh = handle.asCollector(arrayType, arrayLength);
    return debug(mh, "asCollector", handle, arrayType, arrayLength);
}
 
Example 15
@Override
public MethodHandle asCollector(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
    final MethodHandle mh = handle.asCollector(arrayType, arrayLength);
    return debug(mh, "asCollector", handle, arrayType, arrayLength);
}
 
Example 16
@Override
public MethodHandle asCollector(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
    final MethodHandle mh = handle.asCollector(arrayType, arrayLength);
    return debug(mh, "asCollector", handle, arrayType, arrayLength);
}
 
Example 17
/**
 * Creates a method handle out of the original target that will collect the varargs for the exact component type of
 * the varArg array. Note that this will nicely trigger language-specific type converters for exactly those varargs
 * for which it is necessary when later passed to linkerServices.convertArguments().
 *
 * @param target the original method handle
 * @param parameterCount the total number of arguments in the new method handle
 * @return a collecting method handle
 */
static MethodHandle collectArguments(final MethodHandle target, final int parameterCount) {
    final MethodType methodType = target.type();
    final int fixParamsLen = methodType.parameterCount() - 1;
    final Class<?> arrayType = methodType.parameterType(fixParamsLen);
    return target.asCollector(arrayType, parameterCount - fixParamsLen);
}
 
Example 18
/**
 * Creates a method handle out of the original target that will collect the varargs for the exact component type of
 * the varArg array. Note that this will nicely trigger language-specific type converters for exactly those varargs
 * for which it is necessary when later passed to linkerServices.convertArguments().
 *
 * @param target the original method handle
 * @param parameterCount the total number of arguments in the new method handle
 * @return a collecting method handle
 */
static MethodHandle collectArguments(MethodHandle target, final int parameterCount) {
    final MethodType methodType = target.type();
    final int fixParamsLen = methodType.parameterCount() - 1;
    final Class<?> arrayType = methodType.parameterType(fixParamsLen);
    return target.asCollector(arrayType, parameterCount - fixParamsLen);
}
 
Example 19
/**
 * Creates a method handle out of the original target that will collect the varargs for the exact component type of
 * the varArg array. Note that this will nicely trigger language-specific type converters for exactly those varargs
 * for which it is necessary when later passed to linkerServices.convertArguments().
 *
 * @param target the original method handle
 * @param parameterCount the total number of arguments in the new method handle
 * @return a collecting method handle
 */
static MethodHandle collectArguments(final MethodHandle target, final int parameterCount) {
    final MethodType methodType = target.type();
    final int fixParamsLen = methodType.parameterCount() - 1;
    final Class<?> arrayType = methodType.parameterType(fixParamsLen);
    return target.asCollector(arrayType, parameterCount - fixParamsLen);
}
 
Example 20
/**
 * Creates a method handle out of the original target that will collect the varargs for the exact component type of
 * the varArg array. Note that this will nicely trigger language-specific type converters for exactly those varargs
 * for which it is necessary when later passed to linkerServices.convertArguments().
 *
 * @param target the original method handle
 * @param parameterCount the total number of arguments in the new method handle
 * @return a collecting method handle
 */
static MethodHandle collectArguments(final MethodHandle target, final int parameterCount) {
    final MethodType methodType = target.type();
    final int fixParamsLen = methodType.parameterCount() - 1;
    final Class<?> arrayType = methodType.parameterType(fixParamsLen);
    return target.asCollector(arrayType, parameterCount - fixParamsLen);
}