Annotation Interface JS.Coerce
- Enclosing class:
JS
When this annotation is used together with the
JS
annotation, the arguments and
return values of the respective method undergo implicit Java-to-JavaScript and
JavaScript-to-Java conversions.
When the JS.Coerce
annotation is present, Java-value arguments to
JS
-annotated methods are implicitly converted (coerced) to the corresponding
JavaScript type (and more broadly, they are implicitly converted when JavaScript code calls a
Java method that has the JS.Coerce
annotation, and that method returns a value).
These conversions are done in a way such that no information is dropped -- for example, a
Java byte
value is coerced to a JavaScript Number
value, which is a wider
type.
The following Java types are coerced to JavaScript values as follows:
boolean
andBoolean
are converted to a JavaScriptBoolean
.byte
(andByte
),short
(andShort
),char
(andCharacter
),int
(andInteger
),float
(andFloat
),double
(andDouble
), are converted to a JavaScriptNumber
.long
,Long
andBigInteger
are converted to a JavaScriptBigInt
.String
is converted to a JavaScriptString
.- Any functional-interface object (whose class implements exactly one single abstract
method as defined by the
FunctionalInterface
annotation) is converted to a JavaScriptFunction
. - A primitive array is converted to corresponding JavaScript typed arrays. Concretely, an
array of type
boolean[]
is converted to a JavaScriptUint8Array
, abyte[]
array to a JavaScriptInt8Array
, ashort[]
array to a JavaScriptInt16Array
, achar[]
array to a JavaScriptUint16Array
, aint[]
array to a JavaScriptInt32Array
, afloat[]
array to a JavaScriptFloat32Array
, along[]
array to a JavaScriptBigInt64Array
, and adouble[]
array to a JavaScriptFloat64Array
. - All other values are not coerced -- subtypes of the Java
JSValue
class are converted to corresponding JavaScript values, and other objects are converted to JavaScript proxies.
JS.Coerce
annotation is present, JavaScript return values of
JS
-annotated methods, which originate from JavaScript code, are implicitly converted
(coerced) to the corresponding Java values (and more broadly, they are implicitly converted
when JavaScript code passes JavaScript arguments to a Java method that has JS.Coerce
annotation). The conversion is driven by the type that Java expects. For example, if the
return type of a JS
-annotated method is double
, then a JavaScript
Number
will be converted to a double
. If the return type is int
, then
the JavaScript Number
will be converted to an int
, even though this may
truncate the original JavaScript value. However, only certain JavaScript values can be
converted to certain Java types -- for example, a JavaScript String
is not converted
to a Java int
, and the attempt to return a JavaScript String
from a
JS
-annotated method whose return type is int
will throw a
ClassCastException
.
The following JavaScript types are coerced to the expected Java types as follows:
- JavaScript
Boolean
can be coerced to a Javaboolean
andBoolean
. - JavaScript
Number
andBigInt
can be coerced to a Javabyte
(andByte
),short
(andShort
),char
(andCharacter
),int
(andInteger
),long
(andLong
),float
(andFloat
), anddouble
(andDouble
),BigInteger
andBigDecimal
. - JavaScript
String
can be coerced to a JavaString
. - JavaScript typed array can be coerced to a corresponding Java primitive array, if there
is one. Concretely, a JavaScript
UInt8Array
can be coerced to aboolean[]
. A JavaScriptInt8Array
can be coerced to abyte[]
array. A JavaScriptInt16Array
can be coerced to ashort[]
, aUint16Array
can be coerced to achar[]
, aInt32Array
can be coerced to aint[]
, aFloat32Array
can be coerced to afloat[]
, aBigInt64Array
can be coerced to along[]
, and aFloat64Array
can be coerced to adouble[]
. - JavaScript
Object
can be coerced to any Java class that is a subclass ofJSObject
if that class conforms to that JavaScript object. - All other values are not coerced -- they are converted to the corresponding Java
JSValue
class, with the exception of JavaScriptProxy
objects that wrap Java objects (those are converted back to the original Java objects). A mismatch with the user-ascribed type will cause aClassCastException
.
java.lang.Integer
to JavaScript, and
JavaScript returns that object back, the returned integer object may not be reference-equal
to the original integer object).