Re: General concept - or what kind of classes does bsh create?

Patrick Niemeyer <[email protected]> Mon, 31 Jan 2005 11:39:53 -0600
Newsgroups gmane.comp.java.beanshell.user,gmane.comp.java.beanshell.devel
Message-ID <[email protected]>
On Jan 31, 2005, at 7:06 AM, Furash Gary wrote:

> The "classes" I'm familiar with in beanshell are the pseudo-classes,
> like
>
> Potato()
> ...
> Return this;
>
> ...
>
> Is there a different kind of class?

There are technically two more kinds of classes, though to most people 
it will just look like "scripted objects" and "Java classes".

First there are the scripted objects that you mentioned:

   foo() { return this; }
   foo=foo();

Those are pure BeanShell and to outside Java applications are an 
instance of a bsh.This class.

Next, if you're running under Java 1.3 or better, you can script 
interfaces in one of several ways: implicitly, by casting a bsh 'this' 
reference, or just by using the standard Java anonymous inner class 
syntax.  e.g.:

   actionPerformed( e ) { print("action!"); }
   myButton.addActionListener( this );

or

   ActionListener typedListener = (ActionListener)this;

or

   myButton.addActionListener( new ActionListener() {
      public void actionPerformed( e ) { ... }
   ) );

Those forms take advantage of Java's reflection API Proxy generator, 
which can implement interfaces dynamically.  Technically it generates a 
class internally, but BeanShell just uses the API.

Finally, in BeanShell 2.0 we now support the full Java class syntax. So 
you can declare and extend classes just as in Java... or mix class 
declarations and loose BeanShell scripts if you wish:

   class MyActionListener implements ActionListener {
     public void actionPerformed( e ) { ... }
   }

To support these kinds of classes in BeanShell 2.x we have to generate 
a very small amount of bytecode (essentially internally compile a small 
class wrapper).  But the guts of the class delegate back to the script, 
so the class body can contain loose script constructs, methods, etc.

You can even make a real class that simply has a loose script body 
which will by default be executed when it is constructed, e.g.:

   class MyClass {
       print("Hello!");
       foo=2+2;
   }

   MyClass myClass = new MyClass(); // Hello!

These standard Java syntax classes (declared with the 'class' keyword) 
are real classes and will appear as real types and by reflection to 
have real methods when used outside of the script in Java code...  With 
that said, the 2.x branch is still in beta and there a bugs of course.  
We'll get that moving again very soon.


Pat



-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
Tool for open source databases. Create drag-&-drop reports. Save time
by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
Download a FREE copy at http://www.intelliview.com/go/osdn_nl