diff --git a/js/rhino/src/org/mozilla/javascript/NativeJavaClass.java b/js/rhino/src/org/mozilla/javascript/NativeJavaClass.java index 7de2c46dc28f..9804c61a29af 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaClass.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaClass.java @@ -173,6 +173,7 @@ public class NativeJavaClass extends NativeJavaObject implements Function { Modifier.isAbstract(modifiers))) { Constructor[] ctors = members.getConstructors(); + args = NativeJavaMethod.unwrapArgs(args); Member member = NativeJavaMethod.findFunction(ctors, args); Constructor ctor = (Constructor) member; if (ctor == null) { diff --git a/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java b/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java index 728cb86acc46..a8d5eedb6124 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java @@ -189,7 +189,8 @@ public class NativeJavaMethod extends NativeFunction implements Function { if (methods.length == 0) { throw new RuntimeException("No methods defined for call"); } - + Object[] origArgs = args; + args = unwrapArgs(args); Method meth = (Method) findFunction(methods, args); if (meth == null) { Class c = methods[0].getDeclaringClass(); @@ -204,7 +205,15 @@ public class NativeJavaMethod extends NativeFunction implements Function { // First, we marshall the args. for (int i = 0; i < args.length; i++) { - args[i] = NativeJavaObject.coerceType(paramTypes[i], args[i], true); + Object arg = args[i]; + Object coerced = NativeJavaObject.coerceType(paramTypes[i], arg, + true); + if (coerced != arg) { + if (origArgs == args) { + args = (Object[])args.clone(); + } + args[i] = coerced; + } } Object javaObject; if (Modifier.isStatic(meth.getModifiers())) { @@ -311,6 +320,32 @@ public class NativeJavaMethod extends NativeFunction implements Function { throw illegalAccess; } + /** + * If args arrray contains instances of {@link Wrapper}, return a new array + * with all such objects unwrapped, otherwise return the original array. + */ + static Object[] unwrapArgs(Object[] args) + { + Object[] result = args; + for (int i = 0, N = args.length; i != N; ++i) { + Object arg = args[i]; + if (arg instanceof Wrapper) { + Object unwrapped = ((Wrapper)arg).unwrap(); + if (!(unwrapped instanceof Number)) { + // Since numbers are internally represented as + // java.lang.Double, etc. then java.lang.Doubles are + // distinquished by being wrapped. Thus don't unwrap + // here or we'll get overloading wrong. + if (result == args) { + result = (Object[])args.clone(); + } + result[i] = unwrapped; + } + } + } + return result; + } + /** * Find the correct function to call given the set of methods * or constructors and the arguments. @@ -320,20 +355,6 @@ public class NativeJavaMethod extends NativeFunction implements Function { if (methodsOrCtors.length == 0) return null; boolean hasMethods = methodsOrCtors[0] instanceof Method; - // Wrapper support - for (int i=0; i < args.length; i++) { - Object arg = args[i]; - if (arg instanceof Wrapper) { - arg = ((Wrapper)arg).unwrap(); - if (!(arg instanceof Number)) { - // Since numbers are internally represented as - // java.lang.Double, etc. then java.lang.Doubles are - // distinquished by being wrapped. Thus don't unwrap - // here or we'll get overloading wrong. - args[i] = arg; - } - } - } Member bestFit = null; Class[] bestFitTypes = null;