Home > Java > javaTutorial > Detailed explanation of Nashorn script engine for Java8

Detailed explanation of Nashorn script engine for Java8

黄舟
Release: 2017-03-20 10:20:34
Original
2358 people have browsed it

This article is about all the easy-to-understand code examples of the Nashorn JavaScript engine. The Nashorn JavaScript engine is part of Java SE 8 and competes with other independent engines like Google V8 (which is the engine of Google Chrome and Node.js). Nashorn extends Java's ability to run dynamic JavaScript scripts on the JVM.

In the next 15 minutes or so, you'll learn how to dynamically run JavaScript on the JVM. Demonstrate recent Nashorn language features with some short code examples. Learn how Java and JavaScript call each other. Finally, it includes how to integrate dynamic scripts into your daily Java business.

Use Nashorn

The Nashorn javascript engine is either used in the java program in the way of programming or in the command line tool jjs, which is in the directory $JAVA_HOME/ in bin. If you are going to create a symbolic link to jjs, as follows:

$ cd /usr/bin
$ ln -s $JAVA_HOME/bin/jjs jjs
$ jjs
jjs> print('Hello World');
Copy after login

This tutorial focuses on using nashorn in java code, so we will skip jjs for now. A simple HelloWorld example using java code is as follows:

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval("print('Hello World!');");
Copy after login

In order to execute JavaScript code in java, first use the package javax.script in the original Rhino (the engine from Mozilla in the old version of Java) to create a nashorn Script engine. .

You can either execute the JavaScript code directly as a string as above, or you can put it into a js script file, such as:

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval(new FileReader("script.js"));
Copy after login

Nashorn javascript It is based on ECMAScript 5.1, but subsequent versions of nashorn will support ECMAScript 6:

The current Nashorn strategy is to follow the ECMAScript specification. When we release JDK 8, we will implement the ECMAScript 5.1 standard. Subsequent versions of Nashorn will implement the ECMAScript Edition 6 standard.

Nashorn defines many languages ​​and extends the API of the ECMAScript standard. Next we look at the communication between java and JavaScript.

Java calls Javascript functions

Nashorn supports java code to directly call JavaScript functions defined in script files. You can pass a Java object as a parameter to a function and receive the returned data in the Java method that calls the function.

The following JavaScript code will be called on the java side:

var fun1 = function(name) {
    print('Hi there from Javascript, ' + name);
    return "greetings from javascript";
};

var fun2 = function (object) {
    print("JS Class Definition: " + Object.prototype.toString.call(object));
};
Copy after login

In order to call the function, you must first convert the script engine to Invocable. NashornScriptEngine implements the Invocable interface and defines a method to call a JavaScript function invokeFunction, just pass in the function name.

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval(new FileReader("script.js"));

Invocable invocable = (Invocable) engine;

Object result = invocable.invokeFunction("fun1", "Peter Parker");
System.out.println(result);
System.out.println(result.getClass());

// Hi there from Javascript, Peter Parker
// greetings from javascript
// class java.lang.String
Copy after login

The execution of the above code will print three lines of information on the console. Calling the print function pipes the output to the System.out console, so the first thing we see is the information printed by JavaScript.

Now we call the second function by passing any Java object:

invocable.invokeFunction("fun2", new Date());
// [object java.util.Date]

invocable.invokeFunction("fun2", LocalDateTime.now());
// [object java.time.LocalDateTime]

invocable.invokeFunction("fun2", new Person());
// [object com.winterbe.java8.Person]
Copy after login

You can pass any Java object without losing type information on the JavaScript side. Because the script itself is executed in the JVM virtual machine, we can fully leverage the power of the nashorn engine's Java API and external libraries.

Calling Java methods on the JavaScript side

Calling Java methods in JavaScript is very simple. First we define a static Java method:

static String fun1(String name) {
    System.out.format("Hi there from Java, %s", name);
    return "greetings from java";
}
Copy after login

JavaScript can reference Java classes through the Java.type API. This is similar to introducing other classes into a Java class. After defining a Java type, we can directly call its static method fun1() and print the result to sout. Because the method is static, we don't need to create an instance of the class.

var MyJavaClass = Java.type('my.package.MyJavaClass');

var result = MyJavaClass.fun1('John Doe');
print(result);

// Hi there from Java, John Doe
// greetings from java
Copy after login

How does Nashorn handle native JavaScript types and javatype conversion when calling java methods? Let's find out with a simple example.

The following java method simply prints the actual class method parameter type:

static void fun2(Object object) {
    System.out.println(object.getClass());
}
Copy after login

To understand how the engine handles type conversion, I used different JavaScript types to call the java method:

MyJavaClass.fun2(123);
// class java.lang.Integer

MyJavaClass.fun2(49.99);
// class java.lang.Double

MyJavaClass.fun2(true);
// class java.lang.Boolean

MyJavaClass.fun2("hi there")
// class java.lang.String

MyJavaClass.fun2(new Number(23));
// class jdk.nashorn.internal.objects.NativeNumber

MyJavaClass.fun2(new Date());
// class jdk.nashorn.internal.objects.NativeDate

MyJavaClass.fun2(new RegExp());
// class jdk.nashorn.internal.objects.NativeRegExp

MyJavaClass.fun2({foo: 'bar'});
// class jdk.nashorn.internal.scripts.JO4
Copy after login

The original javascript type is converted to the appropriate java wrapper class. Instead of a local javascript objectinternal adapter class. Keep in mind that these classes come from jdk.nashorn.internal, so you should not use these classes on the client:

Anything marked internal will likely change out from underneath you.

ScriptObjectMirror

当使用ScriptObjectMirror把本地JavaScript对象传入时,实际上是有一个java对象表示JavaScript 对象。 ScriptObjectMirror 实现了接口与jdk.nashorn.api内部的映射。这个包下的类目的就是用于客户端代码使用。

下一个示例更改参数类型Object为ScriptObjectMirror,因此我们能获取到传入JavaScript中对象的一些信息:

static void fun3(ScriptObjectMirror mirror) {
    System.out.println(mirror.getClassName() + ": " +
        Arrays.toString(mirror.getOwnKeys(true)));
}
Copy after login

当我们把传递对象hash到方法中,在Java端就能访问这些属性

MyJavaClass.fun3({
    foo: 'bar',
    bar: 'foo'
});

// Object: [foo, bar]
Copy after login

我们也可以在Java端调用JavaScript对象中的函数。我们首先定义一个JavaScript类型 Person,包含属性 firstNamelastName 和函数getFullName。

function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.getFullName = function() {
        return this.firstName + " " + this.lastName;
    }
}
Copy after login

javascript 函数getFullName 能被 ScriptObjectMirror 的callMember()调用。

static void fun4(ScriptObjectMirror person) {
    System.out.println("Full Name is: " + person.callMember("getFullName"));
}
Copy after login

当我们传入一个新的person给java 方法时,我们能在控制台看到预期结果:

var person1 = new Person("Peter", "Parker");
MyJavaClass.fun4(person1);

// Full Name is: Peter Parker
Copy after login

语言扩展

Nashorn 定义一系列的语言和扩展了 ECMAScript 标准的API。 让我们直接进入最新的功能:

类型数组

原始javascript 数组时无类型的。 Nashorn 运行你在JavaScript中使用java数组:

var IntArray = Java.type("int[]");

var array = new IntArray(5);
array[0] = 5;
array[1] = 4;
array[2] = 3;
array[3] = 2;
array[4] = 1;

try {
    array[5] = 23;
} catch (e) {
    print(e.message);  // Array index out of range: 5
}

array[0] = "17";
print(array[0]);  // 17

array[0] = "wrong type";
print(array[0]);  // 0

array[0] = "17.3";
print(array[0]);  // 17
Copy after login

int[] 数组的行为像一个真正的 java int 数组。 但当我们试图添加非整数的值的数组时,Nashorn 会执行隐式类型转换。 字符串会自动转换为int,这相当方便。

集合与For Each

我们可以使用java的集合来代替数组。首先定义使用 Java.type定义一个java类型,而后根据需要创建一个实例。

var ArrayList = Java.type('java.util.ArrayList');
var list = new ArrayList();
list.add('a');
list.add('b');
list.add('c');

for each (var el in list) print(el);  // a, b, c
Copy after login

为了遍历集合和数组中的元素,Nashorn 引入了 for each 语句。这就像是 Java 的 for 循环一样。

这里是一个对集合元素进行遍历的例子,使用的是 :

var map = new java.util.HashMap();
map.put('foo', 'val1');
map.put('bar', 'val2');

for each (var e in map.keySet()) print(e);  // foo, bar

for each (var e in map.values()) print(e);  // val1, val2
Copy after login

Lambda 表达式和 Streams

似乎大家都比较喜欢 Lambda 和 Streams —— Nashorn 也是!虽然 ECMAScript 5.1 中缺少 Java 8 Lambda 表达式中的紧缩箭头的语法,但我们可以在接受 Lambda 表达式的地方使用函数来替代。

var list2 = new java.util.ArrayList();
list2.add("ddd2");
list2.add("aaa2");
list2.add("bbb1");
list2.add("aaa1");
list2.add("bbb3");
list2.add("ccc");
list2.add("bbb2");
list2.add("ddd1");

list2
    .stream()
    .filter(function(el) {
        return el.startsWith("aaa");
    })
    .sorted()
    .forEach(function(el) {
        print(el);
    });
    // aaa1, aaa2
Copy after login

扩展类

Java 的类型可以简单的通过 Java.extend 进行扩展,在下个例子你将在脚本中创建一个多线程示例:

var Runnable = Java.type('java.lang.Runnable');
var Printer = Java.extend(Runnable, {
    run: function() {
        print('printed from a separate thread');
    }
});

var Thread = Java.type('java.lang.Thread');
new Thread(new Printer()).start();

new Thread(function() {
    print('printed from another thread');
}).start();

// printed from a separate thread
// printed from another thread
Copy after login

参数重载

方法和函数可以使用点符号或方括号来进行调用。

var System = Java.type('java.lang.System');
System.out.println(10);              // 10
System.out["println"](11.0);         // 11.0
System.out["println(double)"](12);   // 12.0
Copy after login

在使用重载的参数来调用方法时可以传递可选参数来确定具体调用了哪个方法,如 println(double)。

Java Beans

我们不需要常规的用 getter 或者 setter 来访问类成员属性,可直接用属性名简单访问 Java Bean 中的属性。例如:

var Date = Java.type('java.util.Date');
var date = new Date();
date.year += 1900;
print(date.year);  // 2014
Copy after login

函数语法

如果只是简单的一行函数我们可以不用大括号:

function sqr(x) x * x;
print(sqr(3));    // 9
Copy after login

属性绑定

来自不同对象的属性可以绑定在一起:

var o1 = {};
var o2 = { foo: 'bar'};

Object.bindProperties(o1, o2);

print(o1.foo);    // bar
o1.foo = 'BAM';
print(o2.foo);    // BAM
Copy after login

字符串处理

我喜欢字符串裁剪.

print("   hehe".trimLeft());            // hehe
print("hehe    ".trimRight() + "he");   // hehehe
Copy after login

在哪里

以防忘记你在哪里:

print(FILE, LINE, DIR);
Copy after login

Import 的范围

有时,这在一次性导入多个java 包时非常有用。我们可以使用JavaImporter并结合with,在with块范围内引用:

var imports = new JavaImporter(java.io, java.lang);
with (imports) {
    var file = new File(FILE);
    System.out.println(file.getAbsolutePath());
    // /path/to/my/script.js
}
Copy after login

数组转换

有些包时可以直接使用而不必利用 Java.typeJavaImporter引入,如 java.util:

var list = new java.util.ArrayList();
list.add("s1");
list.add("s2");
list.add("s3");
Copy after login

如下的代码演示了将java list转换为JavaScript的数组:

var jsArray = Java.from(list);
print(jsArray);                                  // s1,s2,s3
print(Object.prototype.toString.call(jsArray));  // [object Array]
Copy after login

其他的方式:

var javaArray = Java.to([3, 5, 7, 11], "int[]");
Copy after login

调用父类函数

在 JavaScript 中访问重载的成员会有一点点尴尬,因为 ECMAScript 没有类似 Java 的 super 关键字一样的东西。所幸的是 Nashorn 有办法解决。

首先我们在 Java 代码中定义一个超类:

class SuperRunner implements Runnable {
    @Override
    public void run() {
        System.out.println("super run");
    }
}
Copy after login

接下来我们在 JavaScript 中重载 SuperRunner 。创建一个新的 Runner 实例时请注意 Nashorn 的扩展语法:其重载成员的语法是参考 Java 的匿名对象的做法。

var SuperRunner = Java.type('com.winterbe.java8.SuperRunner');
var Runner = Java.extend(SuperRunner);

var runner = new Runner() {
    run: function() {
        Java.super(runner).run();
        print('on my run');
    }
}
runner.run();

// super run
// on my run
Copy after login

我们使用Java.super调用了重载方法 SuperRunner.run()。

在JavaScript中执行其它脚本是十分容易的。我们可以load函数载入本地或远程的脚本。

在我的很多web前端中都使用了 Underscore.js ,因此在Nashorn中我们可以重用 Underscore:

load('http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js');

var odds = _.filter([1, 2, 3, 4, 5, 6], function (num) {
    return num % 2 == 1;
});

print(odds);  // 1, 3, 5
Copy after login

扩展脚本的执行是在同一个 JavaScript 上下文中,因此我们可以直接访问 underscore 变量。记住脚本的加载可能会因为变量名的重叠导致代码出问题。

我们可以通过将加载的脚本文件放置到一个新的全局上下文来解决这个问题:

loadWithNewGlobal('script.js');
Copy after login

命令行脚本

如果你对用 Java 编写命令行脚本很感兴趣的话,可以试试 Nake 。Nake 是一个为 Java 8 Nashorn 准备的简单 Make 工具。你可以在 Nakefile 文件中定义任务,然后使用 nake — myTask 来运行任务。任务使用 JavaScript 编写并通过 Nashorn 脚本模式运行,因此你可以让你的终端应用完全利用 Java 8 API 和其他 Java 库强大的功能。

对 Java 开发者而言,编写命令行脚本从来没有如此简单过。

总结

我希望这篇文章对你有用,可以让你轻松理解 Nashorn JavaScript 引擎。更多关于 Nashorn 的信息请阅读 这里, 这里 和 这里. 如果你是要用 Nashorn 编写 Shell 脚本的话可以参考 这里.

过去我也发表了一些 文章 是关于如何在 Nashron 引擎中使用 Backbone.js 模型数据的。如果你想要了解更多 Java 8 的话可以去看看我的文章 Java 8 Tutorial 和 Java 8 Stream Tutorial.

The above is the detailed content of Detailed explanation of Nashorn script engine for Java8. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template