Skip to content

Commit

Permalink
Prefer Java 0-param methods over methods with a single vararg param
Browse files Browse the repository at this point in the history
  • Loading branch information
mattco98 committed Jan 1, 2024
1 parent dcbb729 commit b5171ce
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/org/mozilla/javascript/NativeJavaMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,19 @@ private static int preferSignature(Object[] args,
boolean vararg1,
Class<?>[] sig2,
boolean vararg2) {
// In the case where there is a zero-arg method and a method with a single vararg
// parameter, the zero-arg method should be preferred over the vararg method since,
// while the vararg method can be called in Java with no arguments, in reality it
// has a single argument: T[].
if (args.length == 0) {
if (vararg1 == vararg2)
return PREFERENCE_EQUAL;
if (vararg1 && sig2.length == 0)
return PREFERENCE_SECOND_ARG;
if (vararg2 && sig1.length == 0)
return PREFERENCE_FIRST_ARG;
}

int totalPreference = 0;
for (int j = 0; j < args.length; j++) {
Class<?> type1 = vararg1 && j >= sig1.length ? sig1[sig1.length - 1] : sig1[j];
Expand Down

0 comments on commit b5171ce

Please sign in to comment.