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
nullvalue and the JavaScriptnullvalue. - Java
JSUndefinedand JavaScriptUndefined. - Java
JSBooleanand JavaScriptBoolean. - Java
JSNumberand JavaScriptNumber. - Java
JSBigIntand JavaScriptBigInt. - Java
JSStringand JavaScriptString. - Java
JSSymboland JavaScriptSymbol. - Java
JSObjectand JavaScriptObject. - Any other Java class and the corresponding JavaScript
Proxyfor 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 @interfaceA snippet of JavaScript code that must be included in the image.static @interfaceWhen this annotation is used together with theJSannotation, the arguments and return values of the respective method undergo implicit Java-to-JavaScript and JavaScript-to-Java conversions.static @interfaceDenotes that the annotated Java class should be exported from the VM class.static @interfaceDenotes that the annotated class represents a JavaScript class from the global scope. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe default arguments string is an empty string used to denote the default value of theargsmember. -
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 theargsmember. It is an error for the user of theJSannotation 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, thereturnkeyword 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 usingthiskeyword.- 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 -parametersin order for the argument names to be available in the bytecode. Thethisparameter cannot be renamed.- Returns:
- the new parameter names (their count must match the number of parameters), or
DEFAULT_ARGUMENTSif the original names should be used (this is the default)
- Default:
{""}
-