|
Quick Lists
|
|
Bug ID:
|
6512123
|
|
Votes
|
0
|
|
Synopsis
|
Function to method resolution is broken
|
|
Category
|
scripting:javascript
|
|
Reported Against
|
|
|
Release Fixed
|
|
|
State
|
4-Defer,
bug
|
|
Priority:
|
4-Low
|
|
Related Bugs
|
|
|
Submit Date
|
11-JAN-2007
|
|
Description
|
FULL PRODUCT VERSION :
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
customer Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
If a static method with the same name of an instance method is exposed, it is not possible to invoke it from script at all. This severely limits the ability to invoke Java methods from JavaScript.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the following program:
package test;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class FunctionsClass {
public static boolean xpto(Object arg1, Object arg2) {
return equals(arg1, arg2);
}
public static boolean equals(Object arg1, Object arg2) {
return arg1 == null ? arg2 == null : arg1.equals(arg2);
}
public static Object echo(Object o) {
return o;
}
public static void main(String[] args) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
engine.put("f", new FunctionsClass());
System.out.println(engine.eval("f.echo(f)"));
System.out.println(engine.eval("f.xpto(null, null)"));
System.out.println(engine.eval("f.equals('a', 'b')"));
}
}
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
xxxxx@xxxxx
true
true
ACTUAL -
xxxxx@xxxxx
true
Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: Can't find method java.lang.Object.equals(string,string). (<Unknown source>#1) in <Unknown source> at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:124)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at test.FunctionsClass.main(FunctionsClass.java:24)
Java Result: 1
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
Listed above on steps to reproduce
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Try to avoid the name clash or turn the method into an instance method if you control the class being exposed
Posted Date : 2007-01-11 11:12:27.0
|
|
Work Around
|
N/A
|
|
Evaluation
|
Two observations:
Observation 1: This is not an issue with javax.script implementation on top of Rhino. This can be seen by running Rhino's shell [which comes with Mozilla Rhino download] - avoiding the jsr-223 API cover. Using Rhino 1.6R2 [which is the version bundled with JDK 6], I got the following result:
D:\rhino1_6R2>java -cp js.jar;. org.mozilla.javascript.tools.shell.Main
Rhino 1.6 release 2 2005 09 19
js> p = new Packages.FunctionsClass()
xxxxx@xxxxx
js> p.equals("x", "x")
js: "<stdin>", line 3: Can't find method java.lang.Object.equals(string,string).
js: "<stdin>", line 3: org.mozilla.javascript.EvaluatorException: Can't find met
hod java.lang.Object.equals(string,string). (<stdin>#3)
js> Packages.FunctionsClass.equals('x', 'x')
true
js>
Clearly, Rhino itself does not find the static method when calling through instance. So, it is an issue with javax.script API implementation on top of Rhino.
Observation 2: I am not sure why this should to be treated as a bug. I think this is a design choice by Rhino. Please refer to http://www.mozilla.org/js/liveconnect/lc3_method_overloading.html
-- see section 3.1 titled "Method Accessibility and Applicability" - bullet item 2.
Besides, even with Java, calling static methods through an instance (although allowed) is not a good idea anyway.
Posted Date : 2007-01-12 10:33:01.0
Sorry, in my last evaluation, I wrote:
"Clearly, Rhino itself does not find the static method when calling through instance. So, it is an issue with javax.script API implementation on top of Rhino."
That should have been
"Clearly, Rhino itself does not find the static method when calling through instance. So, it is *not* an issue with javax.script API implementation on top of Rhino."
*not* was missing!
Posted Date : 2007-01-12 12:17:56.0
|
|
Comments
|
PLEASE NOTE: JDK6 is formerly known as Project Mustang
|
|
|
 |