How to access objects from the script?

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

How to access objects from the script?

Philip Cammin
Hello!

I would like to call different methods which are written in a beanscript
file at certain points in my application. For example i do call the
method "on_start()" in the "public static void main(String args[])"
method of my application. And i, ofcourse can give the method strings
and integers, but i did not find an example to give the method, a
reference to an object. Also i did not find out how to modfiy objects or
variable which are in the real java code from the script, if i only had
the reference to it.

I just want to offer the oppertunity for the users to get or set data in
the program at different points, i do not want to to script the whole
classes.

Thank you!


-------------------------------------------------------
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-developers mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-developers
Reply | Threaded
Open this post in threaded view
|

Re: How to access objects from the script?

Wade Chandler-6
--- Philip Cammin <[hidden email]>
wrote:

> Hello!
>
> I would like to call different methods which are
> written in a beanscript
> file at certain points in my application. For
> example i do call the
> method "on_start()" in the "public static void
> main(String args[])"
> method of my application. And i, ofcourse can give
> the method strings
> and integers, but i did not find an example to give
> the method, a
> reference to an object. Also i did not find out how
> to modfiy objects or
> variable which are in the real java code from the
> script, if i only had
> the reference to it.
>
> I just want to offer the oppertunity for the users
> to get or set data in
> the program at different points, i do not want to to
> script the whole
> classes.
>
> Thank you!
>
Read the java docs for Interpreter.  Java docs are
your friend as long as we're talking about hte same
thing.  You will be calling the scripts from your code
at some point, and you'll be instantiating an
Interpreter, unless some how you are using the
beanshell for your main application.  See the method
bsh.Interpreter.set, you'll set your objects before
you ever call eval or source.  Beyond that if you need
to have references to global objects or something from
multiple interpreters in different scripts you'll have
to carefully keep references in a static Map or some
other container so you can get to them.

Wade


-------------------------------------------------------
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-developers mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-developers
Reply | Threaded
Open this post in threaded view
|

Re: How to access objects from the script?

Brian Hawkins
In reply to this post by Philip Cammin
This is how I've done it.

I call set on the interpreter class to set an object into the
environment like so
m_interpreter.set("bshargs", args);

This sets args as the variable "bshargs" (this name can be anything);

Then I call

m_interpreter.eval("on_start(bshargs)");

This calls the "on_start" method and passes the bshargs variable that I
set earlier.  The object passed in can be any real or script object.

Brian

Philip Cammin wrote:

> Hello!
>
> I would like to call different methods which are written in a
> beanscript file at certain points in my application. For example i do
> call the method "on_start()" in the "public static void main(String
> args[])" method of my application. And i, ofcourse can give the method
> strings and integers, but i did not find an example to give the
> method, a reference to an object. Also i did not find out how to
> modfiy objects or variable which are in the real java code from the
> script, if i only had the reference to it.
>
> I just want to offer the oppertunity for the users to get or set data
> in the program at different points, i do not want to to script the
> whole classes.
>
> Thank you!
>
>
> -------------------------------------------------------
> 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-developers mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/beanshell-developers
>

brianhks.vcf (185 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to access objects from the script?

Alexey Zinger
All good answers and I'd just add one little trick that wasn't discussed much.
The issue of affecting references in JVM from inside a BeanShell script.  A
normal object reference one uses in an object or a method has the same
restrictions as far as mutating (can only touch public).  Generally speaking,
it's a much better idea to mutate objects' state via mutator methods as opposed
to public fields:

o.state = blah; // not recommended
o.setState(blah); // recommended

The reason for this is because a method can always be overriden to build in
some additional logic, such as firing events.  However, if, there is a need to
mutate a particular reference in memory one has to have some sort of wrapper.
In the example above, o is acting as a wrapper for its state reference.  But,
as I said, what if there's no class for this o wrapper?  One can use a length-1
array as a reference wrapper:

Object[] refWrap = new Object[1];
refWrap[0] = blah;
Interpreter i = new Interpreter();
i.set("refWrap", refWrap);
// now we can execute something in BSH and it'll
// have a chance to modify refWrap[0], which will
// take effect outside the BSH environment as well

--- Brian Hawkins <[hidden email]> wrote:

> This is how I've done it.
>
> I call set on the interpreter class to set an object into the
> environment like so
> m_interpreter.set("bshargs", args);
>
> This sets args as the variable "bshargs" (this name can be anything);
>
> Then I call
>
> m_interpreter.eval("on_start(bshargs)");
>
> This calls the "on_start" method and passes the bshargs variable that I
> set earlier.  The object passed in can be any real or script object.
>
> Brian
>
> Philip Cammin wrote:
>
> > Hello!
> >
> > I would like to call different methods which are written in a
> > beanscript file at certain points in my application. For example i do
> > call the method "on_start()" in the "public static void main(String
> > args[])" method of my application. And i, ofcourse can give the method
> > strings and integers, but i did not find an example to give the
> > method, a reference to an object. Also i did not find out how to
> > modfiy objects or variable which are in the real java code from the
> > script, if i only had the reference to it.
> >
> > I just want to offer the oppertunity for the users to get or set data
> > in the program at different points, i do not want to to script the
> > whole classes.
> >
> > Thank you!
> >
> >
> > -------------------------------------------------------
> > 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-developers mailing list
> > [hidden email]
> > https://lists.sourceforge.net/lists/listinfo/beanshell-developers
> >
> > begin:vcard
> fn:Brian Hawkins
> n:Hawkins;Brian
> email;internet:[hidden email]
> tel;home:801-754-1383
> tel;cell:801-361-2845
> x-mozilla-html:TRUE
> version:2.1
> end:vcard
>
>


Alexey
2001 Honda CBR600F4i (CCS)
1992 Kawasaki EX500
http://bsheet.sourceforge.net

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


-------------------------------------------------------
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-developers mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-developers