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

The following examples show how to use java.lang.invoke.MethodHandle#asSpreader() . 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 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 2
Source Project: tutorials   File: MethodHandlesTest.java    License: MIT License 5 votes vote down vote up
@Test
public void givenSpreadedEqualsMethodHandle_whenInvokedOnArray_thenCorrectlyEvaluated() throws Throwable {
    MethodHandles.Lookup publicLookup = MethodHandles.publicLookup();
    MethodType mt = MethodType.methodType(boolean.class, Object.class);
    MethodHandle equalsMH = publicLookup.findVirtual(String.class, "equals", mt);

    MethodHandle methodHandle = equalsMH.asSpreader(Object[].class, 2);

    assertTrue((boolean) methodHandle.invoke(new Object[] { "java", "java" }));
    assertFalse((boolean) methodHandle.invoke(new Object[] { "java", "jova" }));
}
 
Example 3
@Override
public void bind(Context ctx) throws IllegalAccessException {
    log.debug("bind %s to context", method);
    MethodHandle boundHandle = MethodHandles.publicLookup().unreflect(method).bindTo(ctx.getProcessor(processorClass));
    if (hasContextParameter()) {
        boundHandle = boundHandle.bindTo(ctx);
    }
    this.methodHandle = boundHandle.asSpreader(Object[].class, arity);
}
 
Example 4
@Test
public static void testAsSpreaderExample() throws Throwable {
    // test the JavaDoc asSpreader-with-pos example
    MethodHandle compare = LOOKUP.findStatic(Objects.class, "compare", methodType(int.class, Object.class, Object.class, Comparator.class));
    MethodHandle compare2FromArray = compare.asSpreader(0, Object[].class, 2);
    Object[] ints = new Object[]{3, 9, 7, 7};
    Comparator<Integer> cmp = (a, b) -> a - b;
    assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 0, 2), cmp) < 0);
    assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 1, 3), cmp) > 0);
    assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 2, 4), cmp) == 0);
}
 
Example 5
@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);
    }
}
 
Example 6
@Override
public MethodHandle asSpreader(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
    return handle.asSpreader(arrayType, arrayLength);
}
 
Example 7
@Override
public MethodHandle asSpreader(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
    final MethodHandle mh = handle.asSpreader(arrayType, arrayLength);
    return debug(mh, "asSpreader", handle, arrayType, arrayLength);
}
 
Example 8
@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);
    }
}
 
Example 9
@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);
    }
}
 
Example 10
@Override
public MethodHandle asSpreader(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
    final MethodHandle mh = handle.asSpreader(arrayType, arrayLength);
    return debug(mh, "asSpreader", handle, arrayType, arrayLength);
}
 
Example 11
@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);
    }
}
 
Example 12
Source Project: dart2java   File: DynamicHelper.java    License: Apache License 2.0 4 votes vote down vote up
public static Object invoke(String methodName, Object... recvAndArgs) {
  // TODO(springerm): Proper exception handling
  // TODO(springerm): Need to do Dart type checks for arguments
  // TODO(springerm): Special case methods defined on DartObject

  try {
    MethodHandles.Lookup lookup = MethodHandles.publicLookup();
    Object receiver = recvAndArgs[0];
    Class<?> receiverClass = receiver.getClass();
    boolean checkForOverloads = false;

    // TODO(springerm): Handle static methods properly
    // The index of the first argument. When calling a static method, the
    // first argument starts at index 0, because no "self" object is passed
    int startArgs = 1;

    if (classImpls.containsKey(receiverClass)) {
      receiverClass = classImpls.get(receiverClass);

      // There may be multiple overloadings for operators, we have to
      // pick the correct one
      checkForOverloads = true;
      startArgs = 0;
    } else if (classesWithJavaInterfaces.contains(receiverClass)) {
      checkForOverloads = true;
    }

    // TODO(springerm): Try to use MethodHandles API for lookup
    // TODO(springerm): Look into byte code generation for invokedynamic
    Method method = null;
    
    methodLoop: 
    for (Method m : receiverClass.getMethods()) {
      if (m.getName().equals(methodName)) {
        if (checkForOverloads) {
          Class<?>[] parameterTypes = m.getParameterTypes();

          // Check if the number of parameters match
          if (parameterTypes.length != recvAndArgs.length - startArgs) {
            continue methodLoop;
          }

          // Check if the parameter types for this method match
          for (int i = startArgs; i < parameterTypes.length; i++) {
            if (!parameterTypes[i - startArgs].isAssignableFrom(
              boxedToMaybeUnboxedType(recvAndArgs[i].getClass()))
              && !parameterTypes[i - startArgs].isAssignableFrom(
              recvAndArgs[i].getClass())) {
              // Picked the wrong method, continue searching
              continue methodLoop;
            }
          }
        }

        // Found matching method
        method = m;
        break methodLoop;
      }
    }

    if (method == null) {
      String argTypes = "";
      for (Object o : recvAndArgs) {
        argTypes = argTypes + o.getClass().getName() + ", ";
      }
      throw new NoSuchMethodException(
        receiverClass.getName() + "." + methodName 
          + " (" + argTypes + ") not found");
    }

    MethodHandle handle = lookup.unreflect(method);
    MethodHandle handleSpreader = handle.asSpreader(
      Object[].class, recvAndArgs.length);
    Object result = handleSpreader.invoke(recvAndArgs);

    return result;
  } catch (Throwable t) {
    throw new RuntimeException(t);
  }
}
 
Example 13
@Override
public MethodHandle asSpreader(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
    final MethodHandle mh = handle.asSpreader(arrayType, arrayLength);
    return debug(mh, "asSpreader", handle, arrayType, arrayLength);
}
 
Example 14
@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);
    }
}
 
Example 15
@Override
public MethodHandle asSpreader(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
    final MethodHandle mh = handle.asSpreader(arrayType, arrayLength);
    return debug(mh, "asSpreader", handle, arrayType, arrayLength);
}
 
Example 16
@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);
    }
}
 
Example 17
@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);
    }
}
 
Example 18
@Override
public MethodHandle asSpreader(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
    final MethodHandle mh = handle.asSpreader(arrayType, arrayLength);
    return debug(mh, "asSpreader", handle, arrayType, arrayLength);
}
 
Example 19
@Override
public MethodHandle asSpreader(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
    final MethodHandle mh = handle.asSpreader(arrayType, arrayLength);
    return debug(mh, "asSpreader", handle, arrayType, arrayLength);
}
 
Example 20
@Override
public MethodHandle asSpreader(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
    final MethodHandle mh = handle.asSpreader(arrayType, arrayLength);
    return debug(mh, "asSpreader", handle, arrayType, arrayLength);
}