javaxscriptInvocable
javax.script.Invocable is a Java interface in the javax.script package that extends the scripting framework to allow dynamic invocation of script-defined functions and methods from Java, and to enable scripts to call Java interfaces or methods. It is part of the Java Scripting API (JSR 223) and is implemented by many script engines that support interactive or host-language interoperability, such as JavaScript engines that existed in Java (for example, Nashorn or Rhino in earlier environments).
- invokeFunction(String name, Object... args): calls a top-level function defined in the script by name, returning the
- invokeMethod(Object thiz, String name, Object... args): calls an instance method defined on a script object referred
- getInterface(Class<T> clasz): creates a proxy that implements the given Java interface, delegating method calls to a
- getInterface(Object thiz, Class<T> clasz): creates a Java interface proxy bound to a script object, enabling typed
Usage pattern typically involves obtaining a ScriptEngine, ensuring it implements Invocable, and casting to Invocable to
- Not all script engines implement Invocable; check engine capabilities or use instanceof Invocable before casting.
- The Nashorn engine supported Invocable in Java 8, but Nashorn was removed in later Java releases;