Syntax of Java calls in @JavaScriptBody

Hardcore developers of DukeScript are using the @JavaScriptBody annotation for creating their own APIs. Calling JavaScript code like this is pretty straightforward.

It’s slightly more difficult if you want to make calls from JavaScript to Java. The first thing is to remember setting the javacall parameter to true:

@JavaScriptBody(
    args = { "msg", "callback" }, 
    javacall = true, 
    body = "if (confirm(msg)) {\n"
         + "  callback.@java.lang.Runnable::run()();\n"
         + "}\n"
)
public static native void confirmByUser(String msg, Runnable callback);

The second difficulty is the syntax of the call itself. It follows this pattern:

[instance-expr.]@class-name::method-name(param-signature)(arguments)

instance-expr. : must be present when calling an instance method and must be absent when calling a static method class-name : the fully-qualified name of the class in which the method is declared (or a subclass thereof) param-signature : the internal Java method signature as specified at JNI Type Signatures but without the trailing signature of the method return type since it is not needed to choose the overload arguments : is the actual argument list to pass to the called method

Most of it is very simple except the param-signature. It describes the type of params passed to the java call. But with this table it should be easy to understand as well:

Type Signature Java Type
Z boolean
B byte
C char
S short
I int
J long
F float
D double
L fully-qualified-class ; fully-qualified-class
[ type type[]

So the Java method:

public void doSomething (int n, String s, int[] arr);

has the following type signature:

(ILjava/lang/String;[I)