Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can't save a reference to a Java method, then call it later

44 views
Skip to first unread message

ehr...@gmail.com

unread,
Oct 27, 2013, 12:28:59 PM10/27/13
to
I'm trying to save a reference to a Java object's method in a var, then call it later, but it fails with

Java method "run" was invoked with [object Object] as "this" value that can not be converted to Java type java.lang.Runnable.

Here's the code:

Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
scope.put("foo", scope, Context.toObject(new Runnable() {
@Override public void run() {
System.err.println("Run");
}
}, scope));
cx.evaluateString(scope, "foo.run(); var bar = foo.run; bar();", "", 1, null);

I understand why this would fail; Rhino either didn't track Java's this when saving the reference or is having problems converting it. What's the right approach to accomplish this?


0 new messages