Nashorn second Attempt (#923)
* Nashorn * Nashorn x2 * Nashorn added trailing newlines * Cleanup * Formatted script lines * Change system outs to asserts * Change Nashorn to be Junit tests * Remove empty test
This commit is contained in:
committed by
Zeger Hendrikse
parent
8f4ef41eb3
commit
fbb4065024
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
@@ -0,0 +1 @@
|
||||
print(__FILE__, __LINE__, __DIR__);
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -0,0 +1 @@
|
||||
function increment(num) ++num;
|
||||
@@ -0,0 +1,2 @@
|
||||
print(" hello world".trimLeft());
|
||||
print("hello world ".trimRight());
|
||||
@@ -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]);
|
||||
Reference in New Issue
Block a user