Re: How to access objects from the script?
Alexey Zinger <inline_four-/[email protected]> Thu, 16 Mar 2006 09:49:40 -0800 (PST)
| Newsgroups | gmane.comp.java.beanshell.devel |
|---|---|
| Message-ID | <[email protected]> |
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 <brianhks-BYHubErIwDGZvNlLnfxc/[email protected]> 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
> > Beanshell-developers-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> > https://lists.sourceforge.net/lists/listinfo/beanshell-developers
> >
> > begin:vcard
> fn:Brian Hawkins
> n:Hawkins;Brian
> email;internet:brianhks-BYHubErIwDGZvNlLnfxc/[email protected]
> 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