Java Reference
In-Depth Information
The two versions of the getInterface() method let you get an instance of a Java
interface that is implemented in a scripting languages. I will discuss these functions in
details in the next section. The invokeFunction() method lets you invoke a top-level
function written in a scripting language. The invokeMethod() method lets you invoke
methods of objects written in a scripting language.
It is the responsibility of the developer to check if a script engine implements the
Invocable interface, before invoking a procedure. Invoking a procedure is a four-step
process:
Check if the script engine supports procedure invocation
Invocable type
Cast the engine reference to the
Evaluate the script that contains the source code for the
procedure, so the engine compiles and caches the script
invokeFunction() method of the Invocable interface
to invoke procedures and functions; use the invokeMethod()
method to invoke methods of the objects created in a scripting
language
Use the
The following snippet of code performs the check that the script engine
implementation class implements the Invocable interface:
// Get the Nashorn engine
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// Make sure the script engine implements the Invocable interface
if (engine instanceof Invocable) {
System.out.println("Invoking procedures is supported.");
}
else {
System.out.println("Invoking procedures is not supported.");
}
The second step is to cast the engine reference to the Invocable interface type:
if (engine instanceof Invocable) {
Invocable inv = (Invocable)engine;
// More code goes here
}
 
Search WWH ::




Custom Search