« December 2009
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today
XML

Blog::Navigation

GetJava Download Button
Get the Source
Personal Blog

Blog::Referers

Today's Page Hits: 1080

Powered by Roller Weblogger.
« doesNotUnderstand in... | Main | Implementing Java... »
20051101 Tuesday November 01, 2005

4 ways to implement Java interface in JavaScript

With Mustang's JavaScript engine, there are 4 ways to implement Java interface in script:

  1. JavaAdapter function
    
        var r = new JavaAdapter(java.lang.Runnable,
                   { 
                       run: function() { print('hello'); } 
                   });
        r.run();
    
    
    This feature may also be used with Java anonymous class-like syntax as shown below:
    
        var r = new java.lang.Runnable() { 
                   run: function() { 
                       print('hello') 
                   }
                };
        r.run();
    
    
    Unlike, Rhino's JavaAdapter, Mustang's JavaAdapter does not support implementing multiple Java interfaces. Also, extending a super class is not allowed.
  2. using engine variable in jrunscript (or exposing ScriptEngine object as global variable and using it in script)
    
        var x = { run: function() { print('hello') } }
        var r = engine.getInterface(java.lang.Runnable, x);
        r.run();
    
    
  3. pass script function whenever a Java interface type parameter is required! (this works from Rhino 1.6R2 onwards -- so, you have to wait for Mustang build 59 or above for this)
    
        function run() { 
            print('hello'); 
        }
        // script function is wrapped automatically
        var t = new java.lang.Thread(run); 
        t.start();   
    
    
    This feature works only if all the methods of the interface have the same signature or there is only one method in the interface. For interface with multiple methods, script function receives the name of the method as first parameter.
  4. using JSAdapter and JavaAdapter.
    
        function Invoker(obj) {
            return new JSAdapter() {
                __has__: function(name) {
                    return true;
                },
                __get__: function(name) {
                    return function() {
                        return obj.invoke(name, arguments);
                    }
                }
            }
        }
    
        var r = { 
               invoke: function(name, args) { 
                   print(name + " called"); 
               }
            };
        var x = new JavaAdapter(java.lang.Runnable, Invoker(r));
        x.run(); // this calls r.invoke('run');
    
    
    This scheme even if interface methods don't have identical signature. But, invoke method has to handle variable number of arguments passed as args array (second argument). The first argument is the name of the interface method.



( Nov 01 2005, 08:28:51 AM IST ) Permalink del.icio.us | furl | simpy | slashdot | technorati | digg

Comments:

Post a Comment:

Comments are closed for this entry.
Copyright (C) 2005, A. Sundararajan's Weblog