File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .winterbe .java8 ;
2
+
3
+ import javax .script .Invocable ;
4
+ import javax .script .ScriptEngine ;
5
+ import javax .script .ScriptEngineManager ;
6
+ import javax .script .ScriptException ;
7
+
8
+ /**
9
+ * @author Benjamin Winterberg
10
+ */
11
+ public class Nashorn7 {
12
+
13
+ public static class Person {
14
+ private String name ;
15
+
16
+ public String getName () {
17
+ return name ;
18
+ }
19
+
20
+ public void setName (String name ) {
21
+ this .name = name ;
22
+ }
23
+
24
+ public int getLengthOfName () {
25
+ return name .length ();
26
+ }
27
+ }
28
+
29
+ public static void main (String [] args ) throws ScriptException , NoSuchMethodException {
30
+ ScriptEngine engine = new ScriptEngineManager ().getEngineByName ("nashorn" );
31
+ engine .eval ("function foo(predicate, obj) { return !!(eval(predicate)); };" );
32
+
33
+ Invocable invocable = (Invocable ) engine ;
34
+
35
+ Person person = new Person ();
36
+ person .setName ("Hans" );
37
+
38
+ String predicate = "obj.getLengthOfName() >= 4" ;
39
+ Object result = invocable .invokeFunction ("foo" , predicate , person );
40
+ System .out .println (result );
41
+ }
42
+
43
+ }
You can’t perform that action at this time.
0 commit comments