Question about XThis

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

Question about XThis

neo anderson
I encounter a problem when obtaining Java object from beanshell script. The error shows

java.lang.ClassCastException: bsh.XThis cannot be cast to example.TransactionService

code snippet used to create that instance is

init_txservice(){

        import example.TransactionService;

        ts = new TransactionService(accountdao.initialize());

        return this;
}

txservice = init_txservice();

After re-reading the bsh docs, in `Scripted Objects ' (http://www.beanshell.org/manual/bshmanual.html ) it says that this can be used to create scripted object using method closures. So it looks like my code to create txservice is not correct; And when I switch to return ts ( ts = new TransactionServcie(...) ) the error seems to go away.

But now a question raises. I use `return this' in other method closure which has similar way to return instance, but why other code snippet does not throw the same error i.e. ClassCastException?

Other code like

init_mock_datasource(){

        import org.clin4j.framework.domain.mock.Driver;
       
        d = "com.mockdb.jdbc.Driver";
        url = "jdbc:mockdb://localhost/ecommerce";
        id = "user";
        pass = "password";

        driver = new Driver(d, url, id, pass);

        return this;
}


when objecting java object using this code snippet, everything works well.

Does any reason can explain this problem?

Thanks for help.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question about XThis

Alexey Zinger
In the second example, what are you casting the object you get from BeanShell to?

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





________________________________
From: neo anderson <[hidden email]>
To: [hidden email]
Sent: Sat, November 14, 2009 2:13:45 PM
Subject: [Beanshell-users]  Question about XThis


I encounter a problem when obtaining Java object from beanshell script. The
error shows

java.lang.ClassCastException: bsh.XThis cannot be cast to
example.TransactionService

code snippet used to create that instance is

init_txservice(){

    import example.TransactionService;

    ts = new TransactionService(accountdao.initialize());

    return this;
}

txservice = init_txservice();

After re-reading the bsh docs, in `Scripted Objects '
(http://www.beanshell.org/manual/bshmanual.html ) it says that this can be
used to create scripted object using method closures. So it looks like my
code to create txservice is not correct; And when I switch to return ts ( ts
= new TransactionServcie(...) ) the error seems to go away.

But now a question raises. I use `return this' in other method closure which
has similar way to return instance, but why other code snippet does not
throw the same error i.e. ClassCastException?

Other code like

init_mock_datasource(){

    import org.clin4j.framework.domain.mock.Driver;
   
        d = "com.mockdb.jdbc.Driver";
        url = "jdbc:mockdb://localhost/ecommerce";
        id = "user";
        pass = "password";

    driver = new Driver(d, url, id, pass);

    return this;
}


when objecting java object using this code snippet, everything works well.

Does any reason can explain this problem?

Thanks for help.
--
View this message in context: http://old.nabble.com/Question-about-XThis-tp26352954p26352954.html
Sent from the BeanShell - User mailing list archive at Nabble.com.


------------------------------------------------------------------------------
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question about XThis

Christophe Vanfleteren
In reply to this post by neo anderson
Shouldn't you return ts instead of this? ie:

 init_txservice(){

        import example.TransactionService;

        ts = new TransactionService(accountdao.initialize());

        return ts;
 }

txservice = init_txservice();

On Sat, Nov 14, 2009 at 8:13 PM, neo anderson
<[hidden email]> wrote:

>
> I encounter a problem when obtaining Java object from beanshell script. The
> error shows
>
> java.lang.ClassCastException: bsh.XThis cannot be cast to
> example.TransactionService
>
> code snippet used to create that instance is
>
> init_txservice(){
>
>        import example.TransactionService;
>
>        ts = new TransactionService(accountdao.initialize());
>
>        return this;
> }
>
> txservice = init_txservice();
>
> After re-reading the bsh docs, in `Scripted Objects '
> (http://www.beanshell.org/manual/bshmanual.html ) it says that this can be
> used to create scripted object using method closures. So it looks like my
> code to create txservice is not correct; And when I switch to return ts ( ts
> = new TransactionServcie(...) ) the error seems to go away.
>
> But now a question raises. I use `return this' in other method closure which
> has similar way to return instance, but why other code snippet does not
> throw the same error i.e. ClassCastException?
>
> Other code like
>
> init_mock_datasource(){
>
>        import org.clin4j.framework.domain.mock.Driver;
>
>        d = "com.mockdb.jdbc.Driver";
>        url = "jdbc:mockdb://localhost/ecommerce";
>        id = "user";
>        pass = "password";
>
>        driver = new Driver(d, url, id, pass);
>
>        return this;
> }
>
>
> when objecting java object using this code snippet, everything works well.
>
> Does any reason can explain this problem?
>
> Thanks for help.
> --
> View this message in context: http://old.nabble.com/Question-about-XThis-tp26352954p26352954.html
> Sent from the BeanShell - User mailing list archive at Nabble.com.
>
>
> ------------------------------------------------------------------------------
> 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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question about XThis

neo anderson
Yes. That's the thing that goes weird.

Now I've changed every return instance to its own variable, instead of this. For instance,

 init_txservice(){ ... return ts; } // return instance of ts

not

 init_txservice(){ ... return this; }

The problem now goes away. But what I am curious is that previously each code block is

init_xxxxxx(){ ... return this; }

They all return `this'. It does compile and no error thrown out until I add the code block - init_txservice() { ... }

If return this is wrong. Why other code block get compiled? If it is not worng, why init_txservice() { ... }  throws error?

Thanks again for your help.

I appreciate it.
Christophe Vanfleteren wrote
Shouldn't you return ts instead of this? ie:

 init_txservice(){

        import example.TransactionService;

        ts = new TransactionService(accountdao.initialize());

        return ts;
 }

txservice = init_txservice();

On Sat, Nov 14, 2009 at 8:13 PM, neo anderson
<javadeveloper999@yahoo.co.uk> wrote:
>
> I encounter a problem when obtaining Java object from beanshell script. The
> error shows
>
> java.lang.ClassCastException: bsh.XThis cannot be cast to
> example.TransactionService
>
> code snippet used to create that instance is
>
> init_txservice(){
>
>        import example.TransactionService;
>
>        ts = new TransactionService(accountdao.initialize());
>
>        return this;
> }
>
> txservice = init_txservice();
>
> After re-reading the bsh docs, in `Scripted Objects '
> (http://www.beanshell.org/manual/bshmanual.html ) it says that this can be
> used to create scripted object using method closures. So it looks like my
> code to create txservice is not correct; And when I switch to return ts ( ts
> = new TransactionServcie(...) ) the error seems to go away.
>
> But now a question raises. I use `return this' in other method closure which
> has similar way to return instance, but why other code snippet does not
> throw the same error i.e. ClassCastException?
>
> Other code like
>
> init_mock_datasource(){
>
>        import org.clin4j.framework.domain.mock.Driver;
>
>        d = "com.mockdb.jdbc.Driver";
>        url = "jdbc:mockdb://localhost/ecommerce";
>        id = "user";
>        pass = "password";
>
>        driver = new Driver(d, url, id, pass);
>
>        return this;
> }
>
>
> when objecting java object using this code snippet, everything works well.
>
> Does any reason can explain this problem?
>
> Thanks for help.
> --
> View this message in context: http://old.nabble.com/Question-about-XThis-tp26352954p26352954.html
> Sent from the BeanShell - User mailing list archive at Nabble.com.
>
>
> ------------------------------------------------------------------------------
> 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
> Beanshell-users@lists.sourceforge.net
> 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
Beanshell-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question about XThis

David Lee
It all depends on what your goals and expectations are .
You can make really nice "classes" out of functions by returning this.

foo() {
    int a = 1 ;
    int b = 2;
    return this ;
}

c = foo();

print(c.a);

1



----
What you were trying to do is entirely different, a function that simply
creates *another object* and returns *that*
Thats entirely different from the "XThis" concept.

You can have it both ways.
If you want to mix the 2 concepts you could do this :

init_txservice(){

       
        example.TransactionService ts =
                new example.TransactionService.TransactionService(accountdao.initialize());

        return this;
 }

myservice = init_txservice();

myservice.ts.doSomething()















David A. Lee
[hidden email]  
http://www.calldei.com
http://www.xmlsh.org
812-482-5224



neo anderson wrote:

> Yes. That's the thing that goes weird.
>
> Now I've changed every return instance to its own variable, instead of this.
> For instance,
>
>  init_txservice(){ ... return ts; } // return instance of ts
>
> not
>
>  init_txservice(){ ... return this; }
>
> The problem now goes away. But what I am curious is that previously each
> code block is
>
> init_xxxxxx(){ ... return this; }
>
> They all return `this'. It does compile and no error thrown out until I add
> the code block - init_txservice() { ... }
>
> If return this is wrong. Why other code block get compiled? If it is not
> worng, why init_txservice() { ... }  throws error?
>
> Thanks again for your help.
>
> I appreciate it.
>
> Christophe Vanfleteren wrote:
>  
>> Shouldn't you return ts instead of this? ie:
>>
>>  init_txservice(){
>>
>>         import example.TransactionService;
>>
>>         ts = new TransactionService(accountdao.initialize());
>>
>>         return ts;
>>  }
>>
>> txservice = init_txservice();
>>
>> On Sat, Nov 14, 2009 at 8:13 PM, neo anderson
>> <[hidden email]> wrote:
>>    
>>> I encounter a problem when obtaining Java object from beanshell script.
>>> The
>>> error shows
>>>
>>> java.lang.ClassCastException: bsh.XThis cannot be cast to
>>> example.TransactionService
>>>
>>> code snippet used to create that instance is
>>>
>>> init_txservice(){
>>>
>>>        import example.TransactionService;
>>>
>>>        ts = new TransactionService(accountdao.initialize());
>>>
>>>        return this;
>>> }
>>>
>>> txservice = init_txservice();
>>>
>>> After re-reading the bsh docs, in `Scripted Objects '
>>> (http://www.beanshell.org/manual/bshmanual.html ) it says that this can
>>> be
>>> used to create scripted object using method closures. So it looks like my
>>> code to create txservice is not correct; And when I switch to return ts (
>>> ts
>>> = new TransactionServcie(...) ) the error seems to go away.
>>>
>>> But now a question raises. I use `return this' in other method closure
>>> which
>>> has similar way to return instance, but why other code snippet does not
>>> throw the same error i.e. ClassCastException?
>>>
>>> Other code like
>>>
>>> init_mock_datasource(){
>>>
>>>        import org.clin4j.framework.domain.mock.Driver;
>>>
>>>        d = "com.mockdb.jdbc.Driver";
>>>        url = "jdbc:mockdb://localhost/ecommerce";
>>>        id = "user";
>>>        pass = "password";
>>>
>>>        driver = new Driver(d, url, id, pass);
>>>
>>>        return this;
>>> }
>>>
>>>
>>> when objecting java object using this code snippet, everything works
>>> well.
>>>
>>> Does any reason can explain this problem?
>>>
>>> Thanks for help.
>>> --
>>> View this message in context:
>>> http://old.nabble.com/Question-about-XThis-tp26352954p26352954.html
>>> Sent from the BeanShell - User mailing list archive at Nabble.com.
>>>
>>>
>>> ------------------------------------------------------------------------------
>>> 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
>>
>>
>>    
>
>  
------------------------------------------------------------------------------
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: Question about XThis

Gary M
In reply to this post by neo anderson
Can you confirm that it's this line where the ClassCastException is
thrown when you return this?

On 11/22/2009 8:59 AM, neo anderson wrote:
> >  txservice = init_txservice();
>    

------------------------------------------------------------------------------
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...