Message-ID: <1507811700.3771.1495502451358.JavaMail.jbsadmin@aojmv0003.oracle.com> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_3770_1595701755.1495502451358" ------=_Part_3770_1595701755.1495502451358 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html Rhino Migration Guide

Rhino Migration Guide

Rhino Migration = Guide

jdk8 replaces Rhino based jsr-223 script engine with nashorn based jsr-2= 23 script engine. If you are using Rhino based jsr-223 script engine in jdk= 6 or jdk 7, you'll have some migration work when moving to jdk 8. This doc= ument is a  migration guide towards that task.

Nashorn extensions

Nashorn implements ECMAScript 5.1 specification with a number of syntax = and API extensions as documented in Nashorn extensions Few of those are Rhino specific extensions = as well. You may want to go through that document to check if a Rhino speci= fic extension is already supported by nashorn.

Ac= cessing Java packages and classes from script

Nashorn supports top-level "Packages" object = and "java", "javax" e= tc. as supported by Rhino. You can use Packages object to access Java packa= ges and classes. But, Nashorn's recommended way to access Java classes is t= o use Java.type.

Packages vs Java.type
=20
var Vector =3D java.util.Vector;
var JFrame =3D Packages.javax.swing.JFrame;

// or preferrably

var Vector =3D Java.type("java.util.Vector")
var JFrame =3D Java.type("javax.swing.JFrame")
=20

Java.type is recommended because

1) It avoid multiple step object.property resolution as done by Packages= method. Class resolution is done in one step - from String name to class

2) Java.type throws ClassNotFoundException rather than silently treating= an unresolved name to be package name!

java.util.vector results in a package object named "java.util.vecto= r" whereas Java.type("java.util.vector") results in ClassNot= FoundException.

Creating Java a= rrays from script

In Rhino, you create a Java array using Java reflection from script. In = Nashorn, you can resolve to a Java array class using the same Java.type API= . And array creation is done using new operator

Creating Java Array
=20
// Rhino way!
var Array =3D java.lang.reflect.Array
var intClass =3D java.lang.Integer.TYPE
var array =3D Array.newInstance(intClass, 8)

// Nashorn way!
var IntArray =3D Java.type("int[]")
var array =3D new IntArray(8)

=20

Java array elements are accessed/modified using [] operator in both rhin= o as well as nashorn. Also special "length" property is supported= both in rhino and nashorn.

Class object an= d .class property

If a java API accepts a java.lang.Class object, in rhino you can pass sc= ript representation of class "as is". In Nashorn, you've to use &= quot;.class" property (similar to Java).

Class object
=20
// Rhino way!
var Array =3D java.lang.reflect.Array
var strArray =3D Array.newInstance(java.lang.String, 10)

// Nashorn way!
var Array =3D Java.type("java.lang.reflect.Array")
var JString =3D Java.type("java.lang.String")

// note ".class" property access to get java.lang.Class object
var strArray =3D Array.newInstance(JString.class, 10)
=20

In the above example, better way to create Java string array from Nashor= n would be to get String[] type from Nashorn using Java.type. The example i= s written this way only to demonstrate ".class" property.

__proto__ magic prope= rty

Rhino's magic writable property __proto__ to read/write prototype of an = object is also supported by nashorn for compatibility. But nashorn recommen= ded way to read/write is Object.getPrototypeOf ( http://es5.github.io/#x15.2.3.2 ) and Object.setPrototypeOf<= /strong> ( https://developer.mozilla.org/en-US/docs/Web/JavaScri= pt/Reference/Global_Objects/Object/setPrototypeOf ) APIs. __proto__, wh= ile supported by nashorn, is deprecated.

JavaImporter and with

Nashorn supports JavaImporter constructor of Rhino. It is possible to lo= cally import multiple java packages and use it within a 'with' statement.

Java exceptions

Rhino wraps Java exceptions as a script object. If you want underlying J= ava exception, you've to use "javaException" property to access i= t. Nashorn does not wrap Java exceptions. Java exception objects as thrown = "as is". So, in catch blocks you can access Java exceptions "= ;as is".

Java exceptions
=20
// rhino
try {
    java.lang.System.loadLibrary(null)
} catch (e) {
    // false!
    print(e instanceof java.lang.NullPointerException)
    // true
    print(e.javaException instanceof java.lang.NullPointerException)
}

// in Nashorn, e instanceof java.lang.NullPointerException is true
// as there is no script wrapping of exceptions.
=20

Also, no Java object is wrapped as "script object" in Nashorn = (unlike Java).

Implementing Java = interface

Both Rhino and Nashorn support java anonymous class-like syntax to imple= ment java interfaces in script.

Java interface
=20
// Works both in rhino and nashorn.
var runnable =3D new java.lang.Runnable() {
    run: function() {=20
        java.lang.System.out.println("I am run!");
    }
};
=20

The example @ https://githu= b.com/mozilla/rhino/blob/master/examples/enum.js works on Nashorn as we= ll.

Extending Java class

To extend a concrete Java class or to implement multiple interfaces, you= have to use Java.extend in Nashorn - unlike "JavaAdapter" in Rhi= no. Java.extend is explained in Nashorn extensions document.

JavaScript vs Java S= trings

Nashorn does not use wrapper objects to provide JavaScript access to Jav= a objects like Rhino did. Since Nashorn uses java.lang.String to represent JavaScript strings internally = it is not able to distinguish between native JavaScript Strings a= nd host Java String objects, and both JavaScript and Java String methods ca= n be invoked on any String object.

There happens to be a conflict in the case of the replace metho= d which is defined in both languages with different semantics. In this case= , the JavaScript method has precedence over the Java method. One could use = explicit = method selection to invoke the Java method but it is usually simpler to= just use the JavaScript method.

Compatibility script

There are few Rhino/Mozilla extensions that are supported only if you lo= ad the compatibility script provided by nashorn. The compatibility script i= s loaded using "load('nashorn:mozilla_compat.js")

Mozilla Compatibility Script
=20
// load compatibility script

load("nashorn:mozilla_compat.js");
=20

The compatibility script implements the following Rhino extensions:

Compatibility script examples
=20
load("nashorn:mozilla_compat.js")

var obj =3D {}
obj.__defineGetter__(
    "name", function(){         =20
      return "sundar"
    }
)
print(obj.name)
obj.__defineSetter__(
   "x", function(xVal) { print("x set to " + xVal); thi=
s._x =3D xVal }
)
obj.x =3D 434;

// import specific class
importClass(java.util.Vector)

var v =3D new Vector(3)
print(v)

// import package
importPackage(java.io)

print(new File(".").getAbsolutePath())

// Using JavaAdapter to extend a Java class
var myVector =3D new JavaAdapter(java.util.Vector) {
     size: function() {
          print("size called!");
          return 0;
     }
};

myVector.size();

// toSource function to get source code
print(print.toSource())
=20

loading compatibility script so that script runs on rhino and nashorn
=20
// if you want the script run both on rhino and nashorn
try {
    load("nashorn:mozilla_compat.js");
} catch (e) {}

// Or you can check for importClass function and then load if missing ...
if (typeof importClass !=3D "function") {
   load("nashorn:mozilla_compat.js");
}
=20

------=_Part_3770_1595701755.1495502451358--