Interface JsObject
- All Known Subinterfaces:
JsArray,JsArrayBuffer,JsFunction,JsMap,JsPromise,JsSet
Allows accessing the object's properties and calling its functions. The JavaScript object is
alive while the web page where the object was created is loaded. When the web page is unloaded,
all the JavaScript objects will be automatically disposed. Access to the already disposed
JavaScript object will lead to the IllegalStateException.
-
Method Summary
Modifier and TypeMethodDescription<T> TExecutes the function with the givenmethodNameand theargsin the JavaScript object.voidclose()Closes this JavaScript object.frame()Returns theFrameinstance of this JavaScript object.booleanhasProperty(JsSymbol jsSymbol) Checks whether this JavaScript object or any of its prototypes has a property with the givenjsSymbolkey.booleanhasProperty(String name) Checks whether this JavaScript object or any of its prototypes has an enumerable property with the givenname.Returns an immutable list of the names of the properties of this JavaScript object, both enumerable and non-enumerable, excluding the properties from the prototype objects.Returns an immutable list of the symbol properties of this JavaScript object, excluding the properties from the prototype objects.<T> Optional<T>Returns anOptionaldescribing the value of the JavaScript object's property with the givenjsSymbolkey or an emptyOptionalif there is no such property or its value is undefined.<T> Optional<T>Returns anOptionaldescribing the value of the JavaScript object's property with the givennameor an emptyOptionalif there is no such property or its value is undefined.Returns an immutable list of the names of the enumerable properties of this JavaScript object, including the properties from the prototype objects.booleanputProperty(JsSymbol jsSymbol, Object value) Creates a new property with the givenjsSymbolkey or updates the existing one in the current JavaScript object and returnstrueif the property with the givennamewas created or updated successfully.booleanputProperty(String name, Object value) Creates a new property with the givennameor updates the existing one in the current JavaScript object and returnstrueif the property with the givennamewas created or updated successfully.booleanremoveProperty(JsSymbol jsSymbol) Removes a property with the givenjsSymbolkey in the JavaScript object and returnstrueif the property was successfully removed.booleanremoveProperty(String name) Removes a property with the givennamein the JavaScript object and returnstrueif the property was successfully removed.
-
Method Details
-
frame
Frame frame()Returns theFrameinstance of this JavaScript object.- Since:
- 7.6
-
propertyNames
Returns an immutable list of the names of the enumerable properties of this JavaScript object, including the properties from the prototype objects.The list returned by this method contains the same values as would be enumerated by a for-in statement over this object in JavaScript.
- Returns:
- a list of the names of the properties of this JavaScript object or an empty list if the object does not have any properties
- Throws:
ObjectClosedException- when the JavaScript object is already disposed or invalid
-
ownPropertyNames
Returns an immutable list of the names of the properties of this JavaScript object, both enumerable and non-enumerable, excluding the properties from the prototype objects.- Returns:
- a list of the names of the own properties of this JavaScript object or an empty list if the object does not have any own properties
- Throws:
ObjectClosedException- when the JavaScript object is already disposed or invalid
-
ownPropertySymbols
Returns an immutable list of the symbol properties of this JavaScript object, excluding the properties from the prototype objects.- Returns:
- a list of the symbol properties of this JavaScript object or an empty list if the object does not have any properties
- Throws:
ObjectClosedException- when the JavaScript object is already disposed or invalid- Since:
- 8.13.0
-
hasProperty
Checks whether this JavaScript object or any of its prototypes has an enumerable property with the givenname.- Parameters:
name- the name of the property- Returns:
trueif object has property with the givenname- Throws:
IllegalArgumentException- whennameis empty or blankObjectClosedException- when the JavaScript object is already disposed or invalid
-
hasProperty
Checks whether this JavaScript object or any of its prototypes has a property with the givenjsSymbolkey.- Parameters:
jsSymbol- a symbol that represents the property key- Returns:
trueif object has property with the givenjsSymbol- Throws:
IllegalArgumentException- whenjsSymbolis from a different web page or frameObjectClosedException- when the JavaScript object orjsSymbolis already disposed or invalid- Since:
- 8.13.0
-
property
Returns anOptionaldescribing the value of the JavaScript object's property with the givennameor an emptyOptionalif there is no such property or its value is undefined.The type mapping rules are the following:
| JavaScript | Java | |--------------------|----------------| | Number | Double | | BigInt | BigInteger | | String | String | | Boolean | Boolean | | Symbol | JsSymbol | | null and undefined | null | | Node | JsObject, Node | | ArrayBuffer | JsArrayBuffer | | Array | JsArray | | Set | JsSet | | Map | JsMap | | Object | JsObject | | Proxy Object | Object |
Proxy objects are mapped to the corresponding injected Java object.
- Type Parameters:
T- the result type according to the type mapping rules- Parameters:
name- a string that represents the name of the property or function- Returns:
- an
Optionaldescribing the value of the JavaScript object's property with the givenname. If the object does not have this property or the property value isnullor undefined, the method returns an emptyOptional - Throws:
IllegalArgumentException- whennameis empty or blankObjectClosedException- when the JavaScript object is already disposed or invalid- See Also:
-
property
Returns anOptionaldescribing the value of the JavaScript object's property with the givenjsSymbolkey or an emptyOptionalif there is no such property or its value is undefined.- Type Parameters:
T- the result type according to the type mapping rules- Parameters:
jsSymbol- a symbol that represents the property key- Returns:
- an
Optionaldescribing the value of the JavaScript object's property with the givenjsSymbolkey. If the object does not have this property or the property value isnullor undefined, the method returns an emptyOptional - Throws:
IllegalArgumentException- whenjsSymbolis from a different web page or frameObjectClosedException- when the JavaScript object orjsSymbolis already disposed or invalid- Since:
- 8.13.0
- See Also:
-
putProperty
Creates a new property with the givennameor updates the existing one in the current JavaScript object and returnstrueif the property with the givennamewas created or updated successfully.The type mapping rules are the following:
| Java | JavaScript | |--------------------|-------------------------------| | Number | Double | | BigInt | BigInteger | | String | String | | Boolean | Boolean | | Symbol | JsSymbol | | 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
JsAccessibleannotation or via theJsAccessibleTypesclass.Java collections that are not made accessible to JavaScript using the
JsAccessibleannotation or via theJsAccessibleTypesclass 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
JsAccessibleannotation or via theJsAccessibleTypesclass are wrapped into a JavaScript proxy object. Such proxy objects can be used to modify the collection in Java.- Parameters:
name- a string that represents the property namevalue- a new value associated with the property with the givenname- Returns:
truewhen property was created or updated successfully- Throws:
IllegalArgumentException- whennameis empty or blankIllegalArgumentException- whenvalueis aJsObjectfrom a different web page or frameObjectClosedException- when the JavaScript object is already disposed or invalid
-
putProperty
Creates a new property with the givenjsSymbolkey or updates the existing one in the current JavaScript object and returnstrueif the property with the givennamewas created or updated successfully.The properties with
Symbolkeys are not enumerable and are not returned inpropertyNames().- Parameters:
jsSymbol- a symbol that represents the property keyvalue- a new value associated with the property with the givenjsSymbol- Returns:
truewhen property was created or updated successfully- Throws:
IllegalArgumentException- whennameis empty or blankIllegalArgumentException- whenvalueis aJsObjectfrom a different web page or frame, orjsSymbolis from a different web page or frameObjectClosedException- when the JavaScript object orjsSymbolis already disposed or invalid- Since:
- 8.13.0
-
removeProperty
Removes a property with the givennamein the JavaScript object and returnstrueif the property was successfully removed. Once you remove the property, it will not be available in the current JavaScript object anymore.- Parameters:
name- a string that represents the name of the property or function- Returns:
trueif the property was successfully removed- Throws:
IllegalArgumentException- whennameis empty or blankObjectClosedException- when the JavaScript object is already disposed or invalid
-
removeProperty
Removes a property with the givenjsSymbolkey in the JavaScript object and returnstrueif the property was successfully removed. Once you remove the property, it will not be available in the current JavaScript object anymore.- Parameters:
jsSymbol- a symbol that represents the property key- Returns:
trueif the property was successfully removed- Throws:
IllegalArgumentException- whenjsSymbolis from a different web page or frameObjectClosedException- when the JavaScript object orjsSymbolis already disposed or invalid- Since:
- 8.13.0
-
call
Executes the function with the givenmethodNameand theargsin the JavaScript object. This method blocks current thread execution until the function finishes its execution. If the function raises an exception, thenJsExceptionwith an error message that describes the reason of the exception will be thrown. Same error message will be printed in JavaScript Console.The type mapping rules are the following:
| Java | JavaScript | |--------------------|-------------------------------| | Number | Double | | BigInt | BigInteger | | String | String | | Boolean | Boolean | | Symbol | JsSymbol | | 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
JsAccessibleannotation or via theJsAccessibleTypesclass.Java collections that are not made accessible to JavaScript using the
JsAccessibleannotation or via theJsAccessibleTypesclass 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
JsAccessibleannotation or via theJsAccessibleTypesclass are wrapped into a JavaScript proxy object. Such proxy objects can be used to modify the collection in Java.The type mapping rules are the following:
| JavaScript | Java | |--------------------|----------------| | Number | Double | | BigInt | BigInteger | | String | String | | Boolean | Boolean | | Symbol | JsSymbol | | null and undefined | null | | Node | JsObject, Node | | ArrayBuffer | JsArrayBuffer | | Array | JsArray | | Set | JsSet | | Map | JsMap | | Object | JsObject | | Proxy Object | Object |
Proxy objects are mapped to the corresponding injected Java object.
- Type Parameters:
T- the result of the JavaScript function execution- Parameters:
methodName- a string that represents a function nameargs- the input arguments that will be converted according to the mapping rules- Throws:
JsException- when an error occurs during function executionIllegalArgumentException- whenmethodNameis empty or blankIllegalArgumentException- whenargscontains an unsupported typeObjectClosedException- when the JavaScript object is already disposed or invalid
-
close
void close()Closes this JavaScript object.This method makes the underlying v8 object eligible for garbage collection, but does not guarantee that it will be destroyed. That is, if the object is still used by the JavaScript code on the web page (there are references to this object), you can obtain it again through the JavaScript API.
Use this method to make unused JavaScript objects garbage collectable. This is especially useful when you transfer a large number of objects through the JavaScript-Java bridge, or when those are objects that can hold a large amount of memory:
JsArrayBuffer,JsArray,JsSet,JsMap, etc.All JavaScript objects are closed automatically when you perform a navigation, reload the web page, or close the browser.
- Since:
- 7.32
-