Scripted interfaces for something other than an Interpreter

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Scripted interfaces for something other than an Interpreter

David Walend
Sorry for reposting this. I never saw a reply.

http://beanshell.org/manual/ 
embeddedmode.html#Embedding_BeanShell_in_Your_Application talks about  
how to get the beanshell interpreter to implement an interface in  
compiled Java code:

// script the method globally
i.eval( "actionPerformed( e ) { print( e ); }");

// Get a reference to the script object (implementing the interface)
ActionListener scriptedHandler =
     (ActionListener)i.eval("return (ActionListener)this");

// Use the scripted event handler normally...
new JButton.addActionListener( scriptedHandler );

Can other beanshell objects that implement an interface be used in  
compiled Java code?

// Get a reference to the script object (implementing the interface)
ActionListener scriptedHandler =
     (ActionListener)i.eval("return (ActionListener)
myScriptedActionListenerThatIsNotThis");

I've tried it this way and it doesn't seem to quite work. I get a  
"ClassCastException: global$2" or the like.

Thanks,

Dave


David Walend
[hidden email]




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Scripted interfaces for something other than an Interpreter

james.m.kortge
Hi Dave,

It seems to work for me.  The only potential problem is classloading, of
course...

I wrote this little test program to try different things:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import bsh.Interpreter;
public class Foo {
      public static void main(String[] args) throws Exception {
            Interpreter i = new Interpreter();

            i.eval("run() { print(\"scripted run()\"); }");
            Runnable runnable = (Runnable)i.eval("return (Runnable)this");
            runnable.run(); // prints 'scripted run()'

            i.eval("actionHandler = new ActionListener() {
actionPerformed(evt) { print(evt); } }");
            ActionListener actionHandler = (ActionListener)i.eval("return
(ActionListener)actionHandler");
            actionHandler.actionPerformed(new ActionEvent(new Object(),
ActionEvent.ACTION_PERFORMED, "whatever"));

            i.setClassLoader(Foo.class.getClassLoader()); // so BeanShell
can load the same Callable class
            i.eval("handler = new Callable() { callback(obj) {
print(\"callback w/arg=\"+obj); } };");
            Callable handler = (Callable)i.eval("return
(Callable)handler");
            handler.callback("ack"); // prints 'callback w/arg=ack'
      }
}
interface Callable {
      public void callback(Object obj);
}


I had to add the call to setClassLoader() before the 3rd chunk in order to
get BeanShell to load the "same" Callable class, where "same" is defined as
"the same fully qualified classname in the same classloader."

HTH,
Jim Kortge



                                                                                                                                             
                      David Walend <[hidden email]>                                                                                        
                      Sent by:                                To:       [hidden email]                                
                      [hidden email]        cc:                                                                            
                      ceforge.net                             Subject:  [Beanshell-users] Scripted interfaces for something other than an    
                                                               Interpreter                                                                  
                                                                                                                                             
                      03/11/2006 10:58 AM                                                                                                    
                                                                                                                                             
                                                                                                                                             




Sorry for reposting this. I never saw a reply.

http://beanshell.org/manual/
embeddedmode.html#Embedding_BeanShell_in_Your_Application talks about
how to get the beanshell interpreter to implement an interface in
compiled Java code:

// script the method globally
i.eval( "actionPerformed( e ) { print( e ); }");

// Get a reference to the script object (implementing the interface)
ActionListener scriptedHandler =
     (ActionListener)i.eval("return (ActionListener)this");

// Use the scripted event handler normally...
new JButton.addActionListener( scriptedHandler );

Can other beanshell objects that implement an interface be used in
compiled Java code?

// Get a reference to the script object (implementing the interface)
ActionListener scriptedHandler =
     (ActionListener)i.eval("return (ActionListener)
myScriptedActionListenerThatIsNotThis");

I've tried it this way and it doesn't seem to quite work. I get a
"ClassCastException: global$2" or the like.

Thanks,

Dave


David Walend
[hidden email]




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live
webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users





-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users