Java Reference
In-Depth Information
Table 12-2. The List of Mapping Between Classes of Script Objects and Java Objects
Script Object's Type
Java Object's Type
jdk.nashorn.internal.runtime.Undefined
Undefined
null
Null
java.lang.Number
Number
java.lang.Boolean
Boolean
java.lang.String
Primitive String type
jdk.nashorn.api.scripting.ScriptObjectMirror
Any script object
The same as the Java object in the script
Any Java object
Note that you are not supposed to use the classes and interfaces in the jdk.nashorn.
internal package and its subpackages. They are listed in the table only for informational
purposes. In this table, the mapping for the Nashorn Number type is shown as java.
lang.Number type. Although Nashorn tries to pass java.lang.Integer and j ava.lang.
Double objects for numeric value whenever possible, it is not reliable to depend on such
specialized Java types. You should expect a java.lang.Number instance when a Nashorn
Number is passed from a script to Java code. Whenever necessary, values of Number and
Boolean types in Nashorn are converted to the corresponding Java primitive types, such
as int , double , boolean , and so on.
In Nashorn, a script object is represented as an instance of the jdk.nashorn.
internal.runtime.ScriptObject class. When the script object is passed to Java code, it
is wrapped in a ScriptObjectMirror object. Before JDK8u40, if you passed a script object
to a Java method that declared its parameter as java.lang.Object type, a ScriptObject
was passed, not a ScriptObjectMirror . JDK8u40 has changed this behavior to pass a
script object to Java code as a ScriptObjectMirror every time. If you had declared the
method's parameter as JSObject or ScriptObjectMirror in Java, Nashorn always passed
the script object to Java as a ScriptObjectMirror .
It is possible for a script to pass the value undefined to Java code. You can check
if a script object passed to Java is undefined using the isUndefined(Object obj) static
method of the ScriptObjectMirror class. the method returns true if the specified obj is
undefined ; otherwise, it returns false.
Tip
The ScriptObjectMirror class implements the JSObject and Bindings interfaces.
The class adds more methods to deal with script objects. You can store the reference of
a script object in Java code in any of the three types of variables: JSObject , Bindings ,
or ScriptObjectMirror . What type you use depends on your needs. Using the
ScriptObjectMirror type gives you more flexibility and access to all features of the script
object. Table 12-3 contains the list of methods declared in the JSObject interface.
 
Search WWH ::




Custom Search