Hi all,

First of all I would like to take this opportunity to wish a happy new
year to all Inline users and their families.

Now on with business. In the last Inline::Java version (0.44), significant
changes where brought (thanks in part to many users on this list) to the
way callbacks are handled within Inline::Java. But the way callbacks are
invoked, i.e.:

   Object rv = CallPerl("my::package", "my_sub", new Object [] {"arg1",
"arg2"}) ;

is still somewhat cumbersome. During the holidays I did a bit of research
(and also looked a bit at JPL) and came up with a cleaner way to define
callbacks and invoke them: I call this "PerlNatives".

Let's look at a realistic example with the way callbacks are invoked
currently:

  use Inline Java => <<'END' ;
     import java.util.* ;
     import org.perl.inline.java.* ;
     import javax.swing.* ;
     import java.awt.event.* ;

     class Button extends InlineJavaPerlCaller
                      implements ActionListener {
        public Button() throws InlineJavaException {
           JFrame frame = new JFrame("Button") ;
           frame.setSize(50,50) ;
           JButton button = new JButton("Click Me!") ;
           frame.getContentPane().add(button) ;
           button.addActionListener(this) ;
           frame.show() ;
        }

        public void actionPerformed(ActionEvent e){
           try {
              CallPerl("main", "button_pressed", new Object [] {}) ;
           }
           catch (InlineJavaPerlException pe){ }
           catch (InlineJavaException pe) { pe.printStackTrace() ;}
        }
     }
  END

my $b = new Button() ;

  sub button_pressed {
     print("click!\n") ; # prints click!
  }


With PerlNatives, you do it like this:


  use Inline Java => <<'END' ;
     import java.util.* ;
     import org.perl.inline.java.* ;
     import javax.swing.* ;
     import java.awt.event.* ;

     class Button extends InlineJavaPerlNatives
                      implements ActionListener {
        public Button() throws InlineJavaException {
           JFrame frame = new JFrame("Button") ;
           frame.setSize(50,50) ;
           JButton button = new JButton("Click Me!") ;
           frame.getContentPane().add(button) ;
           button.addActionListener(this) ;
           frame.show() ;
        }

        public void actionPerformed(ActionEvent e){
           button_pressed() ;
        }

        native public void button_pressed() ;
     }
  END

my $b = new Button() ;

  sub Button::perl::button_pressed {
     my $this = shift ;
     print("click!\n") ; # prints click!
  }


So what has changed here?


First of all, the Button class now extends InlineJavaPerlNatives instead
of InlineJavaPerlCaller. Basically all this does is tell Java that all
methods declared "native" in this class will be implemented (or more
specifically mapped) to a subroutine in Perl.

Secondly, we defined a new native method in our class called
button_pressed. Since this method is in the class Button (which extends
InlineJavaPerlNatives), Inline::Java will know that this method is mapped
to the Button::perl::button_pressed subroutine in Perl (the addition of
the ::perl subpackage is necessary or else you will get into a infinite
loop (remember that since Button.button_pressed is public, Inline::Java
has already mapped to Button::button_pressed!)).

Thirdly, we simply call button_pressed as if it where a normal Java method.

Of course there are a few limitations with PerlNatives:
- You can't have 2 native methods with the same name in a class
- Native methods can only return void or Object types (no primitive
types). This is somewhat anoying but can be easily worked around using the
wrapper classes (Integer, Double, ...)
- (certainly others to be discovered later...)

That's basically it. I still have some implementation details to iron out
and some tiny portability issues, but I've got everything working pretty
well.

Note: For those of you who are wondering why I'm so preoccupied with
callbacks, I just want to say that one day I would like people to use
Inline::Java to call PERL from JAVA. Callbacks (which in this case won't
be callbacks at all) will then become very important.

Anyways, I just wanted to get some feedback on this before I got to far...

Thanks,

Patrick

----------------------------------
| Patrick LeBoutillier
| [EMAIL PROTECTED]

_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE* http://join.msn.com/?page=dept/bcomm&pgmarket=en-ca&RU=http%3a%2f%2fjoin.msn.com%2f%3fpage%3dmisc%2fspecialoffers%26pgmarket%3den-ca




Reply via email to