File tree Expand file tree Collapse file tree 24 files changed +475
-19
lines changed
java/io/github/dunwu/javacore/jdk8/nashorn
resources/META-INF/scripts Expand file tree Collapse file tree 24 files changed +475
-19
lines changed Original file line number Diff line number Diff line change 45
45
- [ Java 正则] ( docs/advanced/java-regex.md )
46
46
- [ Java 编码和加密] ( docs/advanced/java-crypto.md ) - 关键词:` Base64 ` 、` 消息摘要 ` 、` 数字签名 ` 、` 对称加密 ` 、` 非对称加密 ` 、` MD5 ` 、` SHA ` 、` HMAC ` 、` AES ` 、` DES ` 、` DESede ` 、` RSA `
47
47
- [ Java 本地化] ( docs/advanced/java-locale.md )
48
- - [ Java JDK8] ( docs/advanced/jdk8.md )
48
+ - [ Java JDK8] ( docs/advanced/jdk8.md ) - 关键词: ` Stream ` 、 ` lambda ` 、 ` Optional ` 、 ` @FunctionalInterface `
49
49
50
50
### [ Java 容器] ( docs/container )
51
51
Original file line number Diff line number Diff line change
1
+ # JDK8 特性示例
2
+
3
+ > 本项目代码来源于 [ java8-tutorial] ( https://github.com/winterbe/java8-tutorial )
Original file line number Diff line number Diff line change 1
1
package io .github .dunwu .javacore .jdk8 .nashorn ;
2
2
3
+ import cn .hutool .core .io .resource .ResourceUtil ;
3
4
import io .github .dunwu .javacore .jdk8 .lambda .Person ;
4
5
6
+ import java .io .BufferedReader ;
5
7
import java .io .FileReader ;
8
+ import java .io .InputStream ;
9
+ import java .nio .charset .Charset ;
6
10
import java .time .LocalDateTime ;
7
11
import java .util .Date ;
8
12
import javax .script .Invocable ;
@@ -18,7 +22,8 @@ public class Nashorn1 {
18
22
19
23
public static void main (String [] args ) throws Exception {
20
24
ScriptEngine engine = new ScriptEngineManager ().getEngineByName ("nashorn" );
21
- engine .eval (new FileReader ("res/nashorn1.js" ));
25
+ BufferedReader reader = ResourceUtil .getReader ("META-INF/scripts/nashorn1.js" , Charset .defaultCharset ());
26
+ engine .eval (reader );
22
27
23
28
Invocable invocable = (Invocable ) engine ;
24
29
Object result = invocable .invokeFunction ("fun1" , "Peter Parker" );
Original file line number Diff line number Diff line change 1
1
package io .github .dunwu .javacore .jdk8 .nashorn ;
2
2
3
+ import cn .hutool .core .io .resource .ResourceUtil ;
3
4
import jdk .nashorn .api .scripting .NashornScriptEngine ;
4
5
5
6
import java .util .concurrent .TimeUnit ;
@@ -13,7 +14,8 @@ public class Nashorn10 {
13
14
14
15
public static void main (String [] args ) throws ScriptException , NoSuchMethodException {
15
16
NashornScriptEngine engine = (NashornScriptEngine ) new ScriptEngineManager ().getEngineByName ("nashorn" );
16
- engine .eval ("load('res/nashorn10.js')" );
17
+ String filename = ResourceUtil .getResource ("META-INF/scripts/nashorn10.js" ).getFile ();
18
+ engine .eval ("load('" + filename + "')" );
17
19
18
20
long t0 = System .nanoTime ();
19
21
Original file line number Diff line number Diff line change 1
1
package io .github .dunwu .javacore .jdk8 .nashorn ;
2
2
3
+ import cn .hutool .core .io .resource .ResourceUtil ;
3
4
import jdk .nashorn .api .scripting .ScriptObjectMirror ;
4
5
6
+ import java .io .BufferedReader ;
5
7
import java .io .FileReader ;
8
+ import java .nio .charset .Charset ;
6
9
import java .util .Arrays ;
7
10
import javax .script .ScriptEngine ;
8
11
import javax .script .ScriptEngineManager ;
@@ -33,7 +36,8 @@ public static void fun4(ScriptObjectMirror person) {
33
36
34
37
public static void main (String [] args ) throws Exception {
35
38
ScriptEngine engine = new ScriptEngineManager ().getEngineByName ("nashorn" );
36
- engine .eval (new FileReader ("res/nashorn2.js" ));
39
+ BufferedReader reader = ResourceUtil .getReader ("META-INF/scripts/nashorn2.js" , Charset .defaultCharset ());
40
+ engine .eval (reader );
37
41
}
38
42
39
43
}
Original file line number Diff line number Diff line change 1
1
package io .github .dunwu .javacore .jdk8 .nashorn ;
2
2
3
+ import cn .hutool .core .io .resource .ResourceUtil ;
4
+
5
+ import java .io .BufferedReader ;
6
+ import java .nio .charset .Charset ;
3
7
import javax .script .ScriptEngine ;
4
8
import javax .script .ScriptEngineManager ;
5
9
@@ -12,7 +16,8 @@ public class Nashorn3 {
12
16
13
17
public static void main (String [] args ) throws Exception {
14
18
ScriptEngine engine = new ScriptEngineManager ().getEngineByName ("nashorn" );
15
- engine .eval ("load('res/nashorn3.js')" );
19
+ BufferedReader reader = ResourceUtil .getReader ("META-INF/scripts/nashorn3.js" , Charset .defaultCharset ());
20
+ engine .eval (reader );
16
21
}
17
22
18
23
}
Original file line number Diff line number Diff line change 1
1
package io .github .dunwu .javacore .jdk8 .nashorn ;
2
2
3
+ import cn .hutool .core .io .resource .ResourceUtil ;
4
+
5
+ import java .io .BufferedReader ;
6
+ import java .nio .charset .Charset ;
7
+ import java .util .Arrays ;
3
8
import javax .script .ScriptEngine ;
4
9
import javax .script .ScriptEngineManager ;
5
10
@@ -12,7 +17,8 @@ public class Nashorn4 {
12
17
13
18
public static void main (String [] args ) throws Exception {
14
19
ScriptEngine engine = new ScriptEngineManager ().getEngineByName ("nashorn" );
15
- engine .eval ("loadWithNewGlobal('res/nashorn4.js')" );
20
+ String filename = ResourceUtil .getResource ("META-INF/scripts/nashorn4.js" ).getFile ();
21
+ engine .eval ("loadWithNewGlobal('" + filename + "')" );
16
22
}
17
23
18
24
}
Original file line number Diff line number Diff line change 1
1
package io .github .dunwu .javacore .jdk8 .nashorn ;
2
2
3
+ import cn .hutool .core .io .resource .ResourceUtil ;
4
+
3
5
import javax .script .Invocable ;
4
6
import javax .script .ScriptEngine ;
5
7
import javax .script .ScriptEngineManager ;
@@ -13,7 +15,8 @@ public class Nashorn5 {
13
15
14
16
public static void main (String [] args ) throws Exception {
15
17
ScriptEngine engine = new ScriptEngineManager ().getEngineByName ("nashorn" );
16
- engine .eval ("load('res/nashorn5.js')" );
18
+ String filename = ResourceUtil .getResource ("META-INF/scripts/nashorn5.js" ).getFile ();
19
+ engine .eval ("load('" + filename + "')" );
17
20
18
21
Invocable invocable = (Invocable ) engine ;
19
22
Original file line number Diff line number Diff line change 1
1
package io .github .dunwu .javacore .jdk8 .nashorn ;
2
2
3
+ import cn .hutool .core .io .resource .ResourceUtil ;
3
4
import jdk .nashorn .api .scripting .ScriptObjectMirror ;
4
5
5
6
import javax .script .Invocable ;
@@ -19,7 +20,8 @@ public static void getProduct(ScriptObjectMirror result) {
19
20
20
21
public static void main (String [] args ) throws Exception {
21
22
ScriptEngine engine = new ScriptEngineManager ().getEngineByName ("nashorn" );
22
- engine .eval ("load('res/nashorn6.js')" );
23
+ String filename = ResourceUtil .getResource ("META-INF/scripts/nashorn6.js" ).getFile ();
24
+ engine .eval ("load('" + filename + "')" );
23
25
24
26
Invocable invocable = (Invocable ) engine ;
25
27
Original file line number Diff line number Diff line change 1
1
package io .github .dunwu .javacore .jdk8 .nashorn ;
2
2
3
+ import cn .hutool .core .io .resource .ResourceUtil ;
3
4
import io .github .dunwu .javacore .jdk8 .lambda .Person ;
4
5
import jdk .nashorn .api .scripting .NashornScriptEngine ;
5
6
@@ -13,7 +14,8 @@ public class Nashorn8 {
13
14
14
15
public static void main (String [] args ) throws ScriptException , NoSuchMethodException {
15
16
NashornScriptEngine engine = (NashornScriptEngine ) new ScriptEngineManager ().getEngineByName ("nashorn" );
16
- engine .eval ("load('res/nashorn8.js')" );
17
+ String filename = ResourceUtil .getResource ("META-INF/scripts/nashorn8.js" ).getFile ();
18
+ engine .eval ("load('" + filename + "')" );
17
19
18
20
engine .invokeFunction ("evaluate1" ); // [object global]
19
21
engine .invokeFunction ("evaluate2" ); // [object Object]
You can’t perform that action at this time.
0 commit comments