getting BeanShell closures from Java

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

getting BeanShell closures from Java

wally.chang
 
 
Hi,
I am relatively new to BeanShell, and I was wondering if there was any way to get a reference to a BeanShell closure via Java (Assuming a BeanShell interpreter was embedded)?
 
for example, if my bsh script were:
 
   foo(){
            doSomething();
            return this();
   }
 
I want to be able to do something like this in my java class:
 
   Closure closure = (Closure)interp.get("foo");
   closure.invoke(new Object[]{});// no arguments
 
I tried just using interp.eval("foo()") and getting the XThis object, but I don't want foo to be executed until I call it from the Java code. Having an Interpreter instance running for each Closure doesn't seem like a good idea either.
 
Any suggestions?
 
 
Reply | Threaded
Open this post in threaded view
|

Re: getting BeanShell closures from Java

Steve McFarlin
I am not sure if this is what you are looking for...

Take a look at the NameSpace and BshMethod classes. If you source a
script  and then  get the namespace class from the intrepreter, you can
get at all the functions in the script. i.e.

bsh.source("foo.bsh") ;
NameSpace ns = bsh.getNameSpace() ;
BshMethod[] methods = ns.getMethods() ;

for(int i = 0; i < methods.length; i++) {
    System.out.println(methods[i].getName()) ;
}

The Method class contains all the functionality you would ever need to
inspect the (global) functions of the script. In so far as linking class
methods... i am not sure if this is possible or not.

Hope this helps...

steve




[hidden email] wrote:

>  
>  
> Hi,
> I am relatively new to BeanShell, and I was wondering if there was any
> way to get a reference to a BeanShell closure via Java (Assuming a
> BeanShell interpreter was embedded)?
>  
> for example, if my bsh script were:
>  
>    foo(){
>             doSomething();
>             return this();
>    }
>  
> I want to be able to do something like this in my java class:
>  
>    Closure closure = (Closure)interp.get("foo");
>    closure.invoke(new Object[]{});// no arguments
>  
> I tried just using interp.eval("foo()") and getting the XThis object,
> but I don't want foo to be executed until I call it from the Java
> code. Having an Interpreter instance running for each Closure doesn't
> seem like a good idea either.
>  
> Any suggestions?
>  
>  




-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.  Get Certified Today
Register for a JBoss Training Course.  Free Certification Exam
for All Training Attendees Through End of 2005. For more info visit:
http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users