Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

bsh
Class Interpreter  view Interpreter download Interpreter.java

java.lang.Object
  extended bybsh.Interpreter
All Implemented Interfaces:
ConsoleInterface, java.lang.Runnable

public class Interpreter
extends java.lang.Object
implements java.lang.Runnable, ConsoleInterface

The BeanShell script interpreter. An instance of Interpreter can be used to source scripts and evaluate statements or expressions.

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.


Field Summary
(package private)  ConsoleInterface console
           
(package private) static java.io.PrintStream debug
           
static boolean DEBUG
           
(package private)  java.io.PrintStream err
           
private  boolean evalOnly
           
(package private)  NameSpace globalNameSpace
           
(package private)  java.io.Reader in
           
private  boolean interactive
           
 boolean noExitOnEOF
          Do we override exit on EOF as normally done in iteractive mode? (This is used by Sessiond)
(package private)  java.io.PrintStream out
           
(package private)  Interpreter parent
          If this interpeter is a child of another, the parent
(package private)  Parser parser
           
(package private)  java.lang.String sourceFileInfo
          The name of the file or other source that this interpreter is reading
(package private) static This systemObject
          Shared system object visible under bsh.system
static boolean TRACE
           
static java.lang.String VERSION
           
 
Constructor Summary
Interpreter()
          Create an interpreter for evaluation only.
Interpreter(ConsoleInterface console)
          Construct a new interactive interpreter attached to the specified console.
Interpreter(ConsoleInterface console, NameSpace globalNameSpace)
          Construct a new interactive interpreter attached to the specified console using the specified parent namespace.
Interpreter(java.io.Reader in, java.io.PrintStream out, java.io.PrintStream err, boolean interactive)
           
Interpreter(java.io.Reader in, java.io.PrintStream out, java.io.PrintStream err, boolean interactive, NameSpace namespace)
           
Interpreter(java.io.Reader in, java.io.PrintStream out, java.io.PrintStream err, boolean interactive, NameSpace namespace, Interpreter parent, java.lang.String sourceFileInfo)
          The main constructor.
 
Method Summary
static void debug(java.lang.String s)
          Print a debug message on debug stream associated with this interpreter only if debugging is turned on.
 void error(java.lang.String s)
          Print an error message in a standard format on the output stream associated with this interpreter.
 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 statement)
          Evaluate the string in this interpreter's global namespace.
 java.lang.Object eval(java.lang.String statement, NameSpace nameSpace)
          Evaluate the string in the specified namespace.
private  ASCII_UCodeESC_CharStream get_jj_input_stream()
           
private  JJTParserState get_jjtree()
           
 java.lang.Object get(java.lang.String name)
          Get the value of the name.
 java.io.PrintStream getErr()
          Get the error output stream associated with this interpreter.
 java.io.Reader getIn()
          Get the input stream associated with this interpreter.
 NameSpace getNameSpace()
          Get the global namespace of this interpreter.
 java.io.PrintStream getOut()
          Get the outptut stream associated with this interpreter.
 Interpreter getParent()
           
 java.lang.String getSourceFileInfo()
          Specify the source of the text from which this interpreter is reading.
(package private)  java.lang.Object getu(java.lang.String name)
          Unchecked get for internal use
 java.lang.Object getVariable(java.lang.String name)
          Deprecated. does not properly evaluate compound names
private  void initRootSystemObject()
           
private  boolean Line()
           
(package private)  void loadRCFiles()
           
static void main(java.lang.String[] args)
          Run the text only interpreter on the command line or specify a file.
 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.String s)
           
 void println(java.lang.String s)
           
static void redirectOutputToFile(java.lang.String filename)
           
 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 setConsole(ConsoleInterface console)
          Attach the console thusly...
 void setNameSpace(NameSpace globalNameSpace)
          Set the global namespace for this interpreter.
(package private)  void setu(java.lang.String name, java.lang.Object value)
          Unchecked set for internal use
 void setVariable(java.lang.String name, boolean value)
          Deprecated. does not properly evaluate compound names
 void setVariable(java.lang.String name, float value)
          Deprecated. does not properly evaluate compound names
 void setVariable(java.lang.String name, int value)
          Deprecated. does not properly evaluate compound names
 void setVariable(java.lang.String name, java.lang.Object value)
          Deprecated. does not properly evaluate compound names
 java.lang.Object source(java.lang.String filename)
          Read text from fileName and eval it.
 java.lang.Object source(java.lang.String filename, NameSpace nameSpace)
          Read text from fileName and eval it.
(package private) static void staticInit()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

public static final java.lang.String VERSION
See Also:
Constant Field Values

DEBUG

public static boolean DEBUG

TRACE

public static boolean TRACE

debug

static java.io.PrintStream debug

systemObject

static This systemObject
Shared system object visible under bsh.system


parser

Parser parser

globalNameSpace

NameSpace globalNameSpace

in

java.io.Reader in

out

java.io.PrintStream out

err

java.io.PrintStream err

console

ConsoleInterface console

parent

Interpreter parent
If this interpeter is a child of another, the parent


sourceFileInfo

java.lang.String sourceFileInfo
The name of the file or other source that this interpreter is reading


noExitOnEOF

public boolean noExitOnEOF
Do we override exit on EOF as normally done in iteractive mode? (This is used by Sessiond)


evalOnly

private boolean evalOnly

interactive

private boolean interactive
Constructor Detail

Interpreter

public Interpreter(java.io.Reader in,
                   java.io.PrintStream out,
                   java.io.PrintStream err,
                   boolean interactive,
                   NameSpace namespace,
                   Interpreter parent,
                   java.lang.String sourceFileInfo)
The main constructor. All constructors should now pass through here.


Interpreter

public Interpreter(java.io.Reader in,
                   java.io.PrintStream out,
                   java.io.PrintStream err,
                   boolean interactive,
                   NameSpace namespace)

Interpreter

public Interpreter(java.io.Reader in,
                   java.io.PrintStream out,
                   java.io.PrintStream err,
                   boolean interactive)

Interpreter

public Interpreter(ConsoleInterface console,
                   NameSpace globalNameSpace)
Construct a new interactive interpreter attached to the specified console using the specified parent namespace.


Interpreter

public Interpreter(ConsoleInterface console)
Construct a new interactive interpreter attached to the specified console.


Interpreter

public Interpreter()
Create an interpreter for evaluation only.

Method Detail

setConsole

public void setConsole(ConsoleInterface console)
Attach the console thusly... ;)


initRootSystemObject

private void initRootSystemObject()

setNameSpace

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).


getNameSpace

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).


main

public static void main(java.lang.String[] args)
Run the text only interpreter on the command line or specify a file.


run

public void run()
Run interactively. (printing prompts, etc.)

Specified by:
run in interface java.lang.Runnable

source

public java.lang.Object source(java.lang.String filename,
                               NameSpace nameSpace)
                        throws java.io.FileNotFoundException,
                               java.io.IOException,
                               EvalError
Read text from fileName and eval it.


source

public java.lang.Object source(java.lang.String filename)
                        throws java.io.FileNotFoundException,
                               java.io.IOException,
                               EvalError
Read text from fileName and eval it. Convenience method. Use the global namespace.


eval

public java.lang.Object eval(java.io.Reader in,
                             NameSpace nameSpace,
                             java.lang.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

public java.lang.Object eval(java.io.Reader in)
                      throws EvalError
Evaluate the inputstream in this interpreter's global namespace.


eval

public java.lang.Object eval(java.lang.String statement)
                      throws EvalError
Evaluate the string in this interpreter's global namespace.


eval

public java.lang.Object eval(java.lang.String statement,
                             NameSpace nameSpace)
                      throws EvalError
Evaluate the string in the specified namespace.


error

public final void error(java.lang.String s)
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.

Specified by:
error in interface ConsoleInterface

getIn

public java.io.Reader getIn()
Get the input stream associated with this interpreter. This may be be stdin or the GUI console.

Specified by:
getIn in interface ConsoleInterface

getOut

public java.io.PrintStream getOut()
Get the outptut stream associated with this interpreter. This may be be stdout or the GUI console.

Specified by:
getOut in interface ConsoleInterface

getErr

public java.io.PrintStream getErr()
Get the error output stream associated with this interpreter. This may be be stderr or the GUI console.

Specified by:
getErr in interface ConsoleInterface

println

public final void println(java.lang.String s)
Specified by:
println in interface ConsoleInterface

print

public final void print(java.lang.String s)
Specified by:
print in interface ConsoleInterface

debug

public static final void debug(java.lang.String s)
Print a debug message on debug stream associated with this interpreter only if debugging is turned on.


get

public java.lang.Object get(java.lang.String name)
                     throws EvalError
Get the value of the name. name may be any value. e.g. a variable or field


getu

java.lang.Object getu(java.lang.String name)
Unchecked get for internal use


set

public void set(java.lang.String name,
                java.lang.Object value)
         throws EvalError
Assign the value to the name. name may evaluate to anything assignable. e.g. a variable or field.


setu

void setu(java.lang.String name,
          java.lang.Object value)
Unchecked set for internal use


set

public void set(java.lang.String name,
                long value)
         throws EvalError

set

public void set(java.lang.String name,
                int value)
         throws EvalError

set

public void set(java.lang.String name,
                double value)
         throws EvalError

set

public void set(java.lang.String name,
                float value)
         throws EvalError

set

public void set(java.lang.String name,
                boolean value)
         throws EvalError

getVariable

public java.lang.Object getVariable(java.lang.String name)
Deprecated. does not properly evaluate compound names


setVariable

public void setVariable(java.lang.String name,
                        java.lang.Object value)
Deprecated. does not properly evaluate compound names


setVariable

public void setVariable(java.lang.String name,
                        int value)
Deprecated. does not properly evaluate compound names


setVariable

public void setVariable(java.lang.String name,
                        float value)
Deprecated. does not properly evaluate compound names


setVariable

public void setVariable(java.lang.String name,
                        boolean value)
Deprecated. does not properly evaluate compound names


get_jjtree

private JJTParserState get_jjtree()

get_jj_input_stream

private ASCII_UCodeESC_CharStream get_jj_input_stream()

Line

private boolean Line()
              throws ParseException

loadRCFiles

void loadRCFiles()

pathToFile

public java.io.File pathToFile(java.lang.String fileName)
                        throws java.io.IOException
Localize a path to the file name based on the bsh.cwd interpreter working directory.


redirectOutputToFile

public static void redirectOutputToFile(java.lang.String filename)

staticInit

static void staticInit()

getSourceFileInfo

public java.lang.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.


getParent

public Interpreter getParent()