tEquals("[won]", (String) ts1.invokeExact( new Object[]{"won"})); //FAIL assertEquals("[[won]]", (String) ts1.invokeExact((Object) new Object[]{"won"})); // arrayType can be a subtype of Object[] MethodHandle ts2 = deepToString.asCollector(String[].class, 2); assertEquals(methodType(String.class, String.class, String.class), ts2.type()); assertEquals("[two, too]", (String) ts2.invokeExact("two", "too")); MethodHandle ts0 = deepToString.asCollector(Object[].class, 0); assertEquals("[]", (String) ts0.invokeExact()); // collectors can be nested, Lisp-style MethodHandle ts22 = deepToString.asCollector(Object[].class, 3).asCollector(String[].class, 2); assertEquals("[A, B, [C, D]]", ((String) ts22.invokeExact((Object)'A', (Object)"B", "C", "D"))); // arrayType can be any primitive array type MethodHandle bytesToString = publicLookup() .findStatic(Arrays.class, "toString", methodType(String.class, byte[].class)) .asCollector(byte[].class, 3); assertEquals("[1, 2, 3]", (String) bytesToString.invokeExact((byte)1, (byte)2, (byte)3)); MethodHandle longsToString = publicLookup() .findStatic(Arrays.class, "toString", methodType(String.class, long[].class)) .asCollector(long[].class, 1); assertEquals("[123]", (String) longsToString.invokeExact((long)123)); }
@param arrayType often {@code Object[]}, the type of the array argument which will collect the arguments
@param arrayLength the number of arguments to collect into a new array argument
@return a new method handle which collects some trailing argumentinto an array, before calling the original method handle
@throws NullPointerException if {@code arrayType} is a null reference
@throws IllegalArgumentException if {@code arrayType} is not an array typeor {@code arrayType} is not assignable to this method handle's trailing parameter type,or {@code arrayLength} is not a legal array size,or the resulting method handle's type would have
too many parameters
@throws WrongMethodTypeException if the implied {@code asType} call fails
@see #asSpreader
@see #asVarargsCollector