Cast and pure java sourcing

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

Cast and pure java sourcing

TOULA Michael

Hi list !

Let's have a look a this very  simple ".java" source code :

import bsh.Interpreter;

public class DummyCommand extends Command
{
        public DummyCommand()
        {
               
        }
       
        protected void execute()
        {
                System.out.println("Hello Beanshell");
        }

       
        public static void main(String[] args) throws Exception
        {
                try
                {
                        Interpreter i = new Interpreter();
                                       
                        //source this actual source code
                        i.source("/pathToMySource/DummyCommand.java");        
                       
                        //ask the interpreter to create a new Instance of DummyCommand (sourced)
                        Object o1 = i.eval("new DummyCommand()");        

                        System.out.println("Is o1 an Instance of DummyCommand ? " + (o1 instanceof DummyCommand));         //will print false
                        System.out.println("Is o1 an Instance of Command ? " + (o1 instanceof Command));                        //will print true
                }
                catch (Exception ex)
                {
                        ex.printStackTrace();
                }
        }        
}

As you can see in the comments o1 is not an instance of DummyCommand (but is an instance of Command which surprised me).

Now the question is, is there a way to  "force" o1 to be an instance of DummyCommand so that I can cast it to DummyCommand, and use this object in my java code completely transparently as if it was a regular DummyCommand object ?

I'm sorry if this question has already been asked. I've searched the archive with no results.



Best,
Mike

******************************************
Michael TOULA
Software Engineer

Dalim Software GmbH
Strassburger Str. 6
D-77694
Kehl am Rhein
GERMANY

tel:   +49 7851 919 612
fax:  +49 7851 735 76
web:   www.dalim.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: Cast and pure java sourcing

Archie Cobbs
Michael Toula wrote:

>                         //source this actual source code
>                         i.source("/pathToMySource/DummyCommand.java");  
>          
>                         //ask the interpreter to create a new Instance
> of DummyCommand (sourced)
>                         Object o1 = i.eval("new DummyCommand()");        
>
>                         System.out.println("Is o1 an Instance of
> DummyCommand ? " + (o1 instanceof DummyCommand));         //will print
> false
>
> As you can see in the comments o1 is not an instance of DummyCommand
> (but is an instance of Command which surprised me).
>
> Now the question is, is there a way to  "force" o1 to be an instance of
> DummyCommand so that I can cast it to DummyCommand, and use this object
> in my java code completely transparently as if it was a regular
> DummyCommand object ?

This behavior makes sense, as the two DummyCommand classes will
necessarily have different class loaders.

As a workaround, you could perhaps do something like this:

   i.set("dcl", DummyCommand.class);
   Object o1 = i.eval("dcl.newInstance()");

-Archie

__________________________________________________________________________
Archie Cobbs      *        CTO, Awarix        *      http://www.awarix.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: Cast and pure java sourcing

Paul Franz
In reply to this post by TOULA Michael
The answer is no. Because they are two different objects. What your code
is doing is asking if the object that is defined in Beanshell is the
same as the one compiled and of course the answer is going to be no. If
you hadn't said the source for the class is the source for creating the
new object then it should be the same. Because then it would just
instanciate that object.

Paul Franz

Michael Toula wrote:

>
> Hi list !
>
> Let's have a look a this very  simple ".java" source code :
>
> import bsh.Interpreter;
>
> public class DummyCommand extends Command
> {
>         public DummyCommand()
>         {
>                
>         }
>        
>         protected void execute()
>         {
>                 System.out.println("Hello Beanshell");
>         }
>
>        
>         public static void main(String[] args) throws Exception
>         {
>                 try
>                 {
>                         Interpreter i = new Interpreter();
>                                        
>                         //source this actual source code
>                         i.source("/pathToMySource/DummyCommand.java");
>        
>                        
>                         //ask the interpreter to create a new Instance
> of DummyCommand (sourced)
>                         Object o1 = i.eval("new DummyCommand()");        
>
>                         System.out.println("Is o1 an Instance of
> DummyCommand ? " + (o1 instanceof DummyCommand));         //will print
> false
>                         System.out.println("Is o1 an Instance of
> Command ? " + (o1 instanceof Command));                        //will
> print true
>                 }
>                 catch (Exception ex)
>                 {
>                         ex.printStackTrace();
>                 }
>         }        
> }
>
> As you can see in the comments o1 is not an instance of DummyCommand
> (but is an instance of Command which surprised me).
>
> Now the question is, is there a way to  "force" o1 to be an instance
> of DummyCommand so that I can cast it to DummyCommand, and use this
> object in my java code completely transparently as if it was a regular
> DummyCommand object ?
>
> I'm sorry if this question has already been asked. I've searched the
> archive with no results.
>
>
>
> Best,
> Mike
>
> ******************************************
> Michael TOULA
> Software Engineer
>
> Dalim Software GmbH
> Strassburger Str. 6
> D-77694
> Kehl am Rhein
> GERMANY
>
> tel:   +49 7851 919 612
> fax:  +49 7851 735 76
> web:   www.dalim.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
>  

-------------------------------------------------------------------------
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: Cast and pure java sourcing

Stewart Cambridge
In reply to this post by TOULA Michael
>From page 24 of the Beanshell manual, 1.3, pdf format:
"While BeanShell is compatible with standard Java
syntax for statements, expressions, and methods, you can't yet script
new Java classes within BeanShell.
Instead, BeanShell allows you to script objects as "method closures",
similar to the way it is done in Perl 5.x,
JavaScript, and other object−capable scripting languages."

I suggest you compile the class as normal and then do:
interpreter.getNameSpace().importClass( DummyCommand.class.getName() );
Object o1 = interpreter.eval("new DummyCommand()");

Just my 2p worth.

Stewart


On 08/09/06, Michael Toula <[hidden email]> wrote:

>
> Hi list !
>
> Let's have a look a this very  simple ".java" source code :
>
> import bsh.Interpreter;
>
> public class DummyCommand extends Command
> {
>         public DummyCommand()
>         {
>
>         }
>
>         protected void execute()
>         {
>                 System.out.println("Hello Beanshell");
>         }
>
>
>         public static void main(String[] args) throws Exception
>         {
>                 try
>                 {
>                         Interpreter i = new Interpreter();
>
>                         //source this actual source code
>
> i.source("/pathToMySource/DummyCommand.java");
>
>                         //ask the interpreter to create a new Instance of
> DummyCommand (sourced)
>                         Object o1 = i.eval("new DummyCommand()");
>
>                         System.out.println("Is o1 an Instance of
> DummyCommand ? " + (o1 instanceof DummyCommand));         //will print false
>                         System.out.println("Is o1 an Instance of Command ? "
> + (o1 instanceof Command));                        //will print true
>                 }
>                 catch (Exception ex)
>                 {
>                         ex.printStackTrace();
>                 }
>         }
> }
>
> As you can see in the comments o1 is not an instance of DummyCommand (but is
> an instance of Command which surprised me).
>
> Now the question is, is there a way to  "force" o1 to be an instance of
> DummyCommand so that I can cast it to DummyCommand, and use this object in
> my java code completely transparently as if it was a regular DummyCommand
> object ?
>
> I'm sorry if this question has already been asked. I've searched the archive
> with no results.
>
>
>
> Best,
> Mike
>
> ******************************************
> Michael TOULA
> Software Engineer
>
> Dalim Software GmbH
> Strassburger Str. 6
> D-77694
> Kehl am Rhein
> GERMANY
>
> tel:   +49 7851 919 612
> fax:  +49 7851 735 76
> web:   www.dalim.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
>
>
>

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