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> T
Executes the function with the givenmethodName
and theargs
in the JavaScript object.void
close()
Closes this JavaScript object.frame()
Returns theFrame
instance of this JavaScript object.boolean
hasProperty
(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.<T> Optional<T>
Returns anOptional
describing the value of the JavaScript object's property with the givenname
or an emptyOptional
if 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.boolean
putProperty
(String name, Object value) Creates a new property with the givenname
or updates the existing one in the current JavaScript object and returnstrue
if the property with the givenname
was created or updated successfully.boolean
removeProperty
(String name) Removes a property with the givenname
in the JavaScript object and returnstrue
if the property was successfully removed.
-
Method Details
-
frame
Frame frame()Returns theFrame
instance 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
-
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:
true
if object has property with the givenname
- Throws:
IllegalArgumentException
- whenname
is empty or blankObjectClosedException
- when the JavaScript object is already disposed or invalid
-
property
Returns anOptional
describing the value of the JavaScript object's property with the givenname
or an emptyOptional
if there is no such property or its value is undefined.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 |
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
Optional
describing the value of the JavaScript object's property with the givenname
. If the object does not have this property or the property value isnull
or undefined, the method returns an emptyOptional
- Throws:
IllegalArgumentException
- whenname
is empty or blankObjectClosedException
- when the JavaScript object is already disposed or invalid- See Also:
-
putProperty
Creates a new property with the givenname
or updates the existing one in the current JavaScript object and returnstrue
if the property with the givenname
was created or updated successfully.The type mapping rules are the following:
| Java | JavaScript | |--------------------|-------------------------------| | Number | Double | | 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 theJsAccessibleTypes
class.Java collections that are not made accessible to JavaScript using the
JsAccessible
annotation or via theJsAccessibleTypes
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 theJsAccessibleTypes
class 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:
true
when property was created or updated successfully- Throws:
IllegalArgumentException
- whenname
is empty or blankIllegalArgumentException
- whenvalue
is aJsObject
from a different web page or frameObjectClosedException
- when the JavaScript object is already disposed or invalid
-
removeProperty
Removes a property with the givenname
in the JavaScript object and returnstrue
if 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:
true
if the property was successfully removed- Throws:
IllegalArgumentException
- whenname
is empty or blankObjectClosedException
- when the JavaScript object is already disposed or invalid
-
call
Executes the function with the givenmethodName
and theargs
in the JavaScript object. This method blocks current thread execution until the function finishes its execution. If the function raises an exception, thenJsException
with 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 | | 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 theJsAccessibleTypes
class.Java collections that are not made accessible to JavaScript using the
JsAccessible
annotation or via theJsAccessibleTypes
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 theJsAccessibleTypes
class 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 | | String | String | | Boolean | Boolean | | 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
- whenmethodName
is empty or blankIllegalArgumentException
- whenargs
contains 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
-