共用方式為


MethodHandle.AsFixedArity Method

Definition

Makes a <em>fixed arity</em> method handle which is otherwise equivalent to the current method handle.

[Android.Runtime.Register("asFixedArity", "()Ljava/lang/invoke/MethodHandle;", "GetAsFixedArityHandler", ApiSince=26)]
public virtual Java.Lang.Invoke.MethodHandle? AsFixedArity ();
[<Android.Runtime.Register("asFixedArity", "()Ljava/lang/invoke/MethodHandle;", "GetAsFixedArityHandler", ApiSince=26)>]
abstract member AsFixedArity : unit -> Java.Lang.Invoke.MethodHandle
override this.AsFixedArity : unit -> Java.Lang.Invoke.MethodHandle

Returns

a new method handle which accepts only a fixed number of arguments

Attributes

Remarks

Makes a <em>fixed arity</em> method handle which is otherwise equivalent to the current method handle.

If the current method handle is not of #asVarargsCollector variable arity, the current method handle is returned. This is true even if the current method handle could not be a valid input to asVarargsCollector.

Otherwise, the resulting fixed-arity method handle has the same type and behavior of the current method handle, except that #isVarargsCollector isVarargsCollector will be false. The fixed-arity method handle may (or may not) be the a previous argument to asVarargsCollector.

Here is an example, of a list-making variable arity method handle: <blockquote>

{@code
            MethodHandle asListVar = publicLookup()
              .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class))
              .asVarargsCollector(Object[].class);
            MethodHandle asListFix = asListVar.asFixedArity();
            assertEquals("[1]", asListVar.invoke(1).toString());
            Exception caught = null;
            try { asListFix.invoke((Object)1); }
            catch (Exception ex) { caught = ex; }
            assert(caught instanceof ClassCastException);
            assertEquals("[two, too]", asListVar.invoke("two", "too").toString());
            try { asListFix.invoke("two", "too"); }
            catch (Exception ex) { caught = ex; }
            assert(caught instanceof WrongMethodTypeException);
            Object[] argv = { "three", "thee", "tee" };
            assertEquals("[three, thee, tee]", asListVar.invoke(argv).toString());
            assertEquals("[three, thee, tee]", asListFix.invoke(argv).toString());
            assertEquals(1, ((List) asListVar.invoke((Object)argv)).size());
            assertEquals("[three, thee, tee]", asListFix.invoke((Object)argv).toString());
            }

</blockquote>

Java documentation for java.lang.invoke.MethodHandle.asFixedArity().

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