Helma Logo
main list history
previous version  overview  next version

Version 1 by hannes on 28. November 2006, 11:38

RhinoLoader is a Java Classloader that can load Java classes from JavaScript scripts. The scripts are compiled to Java classes and executed using the *Rhino JavaScript engine|http://www.mozilla.org/rhino/*.

The great benefit from this approach is that applications can be extended through scripting without using any Rhino or otherwise scripting specific APIs. The hosting application just uses the java.lang.ClassLoader and java.lang.Class API to get classes and instances as if it was dealing with ordinary Java classes.

Class name to Script lookup follows a very simple resolution mechanism where class "foo.Bar" is resolved to path "foo/Bar.js". Subclassing other Java class or implementing Java interfaces is also straightforward. Just include something like this in your script:

  this.__extends__ = "my.package.SomeClass";
  this.__implements__ = ["my.package.SomeInterface", ...];
The generated class can override Java methods simply by defining JavaScript functions with the method name. Note that functions that don't have a corresponding method in a Java class or Interface can't be invoked from Java, though.

To invoke an overridden Java method from JavaScript use the super$ prefix:
  this.super$put(key, value);

A simple example is contained in the demo folder. To run it, just run ant target "demo":

  ant demo