Skip to content

Java Global Object

Attila Szegedi edited this page Mar 16, 2021 · 5 revisions

Nashorn exposes a non-standard global object named Java that is the primary API entry point into Java platform-specific functionality. You can use it to create instances of Java classes, convert from Java arrays to native arrays and back, and so on. Below is the full alphabetical list of methods it provides:

Method Description
asJSONCompatible(obj) Returns a Java wrapper object (implementing JSObject) that is compatible with expectations of JSON libraries in Java; namely, that if it itself, or any object transitively reachable through it is an ECMAScript array, then such objects will be exposed as JSObject that also implements the java.util.List interface for exposing the array elements. An explicit API is required as otherwise Nashorn exposes all objects externally as JSObject that also implements the java.util.Map interface instead. By using this method, script arrays will be exposed as Lists and all other objects as Maps. See also explicit conversions.
extend(type[, type...[, obj]]) Returns a type object for a subclass of the specified Java class (and/or implementation of the specified interface(s)) that acts as a script-to-Java adapter for it/them. Note that you can also implement interfaces and subclass abstract classes using JavaScript new operator on a type object for an interface or abstract class. However, to extend a non-abstract class, or implement multiple interfaces, you will have to use this method. See Java Type Objects for details.
from(javaObj) Given a Java array or any java.util.Collection, returns an ECMAScript array with a shallow copy of its contents. Note that in most cases, you can use Java arrays and lists natively in Nashorn; in cases where for some reason you need to have an actual script native array (e.g. to work with the array comprehensions functions), you will want to use this method.
isJavaFunction(obj) Returns true if obj represents a Java function (a lambda).
isJavaMethod(obj) Returns true if obj represents a method on a Java object.
isJavaObject(obj) Returns true if obj is a Java object (and not an ECMAScript object).
isScriptObject(obj) Returns true if obj is an ECMAScript object (and not a Java object). It is almost the inverse of isJavaObject except that both return false for null.
isType(obj) Returns true if obj is a Java type object, e.g. one returned by the type or extend methods.
synchronized(func, obj) Returns a function that, when invoked, synchronizes on obj before invoking func.
to(obj, type) Given a script object and a Java type, converts the script object into the desired Java type. Currently it supports conversion to Java arrays, as well as wrapping of objects in java.util.List, java.util.Dequeue, java.util.Queue, and java.util.Collection interfaces. If conversion is not possible or fails for some reason, TypeError is thrown. See also explicit conversions.
type(obj) Given a name of a Java type, returns an object representing that type in Nashorn. Type objects in Nashorn are objects that you can use with the new operator as constructors to create new instances of the class as well as to access static members of the class. See the page on Java Type Objects for details.
typeName(obj) Returns the name of the type represented by obj if it is either a type object or a Java Class object. Returns undefined otherwise.
_super(obj) When given an object created using extend or equivalent mechanism (that is, any JavaScript-to-Java adapter), returns an object that can be used to invoke superclass methods on that object.
Clone this wiki locally