hi Norris,

in our product, which makes heavy use of Rhino, we have many Java Objects
we wrap with ECMAScript wrappers, which extend the ScriptableObject class
and implement the Wrapper interface. Those wrappers automagically wrap the
native Java object with the help of a WrapHandler implementation.

we now ran into a problem :

we have a java class with two overloaded static methods like this :
     public class Test {
         public static String create(File f) {}
         public static String create(Custom c) {}
     }

The Custom class exists as a native Java implementation like
     public class Custom {}

and a accompanying ECMAScript wrapper like
     public class CustomWrapper
        extends ScriptableObject
        implements Wrapper {}

in our ECMAScripts we make the wrapper class known as a host object along
the lines of
     defineClass("CustomWrapper");
and can then use the object as a normal ECMAScript host object. no big deal
and working great.

but : the code
     var s = Test.creat( new Custom( "xyz") );
fails with the information, that the methods are ambiguous, which of course
they are not.

Looking at the code of NativeJavaMethod.findFunction() and the helpers in
NativeJavaObject it seems, that the fact of the Custom host object being a
Wrapper is not taken into account. in an easy fix of
NativeJavaMethod.findFunction(), i simply replace all arguments, which are
Wrapper imlpementation by the wrapped object. this solves my problem, but
of course i'm not sure on side effects.

i attach the testcase as well as the fixed NativeJavaMethod class in the
jar file. to run the test with and without the fix, extract the jar and do
     ant test

please let me know, what you think of this.

regards and thanks, f.

Felix Meschberger
This commit is contained in:
nboyd%atg.com 2002-04-11 12:56:24 +00:00
Родитель 887959238b
Коммит 69e288aec4
1 изменённых файлов: 15 добавлений и 0 удалений

Просмотреть файл

@ -335,6 +335,21 @@ public class NativeJavaMethod extends NativeFunction implements Function {
catch (IllegalAccessException e) {
// Just abandon conversion from JSObject
}
} else {
// 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;