If the current method handle is not of {@linkplain #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 {@code asVarargsCollector}.
Otherwise, the resulting fixed-arity method handle has the same type and behavior of the current method handle, except that {@link #isVarargsCollector isVarargsCollector}will be false. The fixed-arity method handle may (or may not) be the a previous argument to {@code asVarargsCollector}.
Here is an example, of a list-making variable arity method handle:
@return a new method handle which accepts only a fixed number of arguments @see #asVarargsCollector @see #isVarargsCollector{@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()); }
|
|
|
|