You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> var Arrays = java.import('java.util.Arrays');
> var list = Arrays.asList(['a', 'b']);
> list.toArray();
[ [ 'a', 'b' ] ] // should be ['a', 'b'] !!
I haven't looked at the varargs handling code, but I'm assuming it's because of the ambiguity due to the asList method being generic (List<T> asList(T... a)) - there's no way to distinguish if the javascript Array being passed into Arrays.asList() is intended as a single value of type T or if it's intended as a T[].
Assuming that's the issue, we could detect this ambiguity and throw an error, requiring the javascript programmer to use apply when they want a javascript array to be 'spread' into a varargs generic function.
Does this make sense?
(Btw, ES6 compiled by babel works just fine here with Arrays.asList(...arr);).
You are probably correct. We rely on a fork of commons-lang to find which method matches using MethodUtils#getMatchingAccessibleMethod. We have already patched some of the ambiguity issues that commons-lang was not handling this is just another missing case. When I have some time I'll take a look.
Here's my suggestion for the desired behavior: the javascript call Arrays.asList([a, b, c]) would result in the underlying Java call Arrays.asList(a, b, c).
If you want a single array arg to be passed to the java method, then you'd do Arrays.asList([[a, b, c]]), which results in the underlying Java call Arrays.asList([a, b, c]).
Based on the little experiment below, I'd say the existing behavior is correct if we want to make things the same as Java. So this is not a bug, though we might want to add something to the README to explain this behavior.
Activity
joeferner commentedon Dec 3, 2015
You are probably correct. We rely on a fork of commons-lang to find which method matches using
MethodUtils#getMatchingAccessibleMethod. We have already patched some of the ambiguity issues that commons-lang was not handling this is just another missing case. When I have some time I'll take a look.henridf commentedon Dec 3, 2015
I'm happy to work on this if that's helpful -- let me know.
joeferner commentedon Dec 3, 2015
Absolutely that would be great.
henridf commentedon Dec 3, 2015
Here's my suggestion for the desired behavior: the javascript call
Arrays.asList([a, b, c])would result in the underlying Java callArrays.asList(a, b, c).If you want a single array arg to be passed to the java method, then you'd do
Arrays.asList([[a, b, c]]), which results in the underlying Java callArrays.asList([a, b, c]).Does this behavior make sense?
joeferner commentedon Dec 4, 2015
Can we make the rules the same as Java? How does it handle these cases?
henridf commentedon Dec 7, 2015
Based on the little experiment below, I'd say the existing behavior is correct if we want to make things the same as Java. So this is not a bug, though we might want to add something to the README to explain this behavior.
joeferner commentedon Dec 7, 2015
35edc9b