BeanShell API Compiler...
Patrick Niemeyer <[email protected]> Mon, 20 Jun 2005 00:24:47 -0500
| Newsgroups | gmane.comp.java.beanshell.devel |
|---|---|
| Message-ID | <[email protected]> |
(Experimental Feature)
I've checked in the following addition to SVN for the next release:
Using the system property -DsaveClasses=<savedir> you can now
instruct BeanShell to write out persistent class files for class
definitions found in the script. In this mode BeanShell will not
execute code, but will generate and store a class files for each
class definition it finds. These classes are the same as the
ordinary runtime generated delegate classes used internally by
BeanShell but additionally have static initializer code that allows
them to bootstrap an instance of the BeanShell interpreter to execute
their scripted body if the class is used from an external application.
Currently we are using a simple convention whereby a class
"Foo.class" expects to find its associated scripted class definition
in a file "Foo.bsh" in the same location as the class file. Each
compiled stub class must have a corresponding script definition. The
scripts itself may contain any amount of BeanShell code beyond the
class definition and may refer to other scripted classes or saved
classes. However each saved (compiled) stub class will currently
initialize its own interpreter instance.
So, for example, to create a class for the following scripted class
definition:
class Foo {
Foo() { print("Help, I'm being constructed!"); }
}
you could place the script into a file "Foo.bsh" in the current
directory and execute:
java -DsaveClasses=. bsh.Interpreter Foo.bsh
This will create the class Foo.class. The Foo class may be used like
any ordinary Java class and invoked from ordinary Java code. Upon
being loaded it will automatically create a BeanShell interpreter
instance to load and execute its corresponding Foo.bsh script file.
The script is expected to correspond to the structure of the class:
that is to initialize static and instance members and implement all
static and instance methods required by the class. Beyond that, the
script is free to change over time and include additional script body
outside the scripted class definition.
For example, you might later change the Foo.bsh script to the following:
welcomeMessage = "Hello World!";
class Foo {
Foo() { print( "I'm being constructed: "+ welcomeMessage ); }
}
Here's another example, showing a scripted class with a main() method:
// Begin script Foo.bsh
class Foo {
public static void main( String [] args ) { }
}
message = "Hey";
message += " you!";
num = 2 * 2;
message += " The answer is: "+num;
// end script Foo.bsh
Now we have a script that can be launched like any other Java class
via 'java Foo'. The loose script body outside the class is run upon
class initialization. Moving that text into the class body would
cause it to be run on class construction (unless you explicitly put
it in a static block inside the class).
I hope you have fun playing with this. Keep in mind that it's still
a bit experimental. Fortunately the changes to make this work were
very minimal, so it seemed worth adding now.
thanks,
Pat
-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click