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

Extending classes with Javascript/Rhino

1,234 views
Skip to first unread message

tmcc...@gmail.com

unread,
Apr 24, 2008, 2:26:42 PM4/24/08
to
I am having trouble properly extending Java classes with Rhino.

I am using the Mozilla version of Rhino and not the builtin version
with Java 6.

------------------------ Java code to extend
---------------------------------
public class Test {

public int value = 4;

public void foo() {
System.out.println(this.value);
}

}

------------------------ Javascript code
---------------------------------

// Wrap the Java class into an adapted Javascript constructor
function Parent() {
return new JavaAdapter(Test, this);
}

// Define a constructor for Child()
function Child() {
}

// Copy the parent's prototype into the child prototype
Child.prototype = new Parent();

// Override the java version of foo()
Child.prototype.foo = function() {
this.value = 1;

// call the "super" java version of this method
Parent.prototype.foo.call(this);
}

// Create an instance and test method
var t = new Child();
t.foo(); <------ Exception gets thrown here

----------- Error message from Rhino ------------------------

Java method "foo" cannot be assigned to. (#14)

This code works when modified to be totally Javascript based. I don't
believe it's an issue with the Javascript part. Am I going about this
the wrong way? Is it possible to actually extend Java classes?

Thank you for you help.

tmcc...@gmail.com

unread,
Apr 24, 2008, 8:17:47 PM4/24/08
to
The exception actually happens at the "Child.prototype.foo =" part.

tmcc...@gmail.com

unread,
May 13, 2008, 4:41:20 PM5/13/08
to

Great, thanks for the help guys. Since Rhino is apparently incapable
of such a simple feature, I will look elsewhere for scripting support.

Norris Boyd

unread,
May 21, 2008, 10:52:23 AM5/21/08
to

Sorry we missed replying to your question. For the record I'd suggest
looking at JavaAdapter, see http://www.mozilla.org/rhino/scriptjava.html.
This allows you to implement interfaces and extend Java classes.

--N

0 new messages