Loading .java Files

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

Loading .java Files

spring
Hi,

when I load a complete .java File, the global namespace has no methods:

public class Foo() {
  void bar() {
    //
  }
}

When I load this, the namespace has the method bar():

//public class Foo() {
  void bar() {
    //
  }
//}

Is this a bug or by design?

Thank you


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Loading .java Files

David Lee
By what mechanism are you "loading" a java file ?
and yes the behavior is right.
If a method is in a class then it's not in the global namespace it's in a
class.


----------------------------------------
David A. Lee
[hidden email]
http://www.xmlsh.org


-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
Sent: Sunday, June 26, 2011 6:50 AM
To: [hidden email]
Subject: [Beanshell-users] Loading .java Files

Hi,

when I load a complete .java File, the global namespace has no methods:

public class Foo() {
  void bar() {
    //
  }
}

When I load this, the namespace has the method bar():

//public class Foo() {
  void bar() {
    //
  }
//}

Is this a bug or by design?

Thank you


----------------------------------------------------------------------------
--
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Loading .java Files

David Lee
Ok you are using "eval".  The terminology ".java" confused me.
You are loading  a "beanshell" file  not a "java" file.  Although they are
often similar.

If you want to invoke a non-static method in a class you do it the same way
in beanshell as you do in java.
you need to instantiate the class to invoke the method.
   
    new Foo().bar()

I dont think you can do this with BSHMethod.invoke()
(You can with eval)

My prefered method of executing bsh from within java is to use the Interface
methods.
 e.g.
     (ICampaignScript) mInterpreter.getInterface(ICampaignScript.class);

then you can call bsh methods directly via the interface.

but again this only calls 'top level' methods.   To create classes and call
them you need to run eval() or to expose a top level method which calls
them.
I dont know of any way directly from the java API to do it.










----------------------------------------
David A. Lee
[hidden email]
http://www.xmlsh.org


-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
Sent: Sunday, June 26, 2011 7:41 AM
To: 'David Lee'
Subject: RE: [Beanshell-users] Loading .java Files


Hi David,

I use interpreter.eval(<content of .java file as string>).
I also tried interpreter.source(<path to java file>).

When it is in a class - how can i access its methods? I want to invoke them
via BshMethod#invoke(..).

Thank you

> By what mechanism are you "loading" a java file ?
> and yes the behavior is right.
> If a method is in a class then it's not in the global
> namespace it's in a
> class.
>
>
> ----------------------------------------
> David A. Lee
> [hidden email]
> http://www.xmlsh.org
>
>
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> Sent: Sunday, June 26, 2011 6:50 AM
> To: [hidden email]
> Subject: [Beanshell-users] Loading .java Files
>
> Hi,
>
> when I load a complete .java File, the global namespace has
> no methods:
>
> public class Foo() {
>   void bar() {
>     //
>   }
> }
>
> When I load this, the namespace has the method bar():
>
> //public class Foo() {
>   void bar() {
>     //
>   }
> //}
>
> Is this a bug or by design?
>
> Thank you
>
>
> --------------------------------------------------------------
> --------------
> --
> All of the data generated in your IT infrastructure is
> seriously valuable.
> Why? It contains a definitive record of application
> performance, security
> threats, fraudulent activity, and more. Splunk takes this
> data and makes
> sense of it. IT sense. And common sense.
> http://p.sf.net/sfu/splunk-d2d-c2
> _______________________________________________
> Beanshell-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/beanshell-users
>


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Loading .java Files

spring
> Ok you are using "eval".  The terminology ".java" confused me.
> You are loading  a "beanshell" file  not a "java" file.  
> Although they are
> often similar.

The only reason why I want to use .java-Files is that I can edit them in my
IDE with full IDE-support (autocomplete, syntax check etc).

> If you want to invoke a non-static method in a class you do
> it the same way
> in beanshell as you do in java.
> you need to instantiate the class to invoke the method.
>    
>     new Foo().bar()

I do not call the Bsh-class from Bsh, I call it from Java.

> I dont think you can do this with BSHMethod.invoke()
> (You can with eval)
>
> My prefered method of executing bsh from within java is to
> use the Interface
> methods.
>  e.g.
>      (ICampaignScript)
> mInterpreter.getInterface(ICampaignScript.class);
>
> then you can call bsh methods directly via the interface.

But my Bsh-Class does not implement an interface.

> but again this only calls 'top level' methods.   To create
> classes and call
> them you need to run eval() or to expose a top level method
> which calls
> them.

What do you mean with "top level"?



------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Loading .java Files

David Lee
> Ok you are using "eval".  The terminology ".java" confused me.
> You are loading  a "beanshell" file  not a "java" file.  
> Although they are
> often similar.

The only reason why I want to use .java-Files is that I can edit them in my
IDE with full IDE-support (autocomplete, syntax check etc).
------------------------------------

That is a worthwhile goal but I believe your making a fundamental mistake
assuming that beanshell is java.
It is not.   It is similar to java but it is not identical to java.
Using a Java IDE is useful but if it thinks your editing beanshell code it
will give you false information.


===========
What do you mean with "top level"?
===========

In java all methods must be in a class like

class Foo {
   void bar() {} ;
};

In Beanshell methods need not (and usually DO not ) reside in a class. I.e.

bar() {};


Java and Beanshell are quite different languages in this respect.

By "top level" I mean these methods which are not in classes. They are put
into the "global namespace" which is the namespace used by the
BSHMethod.invoke() method.


If you want to edit and dynamically compile and load pure java I suggest
using the Javac classes for this instead of beanshell.  Then you load the
bytecode with a classloader and it becomes java directly.


By "interfaces" I mean the Beanshell method of magically converting bsh
scripts into interfaces as described here:

http://www.beanshell.org/manual/embeddedmode.html#Calling_BeanShell_From_Jav
a

In the section "Getting Interfaces from the Interpreter".
You do not need to implement classes that implement interfaces.
Rather you implement a bsh script who's global methods just happen to match
those of a Java interface.

For example:
In java:

interface Foo {
   void bar();
};


interpreter.eval("bar() { }");  // executes the bash code "bar() {}"  and
places it into the interpreter

Foo foo = (Foo) interpreter.getInterface( Foo.calss );
foo.bar(); // calls the bsh method "bar"







----------------------------------------
David A. Lee
[hidden email]
http://www.xmlsh.org




------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Loading .java Files

spring
> That is a worthwhile goal but I believe your making a
> fundamental mistake
> assuming that beanshell is java.
> It is not.

Of cause not.

> It is similar to java but it is not identical to java.
> Using a Java IDE is useful but if it thinks your editing
> beanshell code it
> will give you false information.

Hm, this I do not understand.

> If you want to edit and dynamically compile and load pure
> java I suggest
> using the Javac classes for this instead of beanshell.  Then
> you load the
> bytecode with a classloader and it becomes java directly.

It's a webapp running in a servlet container. Would be quite complicated
compared to bsh with all these compiling and classloader issues...

> By "interfaces" I mean the Beanshell method of magically
> converting bsh
> scripts into interfaces as described here:
>
> http://www.beanshell.org/manual/embeddedmode.html#Calling_Bean
> Shell_From_Jav
> a
>
> In the section "Getting Interfaces from the Interpreter".
> You do not need to implement classes that implement interfaces.
> Rather you implement a bsh script who's global methods just
> happen to match
> those of a Java interface.
>
> For example:
> In java:
>
> interface Foo {
>    void bar();
> };
>
>
> interpreter.eval("bar() { }");  // executes the bash code
> "bar() {}"  and
> places it into the interpreter
>
> Foo foo = (Foo) interpreter.getInterface( Foo.calss );
> foo.bar(); // calls the bsh method "bar"

Cool feature! But it seem that this does not work, when the bsh-.script is
in a class

Runnable r =(Runnable)interpreter.getInterface(Runnable.class);
r.run();

Caused by: Method run() not found in bsh scripted object: global : at Line:
-1 : in file: <Called from Java Code> : <Compiled Java Code>

        at bsh.This.invokeMethod(Unknown Source)
        at bsh.This.invokeMethod(Unknown Source)
        at bsh.XThis$Handler.invokeImpl(Unknown Source)
        at bsh.XThis$Handler.invoke(Unknown Source)
        ... 113 more


In the moment I help me out be removing "... class ... {" and the last "}"
from the script before I load it into the interpreter. Not that elegant ;)

Thank you!


P.S. Is bsh still under development?


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Loading .java Files

David Lee

> It is similar to java but it is not identical to java.
> Using a Java IDE is useful but if it thinks your editing
> beanshell code it
> will give you false information.

Hm, this I do not understand.

------------
BSH has java-incompatible syntax.   A IDE that is expecting java will
produce errors when given bsh code that doesn't conform to java.
For example a method with no class.  Or using the {} notation to access a
Hash or bsh iterators, typeless variables, closers and anything else in bsh
which isnt java.






========================
Cool feature! But it seem that this does not work, when the bsh-.script is
in a class

Runnable r =(Runnable)interpreter.getInterface(Runnable.class);
r.run();

=============
It "works" exactly how its supposed to work, just not how  *you* want it to
work.
See above notes about how bsh is not java.

======================================


In the moment I help me out be removing "... class ... {" and the last "}"
from the script before I load it into the interpreter. Not that elegant ;)

Thank you!


P.S. Is bsh still under development?
================================================

bsh is what it is.  If you believe that is 'Not that elegant' that's an
opinion.
But realize that bsh was specifically designed to be a scripting language,
not java.
Thus its syntax is intentionally simplier.   It has a feature of allowing
some subset of java syntax,
but thats not the full intent.
Its not that you have to remove "class { ... }" to get something to "work"
but rather what bsh requires for the language and API it supplies is not
java, it doesn't need the "class" ...
That is 'elegant' or 'inelegant' depending on your opinion or expectations.


You can have both worlds by exposing a global method that calls your class
method.
This is a very common thing.   Keep 'pure java' code in a seperate file (in
fact it can be compiled into your app),
then expose the *bare minimum* as bsh code.

eg.

foo.java
===========
class Foo {
        void bar() { do somethign ; }
};

===========

foo.bsh
============

bar() {
    new Foo().bar();
}
=================


now you can call "bar" directly from BSHMethod.invoke

And no, the original bsh is no longer in development, but 'beanshell2' was
forked and is in development by another set of authors.

----------------------------------------
David A. Lee
[hidden email]
http://www.xmlsh.org










------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Loading .java Files

spring
> ------------
> BSH has java-incompatible syntax.   A IDE that is expecting java will
> produce errors when given bsh code that doesn't conform to java.
> For example a method with no class.  Or using the {} notation
> to access a
> Hash or bsh iterators, typeless variables, closers and
> anything else in bsh
> which isnt java.

Yes. Java isn't Bsh compatible.
But Bsh is Java compatible? I mean that Bsh can eval successfully every
script in Java-1.4 syntax?

> bsh is what it is.  If you believe that is 'Not that elegant'
> that's an opinion.

I doesn't meant Bsh with "not elegant". I meant that my code removes the
"class {}" lines before loading the script.

> And no, the original bsh is no longer in development, but
> 'beanshell2' was
> forked and is in development by another set of authors.

Hm, I cannot find any docs at http://code.google.com/p/beanshell2/.

What are the enhancements to the original Bsh?

Thank you!


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Loading .java Files

David Lee
I don't believe that beanshell is 100% backwards compatible with Java.
Especially Java 1.6.
bsh java compatibility syntax is pre 1.4.
Plus even if the syntax is compatible, as you have discovered, its use case
is not.
You can load a java class file into bsh but you cannot access it from java
natively (only from bsh).
The internal bits it creates from parsing "java" code are not  java JVM
Bytecode.  Its beanshell interpreted runtime codes.   So in some cases you
might get away with reading pure java files, in other cases maybe not.
In no cases is it exactly the same.

If you want pure java you need to use javacc or equivalent and use a
classloader.
Its actually not as complicated as one might imagine.

As for beanshell2 ... I'll let others comment, I haven't followed it for a
while.







----------------------------------------
David A. Lee
[hidden email]
http://www.xmlsh.org


-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
Sent: Sunday, June 26, 2011 4:25 PM
To: 'David Lee'
Cc: [hidden email]
Subject: RE: [Beanshell-users] Loading .java Files


> ------------
> BSH has java-incompatible syntax.   A IDE that is expecting java will
> produce errors when given bsh code that doesn't conform to java.
> For example a method with no class.  Or using the {} notation
> to access a
> Hash or bsh iterators, typeless variables, closers and
> anything else in bsh
> which isnt java.

Yes. Java isn't Bsh compatible.
But Bsh is Java compatible? I mean that Bsh can eval successfully every
script in Java-1.4 syntax?

> bsh is what it is.  If you believe that is 'Not that elegant'
> that's an opinion.

I doesn't meant Bsh with "not elegant". I meant that my code removes the
"class {}" lines before loading the script.

> And no, the original bsh is no longer in development, but
> 'beanshell2' was
> forked and is in development by another set of authors.

Hm, I cannot find any docs at http://code.google.com/p/beanshell2/.

What are the enhancements to the original Bsh?

Thank you!


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users