public class Interpreter extends java.lang.Object implements java.lang.Runnable, java.io.Serializable, BshClassManager.Listener
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 | Class and Description |
|---|---|
static class |
Interpreter.Console |
| Modifier and Type | Field and Description |
|---|---|
static boolean |
COMPATIBIILTY |
static java.lang.ThreadLocal<java.lang.Boolean> |
DEBUG |
protected boolean |
evalOnly |
protected boolean |
interactive |
static MainSecurityGuard |
mainSecurityGuard
It's the main security guard used by all instances of the Interpreter to prevent the execution of dangerous code
|
static boolean |
TRACE |
static java.lang.String |
VERSION |
| Constructor and Description |
|---|
Interpreter()
A non interactive interpreter for evaluation purposes only.
|
Interpreter(ConsoleAssignable console,
boolean interactive,
NameSpace namespace,
Interpreter parent,
java.lang.String sourceFileInfo)
The main constructor, all other constructors should pass through here.
|
Interpreter(ConsoleInterface console)
An interactive interpreter attached to a console, no namespace or parent.
|
Interpreter(ConsoleInterface console,
Interpreter parent)
An interactive interpreter attached to a console using parent namesepace.
|
Interpreter(ConsoleInterface console,
NameSpace globalNameSpace)
An interactive interpreter attached to a console with supplied namespace.
|
Interpreter(ConsoleInterface console,
NameSpace namespace,
Interpreter parent)
An interactive interpreter attached to the specified console.
|
Interpreter(Interpreter parent)
Extend existing interpreter.
|
Interpreter(NameSpace namespace)
A non interactive interpreter for namespace.
|
Interpreter(NameSpace namespace,
Interpreter parent)
A non interactive interpreter for namespace and parent.
|
Interpreter(NameSpace namespace,
Interpreter parent,
java.lang.String sourceFileInfo)
A non interactive interpreter for namespace, parent and source file.
|
Interpreter(NameSpace namespace,
java.lang.String sourceFileInfo)
A non interactive interpreter for namespace and source file.
|
Interpreter(java.io.Reader in,
java.io.PrintStream out,
java.io.PrintStream err,
boolean interactive)
Interpreter instance without a namespace, parent and source file.
|
Interpreter(java.io.Reader in,
java.io.PrintStream out,
java.io.PrintStream err,
boolean interactive,
NameSpace namespace)
Interpreter instance without a parent interpreter and source file.
|
Interpreter(java.io.Reader in,
java.io.PrintStream out,
java.io.PrintStream err,
boolean interactive,
NameSpace namespace,
Interpreter parent,
java.lang.String sourceFileInfo)
Wraps individual input output streams as a console collection.
|
| Modifier and Type | Method and Description |
|---|---|
void |
classLoaderChanged() |
static void |
debug(java.lang.Object... msg)
Print a debug message on debug stream associated with this interpreter
only if debugging is turned on.
|
void |
error(java.lang.Object o) |
java.lang.Object |
eval(java.io.Reader in)
Evaluate the inputstream in this interpreter's global namespace.
|
java.lang.Object |
eval(java.io.Reader in,
NameSpace nameSpace,
java.lang.String sourceFileInfo)
Spawn a non-interactive local interpreter to evaluate text in the
specified namespace.
|
java.lang.Object |
eval(java.lang.String statements)
Evaluate the string in this interpreter's global namespace.
|
java.lang.Object |
eval(java.lang.String statements,
NameSpace nameSpace)
Evaluate the string in the specified namespace.
|
java.lang.Object |
get(java.lang.String name)
Get the value of the name.
|
BshClassManager |
getClassManager()
Get the class manager associated with this interpreter
(the BshClassManager of this interpreter's global namespace).
|
boolean |
getCompatibility()
Compatibility mode.
|
java.io.PrintStream |
getErr() |
java.io.Reader |
getIn()
Console delegate methods.
|
java.lang.Object |
getInterface(java.lang.Class<?> interf)
Get a reference to the interpreter (global namespace), cast
to the specified interface type.
|
NameSpace |
getNameSpace()
Retrieve the global namespace for this interpreter.
|
java.io.PrintStream |
getOut() |
Interpreter |
getParent()
Get the parent Interpreter of this interpreter, if any.
|
static boolean |
getSaveClasses() |
static java.lang.String |
getSaveClassesDir() |
boolean |
getShowResults()
Show on/off verbose printing status for the show() command.
|
java.lang.String |
getSourceFileInfo()
Specify the source of the text from which this interpreter is reading.
|
boolean |
getStrictJava() |
static void |
invokeMain(java.lang.Class<?> clas,
java.lang.String[] args)
Convenience method for invoking the main method of a class.
|
static void |
main(java.lang.String[] args)
Interactive interpreter command line execution.
|
java.io.File |
pathToFile(java.lang.String fileName)
Localize a path to the file name based on the bsh.cwd interpreter
working directory.
|
void |
print(java.lang.Object o) |
void |
println(java.lang.Object o) |
static void |
redirectOutputToFile(java.lang.String filename)
Deprecated.
redirects global JVM streams rather than any specific
interpreter's own streams. Use
setOutputFile(String) to
redirect a particular interpreter's output. |
void |
reset()
Optional method to release additional resources.
|
void |
run()
Run interactively.
|
void |
set(java.lang.String name,
boolean value) |
void |
set(java.lang.String name,
double value) |
void |
set(java.lang.String name,
float value) |
void |
set(java.lang.String name,
int value) |
void |
set(java.lang.String name,
long value) |
void |
set(java.lang.String name,
java.lang.Object value)
Assign the value to the name.
|
void |
setClassLoader(java.lang.ClassLoader externalCL)
Set an external class loader to be used as the base classloader
for BeanShell.
|
void |
setCompatibility(boolean value)
Setting compatibility mode.
|
void |
setConsole(ConsoleAssignable console)
Attach an assignable console and initialize a new parser.
|
void |
setConsole(ConsoleInterface console)
Overloaded to accept a read only console.
|
void |
setErr(java.io.PrintStream err) |
void |
setExitOnEOF(boolean value)
Specify whether, in interactive mode, the interpreter exits Java upon
end of input.
|
void |
setIn(java.io.Reader in) |
void |
setNameSpace(NameSpace namespace)
Assign the global namespace for this interpreter.
|
void |
setOut(java.io.PrintStream out) |
void |
setOutputFile(java.lang.String filename)
Redirect this interpreter's own output and error streams to a file,
leaving
System.out / System.err untouched. |
void |
setShowResults(boolean showResults)
Turn on/off the verbose printing of results as for the show()
command.
|
static void |
setShutdownOnExit(boolean value)
Shared system object visible under bsh.system
|
void |
setStrictJava(boolean b)
Set strict Java mode on or off.
|
void |
setYieldDelay(int delay)
Set thread yield delay time in milliseconds.
|
java.lang.Object |
source(java.io.File file)
Source a script from a file for interpretation.
|
java.lang.Object |
source(java.io.File file,
NameSpace namespace)
Source a script from a file for interpretation.
|
java.lang.Object |
source(java.lang.String filename)
Source a script from a filename for interpretation.
|
java.lang.Object |
source(java.lang.String filename,
NameSpace namespace)
Source a script from a filename for interpretation.
|
java.lang.Object |
source(java.net.URL url)
Source a script from a url for interpretation.
|
java.lang.Object |
source(java.net.URL url,
NameSpace namespace)
Source a script from a url for interpretation.
|
void |
unset(java.lang.String name)
Unassign the variable name.
|
public static final java.lang.ThreadLocal<java.lang.Boolean> DEBUG
public static boolean TRACE
public static boolean COMPATIBIILTY
public static final java.lang.String VERSION
public static final MainSecurityGuard mainSecurityGuard
protected boolean evalOnly
protected boolean interactive
public Interpreter(ConsoleAssignable console, boolean interactive, NameSpace namespace, Interpreter parent, java.lang.String sourceFileInfo)
console - assignable collection of input output streams.interactive - whether attached to user input.namespace - global name space or null.parent - interpreter or null.sourceFileInfo - source file info for debugging or null.public Interpreter(java.io.Reader in,
java.io.PrintStream out,
java.io.PrintStream err,
boolean interactive,
NameSpace namespace,
Interpreter parent,
java.lang.String sourceFileInfo)
in - input stream.out - standard output stream.err - error output stream.interactive - whether attached to user input.namespace - global name space or null.parent - interpreter or null.sourceFileInfo - source file info for debugging or null.public Interpreter(java.io.Reader in,
java.io.PrintStream out,
java.io.PrintStream err,
boolean interactive,
NameSpace namespace)
in - input stream.out - standard output stream.err - error output stream.interactive - whether attached to user input.namespace - global name space or null.public Interpreter(java.io.Reader in,
java.io.PrintStream out,
java.io.PrintStream err,
boolean interactive)
in - input stream.out - standard output stream.err - error output stream.interactive - whether attached to user input.public Interpreter(ConsoleInterface console, NameSpace namespace, Interpreter parent)
console - read only collection of input output streams.namespace - global name space or null.parent - interpreter or null.public Interpreter(ConsoleInterface console, Interpreter parent)
console - read only collection of input output streams.parent - interpreter or null.public Interpreter(ConsoleInterface console, NameSpace globalNameSpace)
console - read only collection of input output streams.namespace - global name space or null.public Interpreter(ConsoleInterface console)
console - read only collection of input output streams.public Interpreter()
public Interpreter(NameSpace namespace)
namespace - global name space.public Interpreter(NameSpace namespace, java.lang.String sourceFileInfo)
namespace - global name space.sourceFileInfo - source file info for debugging.public Interpreter(NameSpace namespace, Interpreter parent)
namespace - global name space.parent - interpreter.public Interpreter(NameSpace namespace, Interpreter parent, java.lang.String sourceFileInfo)
namespace - global name space.parent - interpreter.sourceFileInfo - source file info for debugging.public Interpreter(Interpreter parent)
parent - interpreter.public static void setShutdownOnExit(boolean value)
public void setConsole(ConsoleAssignable console)
console - assignable collection of input output streams.public void setConsole(ConsoleInterface console)
console - read only collection of input output streams.public void setNameSpace(NameSpace namespace)
Note: It is preferred to keep the interpreter as long reference and use it to create discardable instances which can inherit from the parent and be dereferenced for collection. This method is only here for completeness.
The global namespace can be accessed in scripts using the variable 'this.namespace' or global.namespace as necessary.
namespace - global name space.public NameSpace getNameSpace()
Note: It is preferred to keep the interpreter as long reference and use it to create discardable instances which can inherit from the parent and be dereferenced for collection. This method is only here for completeness.
The global namespace can be accessed in scripts using the variable 'this.namespace' or global.namespace as necessary.
public static void main(java.lang.String[] args)
args - optional file name for interpretation.public static void invokeMain(java.lang.Class<?> clas,
java.lang.String[] args)
throws java.lang.Exception
clas - with static main method.args - the string arguments.java.lang.Exception - thrown if something fails.public void run()
run in interface java.lang.Runnablepublic java.lang.Object source(java.net.URL url,
NameSpace namespace)
throws EvalError,
java.io.IOException
url - the source url.namespace - effective namespace.EvalError - if a script error occurred.java.io.IOException - if a file read error occurred.public java.lang.Object source(java.io.File file,
NameSpace namespace)
throws EvalError,
java.io.IOException
file - the source file.namespace - effective namespace.EvalError - if a script error occurred.java.io.IOException - if a file read error occurred.public java.lang.Object source(java.lang.String filename,
NameSpace namespace)
throws EvalError,
java.io.IOException
filename - the source file name.namespace - effective namespace.EvalError - if a script error occurred.java.io.IOException - if a file read error occurred.public java.lang.Object source(java.net.URL url)
throws EvalError,
java.io.IOException
url - the source url.EvalError - if a script error occurred.java.io.IOException - if a file read error occurred.public java.lang.Object source(java.io.File file)
throws EvalError,
java.io.IOException
file - the source file.EvalError - if a script error occurred.java.io.IOException - if a file read error occurred.public java.lang.Object source(java.lang.String filename)
throws EvalError,
java.io.IOException
filename - the source file name.EvalError - if a script error occurred.java.io.IOException - if a file read error occurred.public java.lang.Object eval(java.io.Reader in,
NameSpace nameSpace,
java.lang.String sourceFileInfo)
throws EvalError
sourceFileInfo - is for information purposes only. It is used to
display error messages (and in the future may be made available to
the script).EvalError - on script problemsTargetError - on unhandled exceptions from the scriptpublic void reset()
public java.lang.Object eval(java.io.Reader in)
throws EvalError
EvalErrorpublic java.lang.Object eval(java.lang.String statements)
throws EvalError
EvalErrorpublic java.lang.Object eval(java.lang.String statements,
NameSpace nameSpace)
throws EvalError
EvalErrorpublic java.io.Reader getIn()
public java.io.PrintStream getOut()
public java.io.PrintStream getErr()
public final void println(java.lang.Object o)
public final void print(java.lang.Object o)
public final void error(java.lang.Object o)
public void setIn(java.io.Reader in)
public void setOut(java.io.PrintStream out)
public void setErr(java.io.PrintStream err)
public static final void debug(java.lang.Object... msg)
public java.lang.Object get(java.lang.String name)
throws EvalError
EvalErrorpublic void set(java.lang.String name,
java.lang.Object value)
throws EvalError
EvalErrorpublic void unset(java.lang.String name)
throws EvalError
EvalErrorpublic java.lang.Object getInterface(java.lang.Class<?> interf)
throws EvalError
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.
EvalError - if the interface cannot be generated because the
version of Java does not support the proxy mechanism.public void setYieldDelay(int delay)
delay - sleep time in millisecondspublic java.io.File pathToFile(java.lang.String fileName)
throws java.io.IOException
java.io.IOException@Deprecated public static void redirectOutputToFile(java.lang.String filename)
setOutputFile(String) to
redirect a particular interpreter's output.System.out / System.err to a
file. This does not affect any particular Interpreter
instance's own output (see getOut()); despite its name and
signature it is unrelated to a specific interpreter.filename - file to write output to.public void setOutputFile(java.lang.String filename)
System.out / System.err untouched.filename - file to write output to.public void setClassLoader(java.lang.ClassLoader externalCL)
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.
public BshClassManager getClassManager()
public void setStrictJava(boolean b)
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.
public boolean getStrictJava()
setStrictJava( boolean )public java.lang.String getSourceFileInfo()
Node.getSourceFile()public Interpreter getParent()
public void setExitOnEOF(boolean value)
public void setShowResults(boolean showResults)
public boolean getShowResults()
public boolean getCompatibility()
true missing classes are tried to create from corresponding java source files.
The Default value is false. This could be changed to true by setting the system property
"bsh.compatibility" to "true".setCompatibility(boolean)public void setCompatibility(boolean value)
true missing classes are tried to create from corresponding java source
files. The Default value is false. This could be changed to true by setting the system property
"bsh.compatibility" to "true".getCompatibility()public static java.lang.String getSaveClassesDir()
public static boolean getSaveClasses()
public void classLoaderChanged()
classLoaderChanged in interface BshClassManager.Listener