Interface JsArray

All Superinterfaces:
JsObject

public interface JsArray extends JsObject
A JavaScript array.

An array object can be passed between Java and JavaScript as a method argument or a return value. The object lifetime is bound to the lifetime of the frame this object belongs to. When the owner frame is unloaded, all the JavaScript objects are automatically disposed.

An attempt to access a disposed JavaScript object will result in IllegalStateException.

Since:
7.20
  • Method Details

    • set

      boolean set(long index, @Nullable Object element)
      Inserts the specified element into the array at the specified index.

      If there is an existing element at the specified index, it will be replaced. If the index exceeds the array size, the array will be extended and the elements between the last inserted item and the index will be set to undefined.

      The type mapping rules are the following:

      
       | Java               | JavaScript                    |
       |--------------------|-------------------------------|
       | Double             | Number                        |
       | String             | String                        |
       | Boolean            | Boolean                       |
       | null               | null                          |
       | JsObject           | Object                        |
       | Node               | Node                          |
       | List<?>            | Array or Proxy Object         |
       | Set<?>             | Set or Proxy Object           |
       | Map<?,?>           | Map or Proxy Object           |
       | byte[]             | ArrayBuffer                   |
       | Object             | Proxy Object                  |
       

      If you pass a non-primitive Java object to JavaScript, it will be converted into a "proxy" JavaScript object. Method and property calls to this object will be delegated to the Java object. For security reasons, JavaScript can access only those methods and fields of the injected Java object that are explicitly marked as accessible either using the JsAccessible annotation or via the JsAccessibleTypes class.

      Java collections that are not made accessible to JavaScript using the JsAccessible annotation or via the JsAccessibleTypes class are converted to JavaScript collections. The content of the converted collection is a deep copy of the Java collection. Modifications of the converted collection in JavaScript do not affect the collection in Java.

      Java collections that are made accessible to JavaScript using the JsAccessible annotation or via the JsAccessibleTypes class are wrapped into a JavaScript proxy object. Such proxy objects can be used to modify the collection in Java.

      Parameters:
      index - the index in the array to insert the element. Cannot be negative or exceed the maximum array size (232-1)
      element - the element to insert
      Returns:
      true if the element is successfully inserted, false otherwise
      Throws:
      IllegalArgumentException - if the element is negative or exceeds the maximum array size (232-1)
      IllegalArgumentException - when element is a JsObject from a different web page or a frame
      ObjectClosedException - when the JavaScript object is already disposed or invalid
    • get

      @Nullable <T> T get(long index)
      Returns the element by the specified index.

      The type mapping rules are the following:

       | JavaScript         | Java           |
       |--------------------|----------------|
       | Number             | Double         |
       | String             | String         |
       | Boolean            | Boolean        |
       | null and undefined | null           |
       | Node               | JsObject, Node |
       | ArrayBuffer        | JsArrayBuffer  |
       | Array              | JsArray        |
       | Set                | JsSet          |
       | Map                | JsMap          |
       | Object             | JsObject       |
       | Proxy Object       | Object         |
       

      For proxy objects, the corresponding injected Java object is returned.

      Type Parameters:
      T - the result type according to the type mapping rules
      Parameters:
      index - the element's index
      Returns:
      the element by the specified index
      Throws:
      IllegalArgumentException - when index is negative or exceeds the array size
      ObjectClosedException - when the JavaScript object is already disposed or invalid
    • length

      long length()
      Returns the number of elements in this array.

      The maximum size of an array in JavaScript equals to 232-1

      Throws:
      ObjectClosedException - when the JavaScript object is already disposed or invalid
    • toList

      List<Object> toList()
      Converts this array to a List.

      The type mapping rules for the array elements are the following:

      
       | JavaScript         | Java           |
       |--------------------|----------------|
       | Number             | Double         |
       | String             | String         |
       | Boolean            | Boolean        |
       | null and undefined | null           |
       | Node               | Node, JsObject |
       | ArrayBuffer        | byte[]         |
       | Array              | List<?>        |
       | Set                | Set<?>         |
       | Map                | Map<?,?>       |
       | Object             | JsObject       |
       | Proxy Object       | Object         |
       

      Proxy objects are mapped to the corresponding injected Java object.

      Returns:
      an unmodifiable list containing the converted array elements
      Throws:
      ObjectClosedException - when the JavaScript object is already disposed or invalid
    • toList

      <T> List<T> toList(Class<T> type)
      Converts this array to a List with the elements of the specified type.

      The type mapping rules for the array elements are the following:

      
       | JavaScript         | Java           |
       |--------------------|----------------|
       | Number             | Double         |
       | String             | String         |
       | Boolean            | Boolean        |
       | null and undefined | null           |
       | Node               | Node, JsObject |
       | ArrayBuffer        | byte[]         |
       | Array              | List<?>        |
       | Set                | Set<?>         |
       | Map                | Map<?,?>       |
       | Object             | JsObject       |
       | Proxy Object       | Object         |
       

      Proxy objects are mapped to the corresponding injected Java object.

      All elements in the array must be of a type convertible to the type T according to the mapping rules.

      Type Parameters:
      T - the type of the converted elements
      Parameters:
      type - the Java type to convert elements in this array to, according to the mapping rules
      Returns:
      an unmodifiable list containing the converted array elements
      Throws:
      IllegalArgumentException - if the elements in this array cannot be converted to the specified type
      ObjectClosedException - when the JavaScript object is already disposed or invalid