diff options
Diffstat (limited to 'docs/source/InvokeScriptMethod.java')
-rw-r--r-- | docs/source/InvokeScriptMethod.java | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/docs/source/InvokeScriptMethod.java b/docs/source/InvokeScriptMethod.java index a3f5ece7..d3cc788d 100644 --- a/docs/source/InvokeScriptMethod.java +++ b/docs/source/InvokeScriptMethod.java @@ -29,26 +29,29 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import javax.script.*; +import javax.script.Invocable; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +@SuppressWarnings("javadoc") public class InvokeScriptMethod { - public static void main(String[] args) throws Exception { - ScriptEngineManager manager = new ScriptEngineManager(); - ScriptEngine engine = manager.getEngineByName("nashorn"); + public static void main(final String[] args) throws Exception { + final ScriptEngineManager manager = new ScriptEngineManager(); + final ScriptEngine engine = manager.getEngineByName("nashorn"); // JavaScript code in a String. This code defines a script object 'obj' // with one method called 'hello'. - String script = "var obj = new Object(); obj.hello = function(name) { print('Hello, ' + name); }"; + final String script = "var obj = new Object(); obj.hello = function(name) { print('Hello, ' + name); }"; // evaluate script engine.eval(script); // javax.script.Invocable is an optional interface. // Check whether your script engine implements or not! // Note that the JavaScript engine implements Invocable interface. - Invocable inv = (Invocable) engine; + final Invocable inv = (Invocable) engine; // get script object on which we want to call the method - Object obj = engine.get("obj"); + final Object obj = engine.get("obj"); // invoke the method named "hello" on the script object "obj" inv.invokeMethod(obj, "hello", "Script Method !!" ); |