Java method lookup fails for methods with Scriptable argument if argument is a subclass

RESOLVED FIXED

Status

RESOLVED FIXED
10 years ago
9 years ago

People

(Reporter: hannesw, Unassigned)

Tracking

Details

Attachments

(3 attachments)

(Reporter)

Description

10 years ago
User-Agent:       Mozilla/5.0 (X11; U; Linux i686; de; rv:1.9.0.1) Gecko/2008072820 Firefox/3.0.1
Build Identifier: 

The method lookup in NativeJavaObject fails if the target method takes a Scriptable argument and the actual argument is a subclass of the formal parameter.

js> org.mozilla.javascript.Context.currentContext.initStandardObjects({})
js: "<stdin>", line 3: Can't find method org.mozilla.javascript.Context.initStandardObjects(object).
	at <stdin>:3

Reproducible: Always
(Reporter)

Comment 1

10 years ago
Created attachment 339939 [details] [diff] [review]
fix

Use Class.isInstance() instead of ==
(Reporter)

Comment 2

10 years ago
Committing the patch.

Checking in src/org/mozilla/javascript/NativeJavaObject.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java,v  <--  NativeJavaObject.java
new revision: 1.86; previous revision: 1.85
done
Status: UNCONFIRMED → RESOLVED
Last Resolved: 10 years ago
Resolution: --- → FIXED
(Reporter)

Comment 3

10 years ago
Created attachment 368231 [details] [diff] [review]
Prevent previous patch from applying to java.lang.Object arguments

The previous patch causes passing a Scriptable arg to a java.lang.Object parameter to get a conversion weight 1:

js> var sdf = new java.text.SimpleDateFormat();
js> sdf.format(new Date());
js: "<stdin>", line 3: The choice of Java constructor format matching
JavaScript argument types (object) is ambiguous; candidate
constructors are:
    class java.lang.String format(java.lang.Object)
    class java.lang.String format(java.util.Date) 

This patch additionally checks if the formal parameter is actually Scriptable or an class implementing Scriptable.
(Reporter)

Comment 4

10 years ago
I committed the second patch to both Rhino1_7R2_BRANCH and HEAD.
(Reporter)

Comment 5

9 years ago
Created attachment 397616 [details] [diff] [review]
Allow Scriptable objects to be used as Java method parameters of classes they implement

The second patch was too broad. It prevented objects implementing Scriptable to be used for parameters of any other interface or superclass they may implement/extend. 

For example, org.mozilla.javascript.ScriptableObject now implements java.util.Map, but calling new java.util.HashMap({foo: "bar"}) fails because of this.

With the new patch, all formal parameter interfaces/classes that are directly implemented by the Scriptable instance produce a conversion weight of 1, except java.lang.Object, which produces a conversion weight of 2 (which is I think the intended behaviour).
(Reporter)

Comment 6

9 years ago
I committed the new patch.

Checking in src/org/mozilla/javascript/NativeJavaObject.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java,v  <--  NativeJavaObject.java
new revision: 1.89; previous revision: 1.88
done
You need to log in before you can comment on or make changes to this bug.