Version 6 by hannes on 14. October 2005, 13:33
Helma 2 can be used as a library to instantly add scripting capability to any Java program. Scripts loaded from .js files can extend any Java class and implement any Java interface just using the following JavaScript syntax:
this.__extends__ = javaClassName;
this.__implements__ = [javaInterfaceName, ...];
====The scripted class can override any Java methods and define new ones. A trivial example (Note that functions that don't have a corresponding method in a Java class or Interface can't be invoked from Java, though!) Overridden methods in the super class can be called using the super$ prefix:
this.super$put(key, value);
==== A simple example
The Java side:
import org.helma.javascript.RhinoLoader;
import java.io.File;
import java.io.IOException;
public class Test {
public static void main(String[] args)
throws IOException, ClassNotFoundException {
RhinoLoader loader = new RhinoLoader(new File("scriptdir"));
Runnable test = (Runnable) loader.getInstance("Test");
new Thread(test).start();
}
}
This tries to load a script from a file called scriptdir/Test.js that implements java.lang.Runnable. The script file might look as simple as this:
this.__implements__ = "java.lang.Runnable";
function run() {
java.lang.System.err.println("Running!");
}
The scripted class can override any Java methods and define new ones. (Note that functions that don't have a corresponding method in a Java class or Interface can't be invoked from Java, though!) Overridden methods in the super class can be called using the super$ prefix:
this.super$put(key, value);
this.__extends__ = javaClassName;
this.__implements__ = [javaInterfaceName, ...];
====The scripted class can override any Java methods and define new ones. A trivial example (Note that functions that don't have a corresponding method in a Java class or Interface can't be invoked from Java, though!) Overridden methods in the super class can be called using the super$ prefix:
this.super$put(key, value);
==== A simple example
The Java side:
import org.helma.javascript.RhinoLoader;
import java.io.File;
import java.io.IOException;
public class Test {
public static void main(String[] args)
throws IOException, ClassNotFoundException {
RhinoLoader loader = new RhinoLoader(new File("scriptdir"));
Runnable test = (Runnable) loader.getInstance("Test");
new Thread(test).start();
}
}
This tries to load a script from a file called scriptdir/Test.js that implements java.lang.Runnable. The script file might look as simple as this:
this.__implements__ = "java.lang.Runnable";
function run() {
java.lang.System.err.println("Running!");
}
The scripted class can override any Java methods and define new ones. (Note that functions that don't have a corresponding method in a Java class or Interface can't be invoked from Java, though!) Overridden methods in the super class can be called using the super$ prefix:
this.super$put(key, value);
removed
added