dyn:callMethod and varargs
Attila Szegedi
attila.szegedi at oracle.com
Wed Mar 13 13:32:56 PDT 2013
Dynalink doesn't support that for arbitrary Java methods, only if the Java method on the receiving end is itself variable arity. Basically, the static signature at the call site should be "similar" to the invoked method. This is actually a property of a linker - NashornLinker tipically allows any JS function to be invoked as vararg, and Dynalink's BeansLinker does not. A request for a vararg invoker has been articulated by others too - notably, Groovy would need it for their "splat" invocation. I'm thinking of introducing a separate operation "dyn:callVarArg" for this purpose. I don't want to rush a half-baked solution though so I'm gathering requirements for now.
What'd be your use case? Out of curiosity, how are you using Dynalink?
Attila.
Sent from my iPad
On 2013.03.13., at 18:39, Peter Zhelezniakov <Peter.Zhelezniakov at oracle.com> wrote:
> Hello,
>
> I'm looking for a way to pass variable-length list of arguments into a
> Dynalink method handle. E.g. to implement a method like
> call(String methodName, Object obj, Object... args)
>
> I'm trying
> Class[] argTypes = new Class[args.length + 1];
> Arrays.fill(argTypes, Object.class);
> MethodHandle function = Bootstrap.createDynamicInvoker(
> "dyn:callMethod:" + methodName,
> Object.class, argTypes);
> Object o = function.invokeExact(obj, args);
>
> and getting an exception:
>
> java.lang.invoke.WrongMethodTypeException: expected
> (Object,Object,Object)Object but found (Object,Object[])Object
>
> What else might I try?
> Thanks!
> --
> Peter
More information about the nashorn-dev
mailing list