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

The following examples show how to use java.lang.invoke.MethodHandle#asCollector() . 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
Source File: ValueConversions.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: ValueConversions.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: BigArityTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: BigArityTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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 5
Source File: SpreadCollectTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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 6
Source File: DynamicLinker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: DynamicLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
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 8
Source File: BigArityTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: BigArityTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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 10
Source File: DynamicLinker.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: BigArityTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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 12
Source File: DynamicLinker.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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 13
Source File: MethodHandleFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@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 14
Source File: MethodHandleFactory.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: MethodHandleFactory.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public MethodHandle asCollector(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
    return handle.asCollector(arrayType, arrayLength);
}
 
Example 16
Source File: MethodHandleFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: SingleDynamicMethod.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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
Source File: SingleDynamicMethod.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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 19
Source File: SingleDynamicMethod.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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 20
Source File: SingleDynamicMethod.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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);
}