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:
booleanandBooleanare converted to a JavaScriptBoolean.byte(andByte),short(andShort),char(andCharacter),int(andInteger),float(andFloat),double(andDouble), are converted to a JavaScriptNumber.long,LongandBigIntegerare converted to a JavaScriptBigInt.Stringis converted to a JavaScriptString.- Any functional-interface object (whose class implements exactly one single abstract
method as defined by the
FunctionalInterfaceannotation) 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
JSValueclass 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
Booleancan be coerced to a JavabooleanandBoolean. - JavaScript
NumberandBigIntcan be coerced to a Javabyte(andByte),short(andShort),char(andCharacter),int(andInteger),long(andLong),float(andFloat), anddouble(andDouble),BigIntegerandBigDecimal. - JavaScript
Stringcan be coerced to a JavaString. - JavaScript typed array can be coerced to a corresponding Java primitive array, if there
is one. Concretely, a JavaScript
UInt8Arraycan be coerced to aboolean[]. A JavaScriptInt8Arraycan be coerced to abyte[]array. A JavaScriptInt16Arraycan be coerced to ashort[], aUint16Arraycan be coerced to achar[], aInt32Arraycan be coerced to aint[], aFloat32Arraycan be coerced to afloat[], aBigInt64Arraycan be coerced to along[], and aFloat64Arraycan be coerced to adouble[]. - JavaScript
Objectcan be coerced to any Java class that is a subclass ofJSObjectif that class conforms to that JavaScript object. - All other values are not coerced -- they are converted to the corresponding Java
JSValueclass, with the exception of JavaScriptProxyobjects 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).