Annotation Interface JS
Replaces the body of a Java method with JavaScript code. The JavaScript code is specified by the
value()
of this annotation. A method with this annotation can be made native
,
since its Java body is not used at run-time (this is the convention).
Examples:
The data types in JavaScript are different from the data types in Java, so this annotation defines a mapping between the Java and the JavaScript data types. The mapping is 1:1, meaning that each Java data type maps to exactly one JavaScript, and vice versa. This mapping determines how Java arguments are converted to JavaScript, and how JavaScript return values are converted back to Java values. The following Java and JavaScript type pairs correspond to each other:@JS("console.log('User message: '.concat(message));")
public static native void reportToUser(String message);@JS("return obj.hashCode();")
public static native Integer hashCode(Object obj);
- Java
null
value and the JavaScriptnull
value. - Java
JSUndefined
and JavaScriptUndefined
. - Java
JSBoolean
and JavaScriptBoolean
. - Java
JSNumber
and JavaScriptNumber
. - Java
JSBigInt
and JavaScriptBigInt
. - Java
JSString
and JavaScriptString
. - Java
JSSymbol
and JavaScriptSymbol
. - Java
JSObject
and JavaScriptObject
. - Any other Java class and the corresponding JavaScript
Proxy
for that class.
JSValue
is the common supertype of JSUndefined
, JSBoolean
,
JSNumber
, JSBigInt
, JSString
, JSSymbol
and JSObject
. It
defined conversion methods such as JSValue.asInt()
and JSValue.asString()
, which
allow converting certain JSValue
objects to corresponding Java objects (for example,
JSNumber
can be converted to most Java numeric types).
Other Java classes are transformed to JavaScript Proxy
objects, which expose JavaScript
keys that correspond to methods of the underlying Java object. Such an object behaves as if it
were a regular JavaScript object, but its internal behavior is defined by the corresponding Java
code.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic @interface
A snippet of JavaScript code that must be included in the image.static @interface
When this annotation is used together with theJS
annotation, the arguments and return values of the respective method undergo implicit Java-to-JavaScript and JavaScript-to-Java conversions.static @interface
Denotes that the annotated Java class should be exported from the VM class.static @interface
Denotes that the annotated class represents a JavaScript class from the global scope. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The default arguments string is an empty string used to denote the default value of theargs
member. -
Required Element Summary
Required Elements -
Optional Element Summary
Optional Elements
-
Field Details
-
DEFAULT_ARGUMENTS
The default arguments string is an empty string used to denote the default value of theargs
member. It is an error for the user of theJS
annotation to specify an empty-string name of an argument, hence this special value is used to denote the defaults.- See Also:
-
-
Element Details
-
value
String valueJavaScript statements that form the body of this method. When returning a value, thereturn
keyword must not be omitted. The JavaScript code can use parameters using their names specified inargs()
. In non-static methods, it can also access the receiver usingthis
keyword.- Returns:
- a snippet of JavaScript source code
-
args
String[] argsIf specified, overrides parameter names seen by the JavaScript code. By default, Web Image will try to infer the original argument names in the method header from the method bytecode. If that is not possible, the argument names must be specified here. The target method must be compiled withjavac -parameters
in order for the argument names to be available in the bytecode. Thethis
parameter cannot be renamed.- Returns:
- the new parameter names (their count must match the number of parameters), or
DEFAULT_ARGUMENTS
if the original names should be used (this is the default)
- Default:
{""}
-