public interface JsObject
Allows accessing the object's properties and calling its functions. The JavaScript object is
alive until 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
.
Modifier and Type | Method and Description |
---|---|
<T> T |
call(java.lang.String methodName,
java.lang.Object... args)
Executes the function with the given
methodName and the args in the
JavaScript object. |
boolean |
hasProperty(java.lang.String name)
Checks whether the JavaScript object has a property or function with the given
name . |
java.util.List<java.lang.String> |
ownPropertyNames()
Returns an immutable list of the names of the properties of this JavaScript object, excluding
properties from the prototype objects.
|
<T> java.util.Optional<T> |
property(java.lang.String name)
Returns an
Optional describing the value of the JavaScript object's property with the
given name or an empty Optional if there is no such property or the value is
undefined. |
java.util.List<java.lang.String> |
propertyNames()
Returns an immutable list of the names of the properties of this JavaScript object, including
properties from the prototype objects.
|
boolean |
putProperty(java.lang.String name,
java.lang.Object value)
Creates a new property with the given
name or updates the existing one in the current
JavaScript object and returns true if the property with the given name was
created or updated successfully. |
boolean |
removeProperty(java.lang.String name)
Removes a property with the given
name in the JavaScript object and returns true if the property was successfully removed. |
java.util.List<java.lang.String> propertyNames()
java.lang.IllegalStateException
- when the JavaScript object is already disposed or invalidjava.util.List<java.lang.String> ownPropertyNames()
java.lang.IllegalStateException
- when the JavaScript object is already disposed or invalidboolean hasProperty(java.lang.String name)
name
.name
- a string that represents the name of the property or functiontrue
if object has property with the given name
java.lang.IllegalArgumentException
- when name
is empty or blankjava.lang.IllegalStateException
- when the JavaScript object is already disposed or invalid<T> java.util.Optional<T> property(java.lang.String name)
Optional
describing the value of the JavaScript object's property with the
given name
or an empty Optional
if there is no such property or the value is
undefined. The value can be one of the following types: java.lang.Boolean
, java.lang.Double
, java.lang.String
, JsObject
, or EventTarget
.
The following rules are used to convert JavaScript into Java types:
java.lang.Double
java.lang.String
java.lang.Boolean
null
and undefined
to null
JsObject
JsObject
and EventTarget
T
- one of the following types: java.lang.Boolean
, java.lang.Double
,
java.lang.String
, JsObject
, or EventTarget
name
- a string that represents the name of the property or functionOptional
describing the value of the JavaScript object's property with the
given name
. If the object does not have this property or the property value is null
or undefined, the method returns an empty Optional
java.lang.IllegalArgumentException
- when name
is empty or blankjava.lang.IllegalStateException
- when the JavaScript object is already disposed or invalidhasProperty(String)
boolean putProperty(java.lang.String name, java.lang.Object value)
name
or updates the existing one in the current
JavaScript object and returns true
if the property with the given name
was
created or updated successfully.
The value can be one of the following types: java.lang.Boolean
, java.lang.Double
, java.lang.String
, JsObject
, or java.lang.Object
.
As the value
you can use an instance of the java.lang.Object
. In this
case the Java object will be automatically converted/wrapped into a JavaScript object. It
allows injecting Java objects into JavaScript and invoking the public methods of the injected
Java object from JavaScript code. For security reasons, only public methods annotated with
JsAccessible
can be accessed from JavaScript. The Java object's fields are not
accessible. If JavaScript calls the method that does not exist in the injected Java object or
the method isn't public and hasn't been annotated, then the call will throw an error.
The following rules are used to convert JavaScript into Java types:
java.lang.Double
is converted to JavaScript Number
java.lang.String
to JavaScript string
java.lang.Boolean
to JavaScript boolean
null
to JavaScript null
JsObject
to an appropriate JavaScript object
EventTarget
to an appropriate JavaScript DOM
Node object
java.lang.Object
will be wrapped into a JavaScript object
name
- a string that represents the property namevalue
- a new value associated with the property with the given name
true
when property was created or updated successfullyjava.lang.IllegalArgumentException
- when name
is empty or blankjava.lang.IllegalArgumentException
- when value
is a JsObject
from a different
web page or framejava.lang.IllegalStateException
- when the JavaScript object is already disposed or invalidboolean removeProperty(java.lang.String name)
name
in the JavaScript object and returns true
if the property was successfully removed. Once you remove the property, it will not be
available in the current JavaScript object anymore.name
- a string that represents the name of the property or functiontrue
if the property was successfully removedjava.lang.IllegalArgumentException
- when name
is empty or blankjava.lang.IllegalStateException
- when the JavaScript object is already disposed or invalid<T> T call(java.lang.String methodName, java.lang.Object... args)
methodName
and the args
in the
JavaScript object. This method blocks current thread execution until the function finishes
its execution. If the function raises an exception, then JsException
with an error
message that describes the reason of the exception will be thrown. Same error message will be
printed in JavaScript Console.T
- the result of the JavaScript function executionmethodName
- a string that represents a function nameargs
- the list of input arguments. The following types are supported: java.lang.Boolean
, java.lang.Double
, java.lang.String
,
JsObject
, or java.lang.Object
JsException
- when an error occurs during function executionjava.lang.IllegalArgumentException
- when methodName
is empty or blankjava.lang.IllegalArgumentException
- when args
contains an unsupported typejava.lang.IllegalStateException
- when the JavaScript object is already disposed or invalid