Java Reference
In-Depth Information
Chapter 5
Procedures and
Compiled Scripts
In this chapter, you will learn:
How to invoke procedures written in scripts from Java programs
How to implement Java interfaces in Nashorn script
How to compile Nashorn scripts and execute them repeatedly
Invoking Procedures in Scripts
In previous chapters, you have seen how to invoke scripts using the eval() method
of the ScriptEngine . It is also possible to invoke procedures written in a scripting
language directly from the Java program, provided the ScriptEngine supports procedure
invocation.
A scripting language may allow creating procedures, functions, and methods. The
Java Scripting API lets you invoke such procedures, functions, and methods from a Java
application. I will use the term “procedure” to mean procedure, function, and method in
this section. I will use the specific term when the context of the discussion requires it.
Not all script engines are required to support procedure invocation. The Nashorn
JavaScript engine supports procedure invocation. If a script engine supports it, the
implementation of the script engine class must implement the Invocable interface. The
Invocable interface contains the following four methods:
<T> T getInterface(Class<T> cls)
<T> T getInterface(Object obj, Class<T> cls)
Object invokeFunction(String name, Object... args)
throws ScriptException, NoSuchMethodException
Object invokeMethod(Object obj, String name, Object... args)
throws ScriptException, NoSuchMethodException
 
Search WWH ::




Custom Search