Describing the importance of specifying the correct signature
authorJaroslav Tulach <jtulach@netbeans.org>
Wed, 13 May 2015 10:21:37 +0300
changeset 93764f89b8b7244
parent 936 b5bad52d9d49
child 938 22997afdeb75
Describing the importance of specifying the correct signature
boot/src/main/java/net/java/html/js/package.html
     1.1 --- a/boot/src/main/java/net/java/html/js/package.html	Tue May 12 21:58:37 2015 +0300
     1.2 +++ b/boot/src/main/java/net/java/html/js/package.html	Wed May 13 10:21:37 2015 +0300
     1.3 @@ -160,6 +160,43 @@
     1.4          compilation fails. Thus don't despair seeing the syntax, you'll get early
     1.5          warnings when there is a typo.
     1.6          
     1.7 +        <h3>Overloaded Methods</h3>
     1.8 +        
     1.9 +        Specifying the actual callback signature is important in case of 
    1.10 +        overloaded methods. Imagine a class:
    1.11 +<pre>
    1.12 +<b>package</b> x.y.z;
    1.13 +<b>class</b> Handler {
    1.14 +  <b>int</b> pulse() {
    1.15 +    <b>return</b> 1;
    1.16 +  }
    1.17 +  <b>int</b> pulse(<b>int</b> howMuch) {
    1.18 +    <b>return</b> howMuch;
    1.19 +  }
    1.20 +  <b>int</b> pulse(<b>long</b> evenMore) {
    1.21 +    <b>return</b> (<b>int</b>) (5 + evenMore);
    1.22 +  }
    1.23 +}</pre>        
    1.24 +        you then need to choose in {@link net.java.html.js.JavaScriptBody} 
    1.25 +        the appropriate method to call:
    1.26 +<pre>
    1.27 +{@code @}{@link net.java.html.js.JavaScriptBody}(args = { "h" }, javacall = <b>true</b>, <em>// you want to process the @ syntax</em>
    1.28 +  body = "<b>return</b> h.@x.y.z.Handler::pulse()() +" + <em>// the call to no argument method</em>
    1.29 +    "h.@x.y.z.Handler::pulse(I)(10) +" + <em>// the call to method with integer argument</em>
    1.30 +    "h.@x.y.z.Handler::pulse(J)(10);" <em>// the call to method with long argument</em>
    1.31 +  )
    1.32 +  <b>static native void</b> threePulsesFromJavaScript(Handler h);
    1.33 +  <b>static</b> {
    1.34 +    <b>assert</b> 26 == threePulsesFromJavaScript(<b>new</b> Handler());
    1.35 +  }
    1.36 +</pre>
    1.37 +        <p>
    1.38 +        To avoid ambiguity, the specification of the correct signature is 
    1.39 +        required on every call. However, to simplify the development,
    1.40 +        there is an annotation processor to
    1.41 +        verify the signature really refers to an existing method.
    1.42 +        </p>
    1.43 +        
    1.44          <h3>Arrays by Copy</h3>
    1.45          
    1.46          It is possible to exchange arrays between Java and JavaScript. Some