tuxmartin

JavaScriptInJava

Jan 22nd, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. /* *************************** JavaScriptInJava.java *************************** */
  2. package test.test;
  3.  
  4. import javax.script.Bindings;
  5. import javax.script.Compilable; // Require Java 8 !!!
  6. import javax.script.CompiledScript; // Require Java 8 !!!
  7. import javax.script.Invocable;
  8. import javax.script.ScriptEngine;
  9. import javax.script.ScriptEngineManager;
  10. import javax.script.ScriptException;
  11. import javax.script.SimpleBindings;
  12.  
  13. public class JavaScriptInJava {
  14.  
  15.     public static void main(String[] args) throws ScriptException, NoSuchMethodException {
  16.  
  17.         ScriptEngineManager manager = new ScriptEngineManager();
  18.         ScriptEngine engine = manager.getEngineByName("JavaScript");
  19.  
  20.         /* ----------------------------------------------------------------------- */
  21.        
  22.         // Put (string) param to JavaScript.
  23.         engine.put("testString", "Hello, world!");
  24.         engine.eval("print(testString)");
  25.         // Put (string) param to JavaScript.
  26.        
  27.         /* ----------------------------------------------------------------------- */
  28.  
  29.         // Find max number in int array with JavaScript:
  30.         int[] array = { 3, 6, 2, 56, 32, 5, 89, 32 };
  31.         engine.put("arr", array);
  32.  
  33.         String script =
  34.                   "var largest = arr[0];                "
  35.                 + "function maxNumber(arr) {                "
  36.                 + "       for (var i = 0; i < arr.length; i++) {    "
  37.                 + "               if (largest < arr[i] ) {      "
  38.                 + "                       largest = arr[i];     "
  39.                 + "               }                 "
  40.                 + "       }                     "
  41.                 + "       return largest;               "
  42.                 + "}                            "
  43.                 + "maxNumber(arr);                  ";
  44.  
  45.         Number result = (Number) engine.eval(script);
  46.         System.out.println("Max=" + result.longValue());
  47.         // Find max number in int array with JavaScript.
  48.        
  49.         /* ----------------------------------------------------------------------- */
  50.  
  51.         // Work with Java class from JavaScript:       
  52.         String userScript =
  53.                   "user1.setName(\"Test User\");        "
  54.                 + "print( user1.getName() );            ";
  55.        
  56.         Bindings bindings = new SimpleBindings();
  57.         User u = new User();
  58.         bindings.put("user1", u);      
  59.  
  60.         engine.eval(userScript, bindings);
  61.  
  62.             // Require Java 8 !!!
  63.         // Compile JavaScript (faster):
  64.         if (engine instanceof Compilable) {
  65.             System.out.println(" Compiling...");
  66.             Compilable compEngine = (Compilable) engine;
  67.             CompiledScript cs = compEngine.compile(userScript);
  68.             cs.eval(bindings);
  69.         } else {
  70.             engine.eval(userScript, bindings);
  71.         }
  72.         // Compile JavaScript (faster).
  73.             // Require Java 8 !!!
  74.        
  75.         // Work with Java class from JavaScript.
  76.        
  77.         /* ----------------------------------------------------------------------- */
  78.        
  79.         // Use JavaScript function in Java code:
  80.         String math =
  81.                   "function addition(a, b) {            "
  82.                 + "     return a+b;         "
  83.                 + "}                        "
  84.                 + "                     "
  85.                 + "function substraction(a, b) {        "
  86.                 + "     return a-b;         "
  87.                 + "}                        "
  88.                 + "                     ";
  89.        
  90.         engine.eval(math);
  91.        
  92.         Invocable inv = (Invocable) engine;
  93.        
  94.         int a = 10;
  95.         int b = 5;
  96.         System.out.println("A=" + a + " B=" + b);
  97.        
  98.         Object aPlusB = inv.invokeFunction("addition", a, b);
  99.         System.out.println("A+B = " + aPlusB);
  100.        
  101.         Object[] inputParams = {
  102.                 new Integer(10),
  103.                 b
  104.                 };
  105.         Object aMinusB = inv.invokeFunction("substraction", inputParams);
  106.         System.out.println("A-B = " + aMinusB);
  107.        
  108.         int x = (Integer) aPlusB + 1;
  109.         System.out.println("aPlusB + 1 = " + x);
  110.  
  111.         // Use JavaScript function in Java code.
  112.        
  113.         /* ----------------------------------------------------------------------- */      
  114.     }
  115. }
  116.  
  117. /* *************************** User.java *************************** */
  118. package test.test;
  119.  
  120. public class User {
  121.    
  122.     private String name;
  123.    
  124.     public User() {
  125.     }
  126.    
  127.     public String getName() {
  128.         return name;
  129.     }
  130.    
  131.     public void setName(String name) {
  132.         this.name = name;
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment