Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

javax.script, convert Boolean to JS Boolean and back, Rhino

807591Apr 9 2008
Hi.
I'm trying javax.script. I created simple class for comparing time with methods like:
...
public Boolean eq(Object o1, Object o2) {  // o1 and o2 can be java.util.Date or org.joda.time.DateTime
  // return true if dates are equal
}

public Boolean gt(Object o1, Object o2) {  // o1 and o2 can be java.util.Date or org.joda.time.DateTime
  // return true if date o1 is after o2
}

public DateTime create(Integer year, Integer month, Integer day) {
  // return joda DateTime instance
}
...
and put it to script engine. When I do:
engine.eval("var result = (1 == 1)");
return (Boolean) engine.get("result");
everything is OK, result is Boolean object. However if I do:
engine.eval("var result = (date.eq(date.create(2000, 1, 1), date.create(2000, 1, 1))");  // I stored date object under name "date"
return (Boolean) engine.get("result");
i get exception:
java.lang.ClassCastException: sun.org.mozilla.javascript.internal.NativeJavaObject cannot be cast to java.lang.Boolean

Currently I'm using Rhino as underlying script engine and I don't know if I'll have to change it in future so I want to stick with "general" Java scripting API. I can't find any way how to wrap some Java objects to currently used engine representation. The last change I see in static method from Rhino API
[org/mozilla/javascript/Context.html#javaToJS(java.lang.Object, org.mozilla.javascript.Scriptable)|http://www.mozilla.org/rhino/apidocs/org/mozilla/javascript/Context.html#javaToJS(java.lang.Object, org.mozilla.javascript.Scriptable)]
But I don't know where to get the scope object :(.

Thanks for help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 7 2008
Added on Apr 9 2008
0 comments
555 views