diff --git a/core-java/src/main/java/com/baeldung/scripting/Nashorn.java b/core-java/src/main/java/com/baeldung/scripting/Nashorn.java deleted file mode 100644 index ba9b778de5..0000000000 --- a/core-java/src/main/java/com/baeldung/scripting/Nashorn.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.scripting; - -import javax.script.Bindings; -import javax.script.Invocable; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; - -public class Nashorn { - public static void main(String[] args) throws ScriptException, NoSuchMethodException { - ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); - - Object result = engine.eval( - "var greeting='hello world';" + - "print(greeting);" + - "greeting"); - - System.out.println(result); - - Bindings bindings = engine.createBindings(); - bindings.put("count", 3); - bindings.put("name", "baeldung"); - - String script = "var greeting='Hello ';" + - "for(var i=count;i>0;i--) { " + - "greeting+=name + ' '" + - "}" + - "greeting"; - - Object bindingsResult = engine.eval(script, bindings); - System.out.println(bindingsResult); - - engine.eval("function composeGreeting(name) {" + - "return 'Hello ' + name" + - "}"); - Invocable invocable = (Invocable) engine; - - Object funcResult = invocable.invokeFunction("composeGreeting", "baeldung"); - System.out.println(funcResult); - - Object map = engine.eval("var HashMap = Java.type('java.util.HashMap');" + - "var map = new HashMap();" + - "map.put('hello', 'world');" + - "map"); - - System.out.println(map); - } -} diff --git a/core-java/src/main/resources/js/bind.js b/core-java/src/main/resources/js/bind.js new file mode 100644 index 0000000000..652e646d0d --- /dev/null +++ b/core-java/src/main/resources/js/bind.js @@ -0,0 +1,15 @@ +var first = { + name: "Whiskey", + age: 5 +}; + +var second = { + volume: 100 +}; + +Object.bindProperties(first, second); + +print(first.volume); + +second.volume = 1000; +print(first.volume); diff --git a/core-java/src/main/resources/js/locations.js b/core-java/src/main/resources/js/locations.js new file mode 100644 index 0000000000..abfc944639 --- /dev/null +++ b/core-java/src/main/resources/js/locations.js @@ -0,0 +1 @@ +print(__FILE__, __LINE__, __DIR__); diff --git a/core-java/src/main/resources/js/math_module.js b/core-java/src/main/resources/js/math_module.js new file mode 100644 index 0000000000..267a100f36 --- /dev/null +++ b/core-java/src/main/resources/js/math_module.js @@ -0,0 +1,19 @@ +var math = { + increment: function (num) { + return ++num; + }, + + failFunc: function () { + try { + throw "BOOM"; + } catch (e if typeof e === 'string') { + print("String thrown: " + e); + } + catch (e) { + print("this shouldn't happen!"); + } + } +}; + + +math; diff --git a/core-java/src/main/resources/js/no_such.js b/core-java/src/main/resources/js/no_such.js new file mode 100644 index 0000000000..43b50c5cad --- /dev/null +++ b/core-java/src/main/resources/js/no_such.js @@ -0,0 +1,11 @@ +var demo = { + __noSuchProperty__: function (propName) { + print("Accessed non-existing property: " + propName); + }, + + __noSuchMethod__: function (methodName) { + print("Invoked non-existing method: " + methodName); + } +}; + +demo; diff --git a/core-java/src/main/resources/js/script.js b/core-java/src/main/resources/js/script.js new file mode 100644 index 0000000000..6f701ed59d --- /dev/null +++ b/core-java/src/main/resources/js/script.js @@ -0,0 +1 @@ +function increment(num) ++num; diff --git a/core-java/src/main/resources/js/trim.js b/core-java/src/main/resources/js/trim.js new file mode 100644 index 0000000000..81be009978 --- /dev/null +++ b/core-java/src/main/resources/js/trim.js @@ -0,0 +1,2 @@ +print(" hello world".trimLeft()); +print("hello world ".trimRight()); diff --git a/core-java/src/main/resources/js/typed_arrays.js b/core-java/src/main/resources/js/typed_arrays.js new file mode 100644 index 0000000000..6899b29373 --- /dev/null +++ b/core-java/src/main/resources/js/typed_arrays.js @@ -0,0 +1,9 @@ +function arrays(arr) { + + var javaIntArray = Java.to(arr, "int[]"); + print(javaIntArray[0]); + print(javaIntArray[1]); + print(javaIntArray[2]); +} + +arrays([100, "1654", true]); diff --git a/core-java/src/test/java/com/baeldung/scripting/NashornTest.java b/core-java/src/test/java/com/baeldung/scripting/NashornTest.java new file mode 100644 index 0000000000..08ecb24100 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/scripting/NashornTest.java @@ -0,0 +1,134 @@ +package com.baeldung.scripting; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.InputStreamReader; +import java.util.List; +import java.util.Map; + +import javax.script.Bindings; +import javax.script.Invocable; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +public class NashornTest { + + private ScriptEngine engine; + + @Before + public void setUp() { + engine = new ScriptEngineManager().getEngineByName("nashorn"); + } + + @Test + public void trim() throws ScriptException { + engine.eval(new InputStreamReader(NashornTest.class.getResourceAsStream("/js/trim.js"))); + } + + @Test + public void locations() throws ScriptException { + engine.eval(new InputStreamReader(NashornTest.class.getResourceAsStream("/js/locations.js"))); + } + + @Test + public void bindProperties() throws ScriptException { + engine.eval(new InputStreamReader(NashornTest.class.getResourceAsStream("/js/bind.js"))); + } + + @Test + public void magicMethods() throws ScriptException { + engine.eval("var demo = load('classpath:js/no_such.js');" + + "var tmp = demo.doesNotExist;" + + "var none = demo.callNonExistingMethod()"); + } + + @Test + public void typedArrays() throws ScriptException { + engine.eval(new InputStreamReader(NashornTest.class.getResourceAsStream("/js/typed_arrays.js"))); + } + + @Test + public void basicUsage() throws ScriptException { + Object result = engine.eval("var greeting='hello world';" + + "print(greeting);" + + "greeting"); + + Assert.assertEquals("hello world", result); + } + + @Test + public void jsonObjectExample() throws ScriptException { + Object obj = engine.eval("Java.asJSONCompatible({ number: 42, greet: 'hello', primes: [2,3,5,7,11,13] })"); + Map map = (Map) obj; + + Assert.assertEquals("hello", map.get("greet")); + Assert.assertTrue(List.class.isAssignableFrom(map.get("primes").getClass())); + } + + @Test + public void tryCatchGuard() throws ScriptException { + engine.eval("var math = loadWithNewGlobal('classpath:js/math_module.js');" + + "math.failFunc();"); + } + + @Test + public void extensionsExamples() throws ScriptException { + String script = "var list = [1, 2, 3, 4, 5];" + "var result = '';" + + "for each (var i in list) {" + + "result+=i+'-';" + + "};" + + "print(result);"; + engine.eval(script); + } + + @Test + public void bindingsExamples() throws ScriptException { + Bindings bindings = engine.createBindings(); + bindings.put("count", 3); + bindings.put("name", "baeldung"); + + String script = "var greeting='Hello ';" + + "for(var i=count;i>0;i--) { " + + "greeting+=name + ' '" + + "}" + + "greeting"; + + Object bindingsResult = engine.eval(script, bindings); + Assert.assertEquals("Hello baeldung baeldung baeldung ", bindingsResult); + } + + @Test + public void jvmBoundaryExamples() throws ScriptException, NoSuchMethodException { + engine.eval("function composeGreeting(name) {" + + "return 'Hello ' + name" + + "}"); + + Invocable invocable = (Invocable) engine; + + Object funcResult = invocable.invokeFunction("composeGreeting", "baeldung"); + Assert.assertEquals("Hello baeldung", funcResult); + + Object map = engine.eval("var HashMap = Java.type('java.util.HashMap');" + + "var map = new HashMap();" + + "map.put('hello', 'world');" + + "map"); + + Assert.assertTrue(Map.class.isAssignableFrom(map.getClass())); + } + + @Test + public void loadExamples() throws ScriptException { + Object loadResult = engine.eval("load('classpath:js/script.js');" + + "increment(5)"); + + Assert.assertEquals(6.0, loadResult); + + Object math = engine.eval("var math = loadWithNewGlobal('classpath:js/math_module.js');" + + "math.increment(5);"); + + Assert.assertEquals(6.0, math); + } +}