Here are some examples:
Interpeter bsh = new Interpreter();
// Evaluate statements and expressions
bsh.eval("foo=Math.sin(0.5)");
bsh.eval("bar=foo*5; bar=Math.cos(bar);");
bsh.eval("for(i=0; i<10; i++) { print(\"hello\"); }");
// same as above using java syntax and apis only
bsh.eval("for(int i=0; i<10; i++) { System.out.println(\"hello\"); }");
// Source from files or streams
bsh.source("myscript.bsh"); // or bsh.eval("source(\"myscript.bsh\")");
// Use set() and get() to pass objects in and out of variables
bsh.set( "date", new Date() );
Date date = (Date)bsh.get( "date" );
// This would also work:
Date date = (Date)bsh.eval( "date" );
bsh.eval("year = date.getYear()");
Integer year = (Integer)bsh.get("year"); // primitives use wrappers
// With Java1.3+ scripts can implement arbitrary interfaces...
// Script an awt event handler (or source it from a file, more likely)
bsh.eval( "actionPerformed( e ) { print( e ); }");
// Get a reference to the script object (implementing the interface)
ActionListener scriptedHandler =
(ActionListener)bsh.eval("return (ActionListener)this");
// Use the scripted event handler normally...
new JButton.addActionListener( script );
In the above examples we showed a single interpreter instance, however you may wish to use many instances, depending on the application and how you structure your scripts. Interpreter instances are very light weight to create, however if you are going to execute the same script repeatedly and require maximum performance you should consider scripting the code as a method and invoking the scripted method each time on the same interpreter instance (using eval()).
See the BeanShell User's Manual for more information.
| Modifier and Type | Field and Description |
|---|---|
| pack-priv ConsoleInterface | |
| public static boolean | |
| pack-priv static transient PrintStream | |
| pack-priv transient PrintStream | |
| protected boolean | |
| private boolean | exitOnEOF
by default in interactive mode System.exit() on EOF |
| pack-priv NameSpace | |
| pack-priv transient Reader | |
| protected boolean | |
| public static boolean | |
| pack-priv transient PrintStream | |
| pack-priv Interpreter | parent
If this interpeter is a child of another, the parent |
| pack-priv transient Parser | |
| pack-priv static This | sharedObject
Shared system object visible under bsh.system |
| private boolean | showResults
Control the verbose printing of results for the show() command. |
| pack-priv String | sourceFileInfo
The name of the file or other source that this interpreter is reading |
| private boolean | strictJava
Strict Java mode |
| pack-priv static String | |
| public static boolean | |
| public static final String |
| Access | Constructor and Description |
|---|---|
| public | Interpreter(Reader in, PrintStream out, PrintStream err, boolean interactive, NameSpace
If namespace is non-null then this interpreter's
root namespace will be set to the one provided. If it is null a new
one will be created for it. namespace, Interpreter The parent interpreter if this interpreter is a child
of another. May be null. Children share a BshClassManager with
their parent instance. parent, String An informative string holding the filename
or other description of the source from which this interpreter is
reading... used for debugging. May be null. sourceFileInfo)The main constructor. |
| public | |
| public | |
| public | Interpreter(ConsoleInterface console, NameSpace globalNameSpace)
Construct a new interactive interpreter attached to the specified console using the specified parent namespace. |
| public | Interpreter(ConsoleInterface console)
Construct a new interactive interpreter attached to the specified console. |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static final void | |
| public final void | error(Object o)
Implements bsh. |
| public Object | |
| public Object | |
| public Object | |
| public Object | |
| public Object | |
| private JavaCharStream | |
| private JJTParserState | |
| private String | |
| public BshClassManager | getClassManager()
Get the class manager associated with this interpreter (the BshClassManager of this interpreter's global namespace). |
| public PrintStream | getErr()
Implements bsh. |
| public Reader | getIn()
Implements bsh. |
| public Object | getInterface(Class<T> interf)
Get a reference to the interpreter (global namespace), cast to the specified interface type. |
| public NameSpace | |
| public PrintStream | getOut()
Implements bsh. |
| public Interpreter | |
| public boolean | |
| public String | |
| public boolean | |
| pack-priv Object | |
| private void | |
| public static void | |
| private boolean | |
| pack-priv void | |
| public static void | |
| public File | pathToFile(String fileName)
Localize a path to the file name based on the bsh.cwd interpreter working directory. |
| public final void | |
| public final void | |
| private void | |
| public static void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | setClassLoader(ClassLoader externalCL)
Set an external class loader to be used as the base classloader for BeanShell. |
| public void | |
| public void | |
| public void | setExitOnEOF(boolean value)
Specify whether, in interactive mode, the interpreter exits Java upon end of input. |
| public void | |
| public void | |
| public void | setShowResults(boolean showResults)
Turn on/off the verbose printing of results as for the show() command. |
| public void | |
| pack-priv void | |
| private String | |
| public Object | |
| public Object | |
| pack-priv static void | |
| public void |
| console | back to summary |
|---|---|
| pack-priv ConsoleInterface console | |
| DEBUG | back to summary |
|---|---|
| public static boolean DEBUG | |
| debug | back to summary |
|---|---|
| pack-priv static transient PrintStream debug | |
| err | back to summary |
|---|---|
| pack-priv transient PrintStream err | |
| evalOnly | back to summary |
|---|---|
| protected boolean evalOnly | |
| exitOnEOF | back to summary |
|---|---|
| private boolean exitOnEOF by default in interactive mode System.exit() on EOF | |
| globalNameSpace | back to summary |
|---|---|
| pack-priv NameSpace globalNameSpace | |
| in | back to summary |
|---|---|
| pack-priv transient Reader in | |
| interactive | back to summary |
|---|---|
| protected boolean interactive | |
| LOCALSCOPING | back to summary |
|---|---|
| public static boolean LOCALSCOPING | |
| out | back to summary |
|---|---|
| pack-priv transient PrintStream out | |
| parent | back to summary |
|---|---|
| pack-priv Interpreter parent If this interpeter is a child of another, the parent | |
| parser | back to summary |
|---|---|
| pack-priv transient Parser parser | |
| sharedObject | back to summary |
|---|---|
| pack-priv static This sharedObject Shared system object visible under bsh.system | |
| showResults | back to summary |
|---|---|
| private boolean showResults Control the verbose printing of results for the show() command. | |
| sourceFileInfo | back to summary |
|---|---|
| pack-priv String sourceFileInfo The name of the file or other source that this interpreter is reading | |
| strictJava | back to summary |
|---|---|
| private boolean strictJava Strict Java mode
| |
| systemLineSeparator | back to summary |
|---|---|
| pack-priv static String systemLineSeparator | |
| TRACE | back to summary |
|---|---|
| public static boolean TRACE | |
| VERSION | back to summary |
|---|---|
| public static final String VERSION | |
| Interpreter | back to summary |
|---|---|
| public Interpreter(Reader in, PrintStream out, PrintStream err, boolean interactive, NameSpace namespace, Interpreter parent, String sourceFileInfo) The main constructor. All constructors should now pass through here.
| |
| Interpreter | back to summary |
|---|---|
| public Interpreter(Reader in, PrintStream out, PrintStream err, boolean interactive, NameSpace namespace) | |
| Interpreter | back to summary |
|---|---|
| public Interpreter(Reader in, PrintStream out, PrintStream err, boolean interactive) | |
| Interpreter | back to summary |
|---|---|
| public Interpreter(ConsoleInterface console, NameSpace globalNameSpace) Construct a new interactive interpreter attached to the specified console using the specified parent namespace. | |
| Interpreter | back to summary |
|---|---|
| public Interpreter(ConsoleInterface console) Construct a new interactive interpreter attached to the specified console. | |
| Interpreter | back to summary |
|---|---|
| public Interpreter() Create an interpreter for evaluation only. | |
| debug | back to summary |
|---|---|
| public static final void debug(String s) Print a debug message on debug stream associated with this interpreter only if debugging is turned on. | |
| error | back to summary |
|---|---|
| public final void error(Object o) Implements bsh. Print an error message in a standard format on the output stream associated with this interpreter. On the GUI console this will appear in red, etc. | |
| eval | back to summary |
|---|---|
| public Object eval(Reader in, NameSpace nameSpace, String sourceFileInfo) throws EvalError Spawn a non-interactive local interpreter to evaluate text in the specified namespace. Return value is the evaluated object (or corresponding primitive wrapper).
| |
| eval | back to summary |
|---|---|
| public Object eval(Reader in) throws EvalError Evaluate the inputstream in this interpreter's global namespace. | |
| eval | back to summary |
|---|---|
| public Object eval(String statements) throws EvalError Evaluate the string in this interpreter's global namespace. | |
| eval | back to summary |
|---|---|
| public Object eval(String statements, NameSpace nameSpace) throws EvalError Evaluate the string in the specified namespace. | |
| get | back to summary |
|---|---|
| public Object get(String name) throws EvalError Get the value of the name. name may be any value. e.g. a variable or field | |
| get_jj_input_stream | back to summary |
|---|---|
| private JavaCharStream get_jj_input_stream() | |
| get_jjtree | back to summary |
|---|---|
| private JJTParserState get_jjtree() | |
| getBshPrompt | back to summary |
|---|---|
| private String getBshPrompt() Get the prompt string defined by the getBshPrompt() method in the global namespace. This may be from the getBshPrompt() command or may be defined by the user as with any other method. Defaults to "bsh % " if the method is not defined or there is an error. | |
| getClassManager | back to summary |
|---|---|
| public BshClassManager getClassManager() Get the class manager associated with this interpreter (the BshClassManager of this interpreter's global namespace). This is primarily a convenience method. | |
| getErr | back to summary |
|---|---|
| public PrintStream getErr() Implements bsh. Get the error output stream associated with this interpreter. This may be be stderr or the GUI console. | |
| getIn | back to summary |
|---|---|
| public Reader getIn() Implements bsh. Get the input stream associated with this interpreter. This may be be stdin or the GUI console. | |
| getInterface | back to summary |
|---|---|
| public Object getInterface(Class<T> interf) throws EvalError Get a reference to the interpreter (global namespace), cast to the specified interface type. Assuming the appropriate methods of the interface are defined in the interpreter, then you may use this interface from Java, just like any other Java object. For example:
Interpreter interpreter = new Interpreter();
// define a method called run()
interpreter.eval("run() { ... }");
// Fetch a reference to the interpreter as a Runnable
Runnable runnable =
(Runnable)interpreter.getInterface( Runnable.class );
Note that the interpreter does *not* require that any or all of the methods of the interface be defined at the time the interface is generated. However if you attempt to invoke one that is not defined you will get a runtime exception. Note also that this convenience method has exactly the same effect as evaluating the script: (Type)this; For example, the following is identical to the previous example:
// Fetch a reference to the interpreter as a Runnable Runnable runnable = (Runnable)interpreter.eval( "(Runnable)this" ); Version requirement Although standard Java interface types are always available, to be used with arbitrary interfaces this feature requires that you are using Java 1.3 or greater.
| |
| getNameSpace | back to summary |
|---|---|
| public NameSpace getNameSpace() Get the global namespace of this interpreter. Note This is here for completeness. If you're using this a lot it may be an indication that you are doing more work than you have to. For example, caching the interpreter instance rather than the namespace should not add a significant overhead. No state other than the debug status is stored in the interpreter. All features of the namespace can also be accessed using the interpreter via eval() and the script variable 'this.namespace' (or global.namespace as necessary). | |
| getOut | back to summary |
|---|---|
| public PrintStream getOut() Implements bsh. Get the outptut stream associated with this interpreter. This may be be stdout or the GUI console. | |
| getParent | back to summary |
|---|---|
| public Interpreter getParent() Get the parent Interpreter of this interpreter, if any. Currently this relationship implies the following: 1) Parent and child share a BshClassManager 2) Children indicate the parent's source file information in error reporting. When created as part of a source() / eval() the child also shares the parent's namespace. But that is not necessary in general. | |
| getShowResults | back to summary |
|---|---|
| public boolean getShowResults() Show on/off verbose printing status for the show() command. See the BeanShell show() command. If this interpreter has a parent the call is delegated. | |
| getSourceFileInfo | back to summary |
|---|---|
| public String getSourceFileInfo() Specify the source of the text from which this interpreter is reading. Note there is a difference between what file the interrpeter is sourcing and from what file a method was originally parsed. One file may call a method sourced from another file. See SimpleNode for origination file info.
| |
| getStrictJava | back to summary |
|---|---|
public boolean getStrictJava()
| |
| getu | back to summary |
|---|---|
| pack-priv Object getu(String name) Unchecked get for internal use | |
| initRootSystemObject | back to summary |
|---|---|
| private void initRootSystemObject() | |
| invokeMain | back to summary |
|---|---|
| public static void invokeMain(Class<T> clas, String[] args) throws Exception | |
| Line | back to summary |
|---|---|
| private boolean Line() throws ParseException | |
| loadRCFiles | back to summary |
|---|---|
| pack-priv void loadRCFiles() | |
| main | back to summary |
|---|---|
| public static void main(String[] args) Run the text only interpreter on the command line or specify a file. | |
| pathToFile | back to summary |
|---|---|
| public File pathToFile(String fileName) throws IOException Localize a path to the file name based on the bsh.cwd interpreter working directory. | |
| back to summary | |
|---|---|
| public final void print(Object o) Implements bsh. | |
| println | back to summary |
|---|---|
| public final void println(Object o) Implements bsh. | |
| readObject | back to summary |
|---|---|
| private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException De-serialization setup. Default out and err streams to stdout, stderr if they are null. | |
| redirectOutputToFile | back to summary |
|---|---|
| public static void redirectOutputToFile(String filename) | |
| run | back to summary |
|---|---|
| public void run() Implements java. Run interactively. (printing prompts, etc.) | |
| set | back to summary |
|---|---|
| public void set(String name, Object value) throws EvalError Assign the value to the name. name may evaluate to anything assignable. e.g. a variable or field. | |
| set | back to summary |
|---|---|
| public void set(String name, long value) throws EvalError | |
| set | back to summary |
|---|---|
| public void set(String name, int value) throws EvalError | |
| set | back to summary |
|---|---|
| public void set(String name, double value) throws EvalError | |
| set | back to summary |
|---|---|
| public void set(String name, float value) throws EvalError | |
| set | back to summary |
|---|---|
| public void set(String name, boolean value) throws EvalError | |
| setClassLoader | back to summary |
|---|---|
| public void setClassLoader(ClassLoader externalCL) Set an external class loader to be used as the base classloader for BeanShell. The base classloader is used for all classloading unless/until the addClasspath()/setClasspath()/reloadClasses() commands are called to modify the interpreter's classpath. At that time the new paths /updated paths are added on top of the base classloader. BeanShell will use this at the same point it would otherwise use the plain Class.forName(). i.e. if no explicit classpath management is done from the script (addClassPath(), setClassPath(), reloadClasses()) then BeanShell will only use the supplied classloader. If additional classpath management is done then BeanShell will perform that in addition to the supplied external classloader. However BeanShell is not currently able to reload classes supplied through the external classloader.
| |
| setConsole | back to summary |
|---|---|
| public void setConsole(ConsoleInterface console) Attach a console Note: this method is incomplete. | |
| setErr | back to summary |
|---|---|
| public void setErr(PrintStream err) | |
| setExitOnEOF | back to summary |
|---|---|
| public void setExitOnEOF(boolean value) Specify whether, in interactive mode, the interpreter exits Java upon end of input. If true, when in interactive mode the interpreter will issue a System.exit(0) upon eof. If false the interpreter no System.exit() will be done. Note if you wish to cause an EOF externally you can try closing the input stream. This is not guaranteed to work in older versions of Java due to Java limitations, but should work in newer JDK/JREs. (That was the motivation for the Java NIO package). | |
| setNameSpace | back to summary |
|---|---|
| public void setNameSpace(NameSpace globalNameSpace) Set the global namespace for this interpreter. Note This is here for completeness. If you're using this a lot it may be an indication that you are doing more work than you have to. For example, caching the interpreter instance rather than the namespace should not add a significant overhead. No state other than the debug status is stored in the interpreter. All features of the namespace can also be accessed using the interpreter via eval() and the script variable 'this.namespace' (or global.namespace as necessary). | |
| setOut | back to summary |
|---|---|
| public void setOut(PrintStream out) | |
| setShowResults | back to summary |
|---|---|
| public void setShowResults(boolean showResults) Turn on/off the verbose printing of results as for the show() command. If this interpreter has a parent the call is delegated. See the BeanShell show() command. | |
| setStrictJava | back to summary |
|---|---|
| public void setStrictJava(boolean b) Set strict Java mode on or off. This mode attempts to make BeanShell syntax behave as Java syntax, eliminating conveniences like loose variables, etc. When enabled, variables are required to be declared or initialized before use and method arguments are reqired to have types. This mode will become more strict in a future release when classes are interpreted and there is an alternative to scripting objects as method closures. | |
| setu | back to summary |
|---|---|
| pack-priv void setu(String name, Object value) Unchecked set for internal use | |
| showEvalString | back to summary |
|---|---|
| private String showEvalString(String s) | |
| source | back to summary |
|---|---|
| public Object source(String filename, NameSpace nameSpace) throws FileNotFoundException, IOException, EvalError Read text from fileName and eval it. | |
| source | back to summary |
|---|---|
| public Object source(String filename) throws FileNotFoundException, IOException, EvalError Read text from fileName and eval it. Convenience method. Use the global namespace. | |
| staticInit | back to summary |
|---|---|
| pack-priv static void staticInit() | |
| unset | back to summary |
|---|---|
| public void unset(String name) throws EvalError Unassign the variable name. Name should evaluate to a variable. | |