public FunctionReference asFixedArity() { return new FunctionReference(handle.asFixedArity(), this.parameterNames); }
/** Appends varargs collector as appropriate to the MethodHandles lookup. * The return pushes the cascade chaining result to the parent. * {@inheritDoc} */ @Override protected MethodHandle lookup(MethodHandle m) { if (isVarArgs() && null != m) return m.asFixedArity().asVarargsCollector(getVarArgsType()); return m; }
public MethodHandle coerce(MethodHandle target) { if (target.isVarargsCollector() && isLastArgumentAnArray()) { return target.asFixedArity().asType(type); } return target.asType(type); }
OverloadedMethod(List<MethodHandle> methodHandles, OverloadedDynamicMethod parent, MethodType callSiteType, LinkerServices linkerServices) { this.parent = parent; this.callSiteType = callSiteType; this.linkerServices = linkerServices; fixArgMethods = new ArrayList<>(methodHandles.size()); varArgMethods = new ArrayList<>(methodHandles.size()); final int argNum = callSiteType.parameterCount(); for(MethodHandle mh: methodHandles) { if(mh.isVarargsCollector()) { final MethodHandle asFixed = mh.asFixedArity(); if(argNum == asFixed.type().parameterCount()) { fixArgMethods.add(asFixed); } varArgMethods.add(mh); } else { fixArgMethods.add(mh); } } fixArgMethods.trimToSize(); varArgMethods.trimToSize(); final MethodHandle bound = SELECT_METHOD.bindTo(this); final MethodHandle collecting = SingleDynamicMethod.collectArguments(bound, argNum).asType( callSiteType.changeReturnType(MethodHandle.class)); invoker = MethodHandles.foldArguments(MethodHandles.exactInvoker(callSiteType), collecting); }
OverloadedMethod(final List<MethodHandle> methodHandles, final OverloadedDynamicMethod parent, final MethodType callSiteType, final LinkerServices linkerServices) { this.parent = parent; final Class<?> commonRetType = getCommonReturnType(methodHandles); this.callSiteType = callSiteType.changeReturnType(commonRetType); this.linkerServices = linkerServices; fixArgMethods = new ArrayList<>(methodHandles.size()); varArgMethods = new ArrayList<>(methodHandles.size()); final int argNum = callSiteType.parameterCount(); for(MethodHandle mh: methodHandles) { if(mh.isVarargsCollector()) { final MethodHandle asFixed = mh.asFixedArity(); if(argNum == asFixed.type().parameterCount()) { fixArgMethods.add(asFixed); } varArgMethods.add(mh); } else { fixArgMethods.add(mh); } } fixArgMethods.trimToSize(); varArgMethods.trimToSize(); final MethodHandle bound = SELECT_METHOD.bindTo(this); final MethodHandle collecting = SingleDynamicMethod.collectArguments(bound, argNum).asType( callSiteType.changeReturnType(MethodHandle.class)); invoker = linkerServices.asTypeLosslessReturn(MethodHandles.foldArguments( MethodHandles.exactInvoker(this.callSiteType), collecting), callSiteType); }
handle = handle.asFixedArity();
type = type.changeReturnType(void.class); mh = mh.asFixedArity(); mh = mh.asType(type);
/** * Spread arguments over this function parameters. * * @param arguments arguments as an array. * @return a return value. * @throws Throwable ...because an exception can be thrown. */ public Object spread(Object... arguments) throws Throwable { int arity = arity(); if (this.handle.isVarargsCollector() && (arity > 0) && (arguments[arity - 1] instanceof Object[])) { return this.handle .asFixedArity() .asSpreader(Object[].class, arguments.length) .invoke(arguments); } return this.handle .asSpreader(Object[].class, arguments.length) .invoke(arguments); }
handle = handle.asFixedArity().asType(type); } else { handle = handle.asType(type); types = constructor.getParameterTypes(); if (constructor.isVarArgs() && TypeMatching.isLastArgumentAnArray(types.length, args)) { handle = caller.unreflectConstructor(constructor).asFixedArity().asType(type); } else { handle = caller.unreflectConstructor(constructor).asType(type);
final int paramsLen = methodType.parameterCount(); final boolean varArgs = target.isVarargsCollector(); final MethodHandle fixTarget = varArgs ? target.asFixedArity() : target; final int fixParamsLen = varArgs ? paramsLen - 1 : paramsLen; final int argsLen = callSiteType.parameterCount();
final int paramsLen = methodType.parameterCount(); final boolean varArgs = target.isVarargsCollector(); final MethodHandle fixTarget = varArgs ? filteredTarget.asFixedArity() : filteredTarget; final int fixParamsLen = varArgs ? paramsLen - 1 : paramsLen; final int argsLen = callSiteType.parameterCount();
invoker = invoker.asFixedArity().asType(callSite.type()); } else { invoker = invoker.asCollector(