Arrgh! How to create a class in BSH and call from Java?

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

Arrgh! How to create a class in BSH and call from Java?

Michael Freeman
Could someone please post the simplest and most direct
example of how to create a Java class in some foo.bsh
and then invoke a class method from some bar.java?

I swear i have search all over including available
docs and this newsgroup but i swear i don't see how
one does this? (Are the use of namespaces also somehow
involved?)

--Mike

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 


-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&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: Arrgh! How to create a class in BSH and call from Java?

Brian Lalor
On Apr 30, 2006, at 4:14 PM, Michael Freeman wrote:

> Could someone please post the simplest and most direct
> example of how to create a Java class in some foo.bsh
> and then invoke a class method from some bar.java?

I think you'd create the interpreter in bar.java, evaluate a script,  
and then call the interpreter's get() method to retrieve an object  
from its context.  Is that what you're after?  Your scripted class  
would either have to implement an interface, or you'd have to use  
reflection to find the defined methods.

--
Brian Lalor
[hidden email]





-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&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: Arrgh! How to create a class in BSH and call from Java?

Jason Essington
In reply to this post by Michael Freeman
The way I do it is to start by having an interface that has known  
methods to use:

Then build the source based upon some external criteria (I use  
velocity templates to build java source at runtime, but you could  
just as easily load source from a file, or a database.) and have my  
class implement that interface. Lastly, your source needs to return  
an instance of the object.

so your bsh source would look something like:

        // todo add imports here
        public class MyClass implements MyInterface
        {
                //todo implement interface methods
        }
        return new MyClass();

then in your java code it is simply a matter of doing something like:

        Interpreter interpreter = new Interpreter();
        // I also had to do
        // interpreter.setClassLoader(getClass().getClassLoader());
        // because there was a bit if a classloading problem in 2.0b4
        MyInterface foo = (MyInterface) interpreter.eval(bshSource);

at which point, you've now got foo that was created from your source  
and you can treat it just like any other class. So if you had a doBaz
() method in your interface, you would simply:
       
        foo.doBaz();

If I am not mistaken, this requires a 2.0 version of BeanShell

-jason

On Apr 30, 2006, at 2:14 PM, Michael Freeman wrote:

> Could someone please post the simplest and most direct
> example of how to create a Java class in some foo.bsh
> and then invoke a class method from some bar.java?
>
> I swear i have search all over including available
> docs and this newsgroup but i swear i don't see how
> one does this? (Are the use of namespaces also somehow
> involved?)
>
> --Mike



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users