However, when you are thinking about script language support, the first thing you would be concerned might probably not be the performance, but the Java language interoperable. As I mentioned in the previous POST, JDK 6’s script engine only support engine.eval() method. If you have experience with BeanShell, you might probably be comfortable with it. Yes, I can feed the method with a script and return an Object so that I can go further. How about the parameters? OK, there are Bindings I can use.
Invocable
The only access to your script via ScriptEngine is .eval(), however, some ScriptEngine implements another interface:
Invocable
Code Example 7: You can use the Invocable
interface to call specific methods in a script.
jsEngine.eval("function sayHello() {" +
" println('Hello, world!');" +
"}");
Invocable invocableEngine = (Invocable) jsEngine;
invocableEngine.invokeFunction("sayHello");
Just look at the Invocable interface’s description:
Method Summary | ||
---|---|---|
| getInterface(Class<T> clasz) Returns an implementation of an interface using functions compiled in the interpreter. | |
| getInterface(Object thiz, Class<T> clasz) Returns an implementation of an interface using member functions of a scripting object compiled in the interpreter. | |
Object | invokeFunction(String name, Object... args) Used to call top-level procedures and functions defined in scripts. | |
Object | invokeMethod(Object thiz, String name, Object... args) Calls a method on a script object compiled during a previous script execution, which is retained in the state of the ScriptEngine . |
It looks nice. It’s much better than using the stupid eval() and binding methods in ScriptEngine.
No comments:
Post a Comment