A newbie question related to using bsh

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

A newbie question related to using bsh

neo anderson

I read the doc at http://www.beanshell.org/examples/callscript.html. But this arises me a question. How can I cast the result object into the type I want? For instance, in the script I finally have type A created, then in the Java application I obtained using the following statement

Object result = new bsh.Interpreter().source("myscript.bsh");

I can cast result to A if I only have one script e.g.

A a = (A) result;

However, how if I want to collect many scripts once and then cast all object collected into type desired?

Is there any way I can achieve this?

Thanks for help.




     

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: A newbie question related to using bsh

KUN LI

You can use "Collection" which can be a "List" or "Vector".

Example:

Collection<A> results = new Arraylist();

results.add(result);

Kun LI




> Date: Wed, 5 Aug 2009 15:22:02 +0000
> From: [hidden email]
> To: [hidden email]
> Subject: [Beanshell-users] A newbie question related to using bsh
>
>
> I read the doc at http://www.beanshell.org/examples/callscript.html. But this arises me a question. How can I cast the result object into the type I want? For instance, in the script I finally have type A created, then in the Java application I obtained using the following statement
>
> Object result = new bsh.Interpreter().source("myscript.bsh");
>
> I can cast result to A if I only have one script e.g.
>
> A a = (A) result;
>
> However, how if I want to collect many scripts once and then cast all object collected into type desired?
>
> Is there any way I can achieve this?
>
> Thanks for help.
>
>
>
>
>      
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> Beanshell-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/beanshell-users

_________________________________________________________________
Téléphonez gratuitement à tous vos proches avec Windows Live Messenger  !  Téléchargez-le maintenant !
http://www.windowslive.fr/messenger/1.asp
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: A newbie question related to using bsh

neo anderson

But how if those scripts would return variant types? For instance, there are 3 bsh scripts I want to collect. So in the Java application I might scan the dir and use

for(...){
Object result = bsh.Interpreter().source(BshFileNames);
}

So the result object would be different kind of type e.g. class A, class B and Class C.

Is there any chance that I can cast them dynamically?

Thanks to reply my question.

--- On Wed, 5/8/09, KUN LI <[hidden email]> wrote:

> From: KUN LI <[hidden email]>
> Subject: RE: [Beanshell-users] A newbie question related to using bsh
> To: [hidden email], "beanshell - users" <[hidden email]>
> Date: Wednesday, 5 August, 2009, 3:34 PM
>
>
>
> #yiv1311021128 .hmmessage P
> {
> margin:0px;padding:0px;}
> #yiv1311021128 {
> font-size:10pt;font-family:Verdana;}
>
>
>  
> You can use "Collection" which can be a
> "List" or "Vector".
>
> Example:
>
> Collection<A> results = new Arraylist();
>
> results.add(result);
>
> Kun LI
>
>
>
>
> > Date: Wed, 5 Aug 2009 15:22:02 +0000
> > From: [hidden email]
> > To: [hidden email]
> > Subject: [Beanshell-users] A newbie question related
> to using bsh
> >
> >
> > I read the doc at
> http://www.beanshell.org/examples/callscript.html. But this
> arises me a question. How can I cast the result object into
> the type I want? For instance, in the script I finally have
> type A created, then in the Java application I obtained
> using the following statement
> >
> > Object result = new
> bsh.Interpreter().source("myscript.bsh");
> >
> > I can cast result to A if I only have one script e.g.
> >
> > A a = (A) result;
> >
> > However, how if I want to collect many scripts once
> and then cast all object collected into type desired?
> >
> > Is there any way I can achieve this?
> >
> > Thanks for help.
> >
> >
> >
> >
> >      
> >
> >
> ------------------------------------------------------------------------------
> > Let Crystal Reports handle the reporting - Free
> Crystal Reports 2008 30-Day
> > trial. Simplify your report design, integration and
> deployment - and focus on
> > what you do best, core application coding. Discover
> what's new with
> > Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> > _______________________________________________
> > Beanshell-users mailing list
> > [hidden email]
> >
> https://lists.sourceforge.net/lists/listinfo/beanshell-users
>
> Discutez sur Messenger où que vous soyez ! Mettez
> Messenger sur votre mobile !
>


     

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: A newbie question related to using bsh

KUN LI

 If you can know the type of every script or get his type by a method, use a HashMap, with a key who is his type.
HashMap<String, Object>   myScripts = new HashMap();

for(...){
 Object result = bsh.Interpreter().source(BshFileName);
 String type = getType(result);
 myScripts.put(type, result);
 }

now you could get each result with a key (his type).

Kun LI




> Date: Wed, 5 Aug 2009 15:40:52 +0000
> From: [hidden email]
> To: [hidden email]
> Subject: Re: [Beanshell-users] A newbie question related to using bsh
>
>
> But how if those scripts would return variant types? For instance, there are 3 bsh scripts I want to collect. So in the Java application I might scan the dir and use
>
> for(...){
> Object result = bsh.Interpreter().source(BshFileNames);
> }
>
> So the result object would be different kind of type e.g. class A, class B and Class C.
>
> Is there any chance that I can cast them dynamically?
>
> Thanks to reply my question.
>
> --- On Wed, 5/8/09, KUN LI <[hidden email]> wrote:
>
> > From: KUN LI <[hidden email]>
> > Subject: RE: [Beanshell-users] A newbie question related to using bsh
> > To: [hidden email], "beanshell - users" <[hidden email]>
> > Date: Wednesday, 5 August, 2009, 3:34 PM
> >
> >
> >
> > #yiv1311021128 .hmmessage P
> > {
> > margin:0px;padding:0px;}
> > #yiv1311021128 {
> > font-size:10pt;font-family:Verdana;}
> >
> >
> >  
> > You can use "Collection" which can be a
> > "List" or "Vector".
> >
> > Example:
> >
> > Collection<A> results = new Arraylist();
> >
> > results.add(result);
> >
> > Kun LI
> >
> >
> >
> >
> > > Date: Wed, 5 Aug 2009 15:22:02 +0000
> > > From: [hidden email]
> > > To: [hidden email]
> > > Subject: [Beanshell-users] A newbie question related
> > to using bsh
> > >
> > >
> > > I read the doc at
> > http://www.beanshell.org/examples/callscript.html. But this
> > arises me a question. How can I cast the result object into
> > the type I want? For instance, in the script I finally have
> > type A created, then in the Java application I obtained
> > using the following statement
> > >
> > > Object result = new
> > bsh.Interpreter().source("myscript.bsh");
> > >
> > > I can cast result to A if I only have one script e.g.
> > >
> > > A a = (A) result;
> > >
> > > However, how if I want to collect many scripts once
> > and then cast all object collected into type desired?
> > >
> > > Is there any way I can achieve this?
> > >
> > > Thanks for help.
> > >
> > >
> > >
> > >
> > >      
> > >
> > >
> > ------------------------------------------------------------------------------
> > > Let Crystal Reports handle the reporting - Free
> > Crystal Reports 2008 30-Day
> > > trial. Simplify your report design, integration and
> > deployment - and focus on
> > > what you do best, core application coding. Discover
> > what's new with
> > > Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> > > _______________________________________________
> > > Beanshell-users mailing list
> > > [hidden email]
> > >
> > https://lists.sourceforge.net/lists/listinfo/beanshell-users
> >
> > Discutez sur Messenger où que vous soyez ! Mettez
> > Messenger sur votre mobile !
> >
>
>
>      
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> Beanshell-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/beanshell-users

_________________________________________________________________
Découvrez toutes les possibilités de communication avec vos proches
http://www.microsoft.com/windows/windowslive/default.aspx
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: A newbie question related to using bsh

Alexey Zinger
In reply to this post by neo anderson
Dynamically, the JVM doesn't care about the type of reference to any object.  And of course, each object knows its own type anyway.  If you wish to execute a set of scripts not knowing what their return objects will be, you'll just have to put them in some collection of Object: Set<?>/ List<?>.

Presumably, since you expect to do something with those result objects, you'll have some idea about their types.  So you can then cast as you retrieve them from said collection:

List<?> myResults = ...;
Number howMany = (Number)myResults.get(0);
Date when = (Date)myResults.get(1);

Make sense?

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





________________________________
From: Neo Anderson <[hidden email]>
To: [hidden email]
Sent: Wednesday, August 5, 2009 11:40:52 AM
Subject: Re: [Beanshell-users] A newbie question related to using bsh


But how if those scripts would return variant types? For instance, there are 3 bsh scripts I want to collect. So in the Java application I might scan the dir and use

for(...){
Object result = bsh.Interpreter().source(BshFileNames);
}

So the result object would be different kind of type e.g. class A, class B and Class C.

Is there any chance that I can cast them dynamically?

Thanks to reply my question.

--- On Wed, 5/8/09, KUN LI <[hidden email]> wrote:

> From: KUN LI <[hidden email]>
> Subject: RE: [Beanshell-users] A newbie question related to using bsh
> To: [hidden email], "beanshell - users" <[hidden email]>
> Date: Wednesday, 5 August, 2009, 3:34 PM
>
>
>
> #yiv1311021128 .hmmessage P
> {
> margin:0px;padding:0px;}
> #yiv1311021128 {
> font-size:10pt;font-family:Verdana;}
>
>
>  
> You can use "Collection" which can be a
> "List" or "Vector".
>
> Example:
>
> Collection<A> results = new Arraylist();
>
> results.add(result);
>
> Kun LI
>
>
>
>
> > Date: Wed, 5 Aug 2009 15:22:02 +0000
> > From: [hidden email]
> > To: [hidden email]
> > Subject: [Beanshell-users] A newbie question related
> to using bsh
> >
> >
> > I read the doc at
> http://www.beanshell.org/examples/callscript.html. But this
> arises me a question. How can I cast the result object into
> the type I want? For instance, in the script I finally have
> type A created, then in the Java application I obtained
> using the following statement
> >
> > Object result = new
> bsh.Interpreter().source("myscript.bsh");
> >
> > I can cast result to A if I only have one script e.g.
> >
> > A a = (A) result;
> >
> > However, how if I want to collect many scripts once
> and then cast all object collected into type desired?
> >
> > Is there any way I can achieve this?
> >
> > Thanks for help.
> >
> >
> >
> >
> >      
> >
> >
> ------------------------------------------------------------------------------
> > Let Crystal Reports handle the reporting - Free
> Crystal Reports 2008 30-Day
> > trial. Simplify your report design, integration and
> deployment - and focus on
> > what you do best, core application coding. Discover
> what's new with
> > Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> > _______________________________________________
> > Beanshell-users mailing list
> > [hidden email]
> >
> https://lists.sourceforge.net/lists/listinfo/beanshell-users
>
> Discutez sur Messenger où que vous soyez ! Mettez
> Messenger sur votre mobile !
>


     

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users



     
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Loading...