How to BIND java objects to js (javascript) objects - in Nashorn Engine in Java 8




Program/ Example of Nashorn Javascript (js) Engine in Java 8 - How to BIND java objects to js (javascript) objects >

package nashorn11_BINDjavaObjectsToJsObjects;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
* How to BIND java objects to js (javascript) objects.
*
*/
public class BindJavaObjectsToJsObjects {
   public static void main(String[] args) throws Exception {
     
      System.out.println("In java - START");
      
       ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
       engine.eval("load('E:/pocJmse/myNashornJavaScript.js')");
       Invocable invocable = (Invocable) engine;
       Employee employee = new Employee();
       employee.setCity("Delhi");
       System.out.println("\n"+"In java - Display Employee's city in java - Before calling js function");
       System.out.println("employee.getCity() = "+employee.getCity()); //It will print "Delhi"
      
       Object result = invocable.invokeFunction("employeeBindingFunction", employee);    
       System.out.println("\nIn java, result = "+result);
      
       System.out.println("\n"+"In java - Now, let's display Employee's city again (after modification in js)");
       System.out.println("employee.getCity() = "+employee.getCity()); //It will print "Paris" as changed in js by binded object (i.e. by jsEmployee)
      
       System.out.println("In java - FINISH");
      
   }
}
/*OUTPUT
In java - START
In myNashornJavaScript.js
End myNashornJavaScript.js
In java - Display Employee's city in java - Before calling js function
employee.getCity() = Delhi
1- In js - Print city of js and java employee BEFORE binding
jsEmployee.city = undefined
javaEmployee.city = Delhi
2- In js - BIND js and java employee objects
3- In js - Print city of js and java employee AFTER binding
jsEmployee.city = Delhi
javaEmployee.city = Delhi
4- In js - Modify city of js employee - Changes will be reflected in java Employee as well
5- In js - Print city of js and java employee AFTER MODIFICATION
jsEmployee.city = Paris
javaEmployee.city = Paris
In java, result = binding And Modification Completed
In java - Now, let's display Employee's city again (after modification in js)
employee.getCity() = Paris
In java - FINISH
*/

Create Employee class >
package nashorn11_BINDjavaObjectsToJsObjects;
public class Employee {
   private String city;
   public String getCity() {
          return city;
   }
   public void setCity(String city) {
          this.city = city;
   }
}


E:/pocJmse/myNashornJavaScript.js looks like this >
print('In myNashornJavaScript.js');
var employee = new Employee();
function Employee() {}
//Create function which will bind  to bind js and java employee objects
var employeeBindingFunction = function(javaEmployee) {
   var jsEmployee = new Employee();
   print("\n1- In js - Print city of js and java employee BEFORE binding");
   print("jsEmployee.city = "+jsEmployee.city); //It will be undefined at this stage
   print("javaEmployee.city = "+javaEmployee.city); //It will have value as "Delhi" as passed from java
   print("\n2- In js - BIND js and java employee objects");
   Object.bindProperties(jsEmployee, javaEmployee);
  
   print("\n3- In js - Print city of js and java employee AFTER binding");
   print("jsEmployee.city = "+jsEmployee.city); //Now, after binding with java Employee, it's value will also be "Delhi"
   print("javaEmployee.city = "+javaEmployee.city);  //It will have value as "Delhi" as passed from java
  
   print("\n4- In js - Modify city of js employee - Changes will be reflected in java Employee as well");
   jsEmployee.city="Paris";
  
   print("\n5- In js - Print city of js and java employee AFTER MODIFICATION");
   print("jsEmployee.city = "+jsEmployee.city); //Modified value
   print("javaEmployee.city = "+javaEmployee.city);  //changes to js employee - reflected in java Employee as well
  
   //return message
   return "binding And Modification Completed";
};
print('End myNashornJavaScript.js');




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

eEdit
Must read for you :