Non deterministic behaviour with vararg methods #1433
+84
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
If there are two methods, one with varargs and an other without, for example
String args(String arg1, String... args)
String args(String arg1)
rhino considers these two methods as
PREFERENCE_EQUAL
when it is invoked withargs('foo')
from javascript. This may result in a non deterministic behaviour as one of the two was taken.Details
In the case described above, all methods are compared and it is tried to find the best matchin one. So after
preferSignature
we will run into this code part inNativeJavaMethod
In our application, where the bug occured, we run in the
Ignoring same signature member
code path and one (not really deterministic - because of HashMap) method was taken. So the code sometimes works and sometimes not.It was also not easy to provide a deterministic failing test, that's why (and maybe to add more determinism) I've changed the map in
discoverAccessibleMethods
to a LinkedHashMap (and yet the test depends heavily on the order of the JVM'sgetDeclaredMethods
implementation)Fix
When there are two candidates, the no-vararg method is taken. This is the same behaviour, as it is in Java.