A typical JSR-223 script would begin with a series of surrogate imports like this (JavaScript+Nashorn chosen for example purposes):
// "import" classes and static methods
var Foo = Java.type("my.package.Foo"); // application classes require Java.type() use
var bar = Foo.bar; // static method
var Logger = java.util.logging.Logger; // system classes can be accessed directly
var sin = java.lang.Math.sin; // the same for static methods
// use them
var foo = new Foo();
print(bar());
var log = Logger.getLogger("foo");
print(sin(42));
I want to get rid of those surrogates by emulating an import-like functionality for scripts. That means, I want to have global objects (like Foo
, bar
, Logger
and sin
in the example above) pre-created by my Java code. This should automatically make a common set of imports available for multiple scripts.
I have found two methods for doing that with Nashorn:
Method 1: Generate a script prelude and eval()
it before the main script. That would be literally the first half of the example code above.
Method 2: Obtain class and method references from a ScriptEngine, cache them and use for subsequent script invocations:
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine nashorn = sem.getEngineByName("nashorn");
Object fooClass = nashorn.eval("Java.type('my.package.Foo')"); // instance of jdk.internal.dynalink.beans.StaticClass
Object loggerClass = nashorn.eval("java.util.logging.Logger"); // the same
Object barFunction = nashorn.eval("Java.type('my.package.Foo').bar"); // instance of jdk.internal.dynalink.beans.SimpleDynamicMethod
Object sinFunction = nashorn.eval("java.lang.Math.sin"); // the same
ScriptEngine nashorn1 = sem.getEngineByName("nashorn");
nashorn1.put("Foo", fooClass);
nashorn1.put("bar", barFunction);
nashorn1.put("Logger", loggerClass);
nashorn1.put("sin", sinFunction);
nashorn1.eval("var foo = new Foo(); bar(); var log = Logger.getLogger('foo'); print(sin(42));");
Obviously, none of those methods would work for any other JSR-223 engine. Is there a way to implement the same in a portable manner?