Udostępnij za pośrednictwem


MethodHandle.AsCollector Method

Definition

Overloads

AsCollector(Class, Int32)

Makes an <em>array-collecting</em> method handle, which accepts a given number of trailing positional arguments and collects them into an array argument.

AsCollector(Int32, Class, Int32)

Makes an <em>array-collecting</em> method handle, which accepts a given number of positional arguments starting at a given position, and collects them into an array argument.

AsCollector(Class, Int32)

Makes an <em>array-collecting</em> method handle, which accepts a given number of trailing positional arguments and collects them into an array argument.

[Android.Runtime.Register("asCollector", "(Ljava/lang/Class;I)Ljava/lang/invoke/MethodHandle;", "GetAsCollector_Ljava_lang_Class_IHandler", ApiSince=26)]
public virtual Java.Lang.Invoke.MethodHandle? AsCollector (Java.Lang.Class? arrayType, int arrayLength);
[<Android.Runtime.Register("asCollector", "(Ljava/lang/Class;I)Ljava/lang/invoke/MethodHandle;", "GetAsCollector_Ljava_lang_Class_IHandler", ApiSince=26)>]
abstract member AsCollector : Java.Lang.Class * int -> Java.Lang.Invoke.MethodHandle
override this.AsCollector : Java.Lang.Class * int -> Java.Lang.Invoke.MethodHandle

Parameters

arrayType
Class

often Object[], the type of the array argument which will collect the arguments

arrayLength
Int32

the number of arguments to collect into a new array argument

Returns

a new method handle which collects some trailing argument into an array, before calling the original method handle

Attributes

Remarks

Makes an <em>array-collecting</em> method handle, which accepts a given number of trailing positional arguments and collects them into an array argument. The new method handle adapts, as its target, the current method handle. The type of the adapter will be the same as the type of the target, except that a single trailing parameter (usually of type arrayType) is replaced by arrayLength parameters whose type is element type of arrayType.

If the array type differs from the final argument type on the original target, the original target is adapted to take the array type directly, as if by a call to #asType asType.

When called, the adapter replaces its trailing arrayLength arguments by a single new array of type arrayType, whose elements comprise (in order) the replaced arguments. Finally the target is called. What the target eventually returns is returned unchanged by the adapter.

(The array may also be a shared constant when arrayLength is zero.)

(<em>Note:</em> The arrayType is often identical to the last parameter type of the original target. It is an explicit argument for symmetry with asSpreader, and also to allow the target to use a simple Object as its last parameter type.)

In order to create a collecting adapter which is not restricted to a particular number of collected arguments, use #asVarargsCollector asVarargsCollector instead.

Here are some examples of array-collecting method handles: <blockquote>

{@code
            MethodHandle deepToString = publicLookup()
              .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class));
            assertEquals("[won]",   (String) deepToString.invokeExact(new Object[]{"won"}));
            MethodHandle ts1 = deepToString.asCollector(Object[].class, 1);
            assertEquals(methodType(String.class, Object.class), ts1.type());
            //assertEquals("[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));
            }

</blockquote>

Java documentation for java.lang.invoke.MethodHandle.asCollector(java.lang.Class<?>, int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

AsCollector(Int32, Class, Int32)

Makes an <em>array-collecting</em> method handle, which accepts a given number of positional arguments starting at a given position, and collects them into an array argument.

[Android.Runtime.Register("asCollector", "(ILjava/lang/Class;I)Ljava/lang/invoke/MethodHandle;", "GetAsCollector_ILjava_lang_Class_IHandler", ApiSince=33)]
public virtual Java.Lang.Invoke.MethodHandle? AsCollector (int collectArgPos, Java.Lang.Class? arrayType, int arrayLength);
[<Android.Runtime.Register("asCollector", "(ILjava/lang/Class;I)Ljava/lang/invoke/MethodHandle;", "GetAsCollector_ILjava_lang_Class_IHandler", ApiSince=33)>]
abstract member AsCollector : int * Java.Lang.Class * int -> Java.Lang.Invoke.MethodHandle
override this.AsCollector : int * Java.Lang.Class * int -> Java.Lang.Invoke.MethodHandle

Parameters

collectArgPos
Int32

the zero-based position in the parameter list at which to start collecting.

arrayType
Class

often Object[], the type of the array argument which will collect the arguments

arrayLength
Int32

the number of arguments to collect into a new array argument

Returns

a new method handle which collects some arguments into an array, before calling the original method handle

Attributes

Remarks

Makes an <em>array-collecting</em> method handle, which accepts a given number of positional arguments starting at a given position, and collects them into an array argument. The new method handle adapts, as its target, the current method handle. The type of the adapter will be the same as the type of the target, except that the parameter at the position indicated by collectArgPos (usually of type arrayType) is replaced by arrayLength parameters whose type is element type of arrayType.

This method behaves very much like #asCollector(Class, int), but differs in that its collectArgPos argument indicates at which position in the parameter list arguments should be collected. This index is zero-based.

Added in 9.

Java documentation for java.lang.invoke.MethodHandle.asCollector(int, java.lang.Class<?>, int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to