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.
Great, thanks for the help guys. Since Rhino is apparently incapable
of such a simple feature, I will look elsewhere for scripting support.
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