Java Doc for Interpreter.java in  » Scripting » beanshell » bsh » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Scripting » beanshell » bsh 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   bsh.Interpreter

Interpreter
public class Interpreter implements Runnable,ConsoleInterface,Serializable(Code)
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
public static  booleanDEBUGTRACELOCALSCOPING
    
final public static  StringVERSION
    
 ConsoleInterfaceconsole
    
static transient  PrintStreamdebug
    
transient  PrintStreamerr
    
protected  booleanevalOnlyinteractive
    
 NameSpaceglobalNameSpace
    
transient  Readerin
    
transient  PrintStreamout
    
 Interpreterparent
    
transient  Parserparser
    
static  ThissharedObject
    
 StringsourceFileInfo
    
static  StringsystemLineSeparator
    

Constructor 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.
Parameters:
  namespace - If namespace is non-null then this interpreter's root namespace will be set to the one provided.
public  Interpreter(Reader in, PrintStream out, PrintStream err, boolean interactive, NameSpace namespace)
    
public  Interpreter(Reader in, PrintStream out, PrintStream err, boolean interactive)
    
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  Interpreter()
     Create an interpreter for evaluation only.

Method Summary
final public static  voiddebug(String s)
     Print a debug message on debug stream associated with this interpreter only if debugging is turned on.
final public  voiderror(Object o)
     Print an error message in a standard format on the output stream associated with this interpreter.
public  Objecteval(Reader in, NameSpace nameSpace, String sourceFileInfo)
     Spawn a non-interactive local interpreter to evaluate text in the specified namespace.
public  Objecteval(Reader in)
     Evaluate the inputstream in this interpreter's global namespace.
public  Objecteval(String statements)
     Evaluate the string in this interpreter's global namespace.
public  Objecteval(String statements, NameSpace nameSpace)
     Evaluate the string in the specified namespace.
public  Objectget(String name)
     Get the value of the name. name may be any value.
public  BshClassManagergetClassManager()
     Get the class manager associated with this interpreter (the BshClassManager of this interpreter's global namespace).
public  PrintStreamgetErr()
     Get the error output stream associated with this interpreter.
public  ReadergetIn()
     Get the input stream associated with this interpreter.
public  ObjectgetInterface(Class interf)
     Get a reference to the interpreter (global namespace), cast to the specified interface type.
public  NameSpacegetNameSpace()
     Get the global namespace of this interpreter.

Note: This is here for completeness.

public  PrintStreamgetOut()
     Get the outptut stream associated with this interpreter.
public  InterpretergetParent()
     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.
public static  booleangetSaveClasses()
    
public static  StringgetSaveClassesDir()
    
public  booleangetShowResults()
     Show on/off verbose printing status for the show() command.
public  StringgetSourceFileInfo()
     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.
public  booleangetStrictJava()
    
 Objectgetu(String name)
    
public static  voidinvokeMain(Class clas, String[] args)
    
 voidloadRCFiles()
    
public static  voidmain(String[] args)
     Run the text only interpreter on the command line or specify a file.
public  FilepathToFile(String fileName)
     Localize a path to the file name based on the bsh.cwd interpreter working directory.
final public  voidprint(Object o)
    
final public  voidprintln(Object o)
    
public static  voidredirectOutputToFile(String filename)
    
public  voidrun()
     Run interactively.
public  voidset(String name, Object value)
     Assign the value to the name. name may evaluate to anything assignable.
public  voidset(String name, long value)
    
public  voidset(String name, int value)
    
public  voidset(String name, double value)
    
public  voidset(String name, float value)
    
public  voidset(String name, boolean value)
    
public  voidsetClassLoader(ClassLoader externalCL)
     Set an external class loader to be used as the base classloader for BeanShell.
public  voidsetConsole(ConsoleInterface console)
     Attach a console Note: this method is incomplete.
public  voidsetErr(PrintStream err)
    
public  voidsetExitOnEOF(boolean value)
     Specify whether, in interactive mode, the interpreter exits Java upon end of input.
public  voidsetNameSpace(NameSpace globalNameSpace)
     Set the global namespace for this interpreter.

Note: This is here for completeness.

public  voidsetOut(PrintStream out)
    
public  voidsetShowResults(boolean showResults)
     Turn on/off the verbose printing of results as for the show() command.
public  voidsetStrictJava(boolean b)
     Set strict Java mode on or off.
 voidsetu(String name, Object value)
    
public  Objectsource(String filename, NameSpace nameSpace)
     Read text from fileName and eval it.
public  Objectsource(String filename)
     Read text from fileName and eval it. Convenience method.
static  voidstaticInit()
    
public  voidunset(String name)
     Unassign the variable name. Name should evaluate to a variable.

Field Detail
DEBUGTRACELOCALSCOPING
public static boolean DEBUGTRACELOCALSCOPING(Code)



VERSION
final public static String VERSION(Code)



console
ConsoleInterface console(Code)



debug
static transient PrintStream debug(Code)



err
transient PrintStream err(Code)



evalOnlyinteractive
protected boolean evalOnlyinteractive(Code)



globalNameSpace
NameSpace globalNameSpace(Code)



in
transient Reader in(Code)



out
transient PrintStream out(Code)



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



parser
transient Parser parser(Code)



sharedObject
static This sharedObject(Code)
Shared system object visible under bsh.system



sourceFileInfo
String sourceFileInfo(Code)
The name of the file or other source that this interpreter is reading



systemLineSeparator
static String systemLineSeparator(Code)




Constructor Detail
Interpreter
public Interpreter(Reader in, PrintStream out, PrintStream err, boolean interactive, NameSpace namespace, Interpreter parent, String sourceFileInfo)(Code)
The main constructor. All constructors should now pass through here.
Parameters:
  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.
Parameters:
  parent - The parent interpreter if this interpreter is a child of another. May be null. Children share a BshClassManager withtheir parent instance.
Parameters:
  sourceFileInfo - An informative string holding the filename or other description of the source from which this interpreter isreading... used for debugging. May be null.



Interpreter
public Interpreter(Reader in, PrintStream out, PrintStream err, boolean interactive, NameSpace namespace)(Code)



Interpreter
public Interpreter(Reader in, PrintStream out, PrintStream err, boolean interactive)(Code)



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



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



Interpreter
public Interpreter()(Code)
Create an interpreter for evaluation only.




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



error
final public void error(Object o)(Code)
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
public Object eval(Reader in, NameSpace nameSpace, String sourceFileInfo) throws EvalError(Code)
Spawn a non-interactive local interpreter to evaluate text in the specified namespace. Return value is the evaluated object (or corresponding primitive wrapper).
Parameters:
  sourceFileInfo - is for information purposes only. It is used todisplay error messages (and in the future may be made available tothe script).
throws:
  EvalError - on script problems
throws:
  TargetError - on unhandled exceptions from the script



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



eval
public Object eval(String statements) throws EvalError(Code)
Evaluate the string in this interpreter's global namespace.



eval
public Object eval(String statements, NameSpace nameSpace) throws EvalError(Code)
Evaluate the string in the specified namespace.



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



getClassManager
public BshClassManager getClassManager()(Code)
Get the class manager associated with this interpreter (the BshClassManager of this interpreter's global namespace). This is primarily a convenience method.



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



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



getInterface
public Object getInterface(Class interf) throws EvalError(Code)
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.


throws:
  EvalError - if the interface cannot be generated because theversion of Java does not support the proxy mechanism.




getNameSpace
public NameSpace getNameSpace()(Code)
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
public PrintStream getOut()(Code)
Get the outptut stream associated with this interpreter. This may be be stdout or the GUI console.



getParent
public Interpreter getParent()(Code)
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.



getSaveClasses
public static boolean getSaveClasses()(Code)



getSaveClassesDir
public static String getSaveClassesDir()(Code)



getShowResults
public boolean getShowResults()(Code)
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
public String getSourceFileInfo()(Code)
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.
See Also:   bsh.SimpleNode.getSourceFile



getStrictJava
public boolean getStrictJava()(Code)

See Also:   Interpreter.setStrictJava(boolean)



getu
Object getu(String name)(Code)
Unchecked get for internal use



invokeMain
public static void invokeMain(Class clas, String[] args) throws Exception(Code)



loadRCFiles
void loadRCFiles()(Code)



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



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



print
final public void print(Object o)(Code)



println
final public void println(Object o)(Code)



redirectOutputToFile
public static void redirectOutputToFile(String filename)(Code)



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



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



set
public void set(String name, long value) throws EvalError(Code)



set
public void set(String name, int value) throws EvalError(Code)



set
public void set(String name, double value) throws EvalError(Code)



set
public void set(String name, float value) throws EvalError(Code)



set
public void set(String name, boolean value) throws EvalError(Code)



setClassLoader
public void setClassLoader(ClassLoader externalCL)(Code)
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.


See Also:   BshClassManager.setClassLoader(ClassLoader)




setConsole
public void setConsole(ConsoleInterface console)(Code)
Attach a console Note: this method is incomplete.



setErr
public void setErr(PrintStream err)(Code)



setExitOnEOF
public void setExitOnEOF(boolean value)(Code)
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
public void setNameSpace(NameSpace globalNameSpace)(Code)
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
public void setOut(PrintStream out)(Code)



setShowResults
public void setShowResults(boolean showResults)(Code)
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
public void setStrictJava(boolean b)(Code)
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
void setu(String name, Object value)(Code)
Unchecked set for internal use



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



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



staticInit
static void staticInit()(Code)



unset
public void unset(String name) throws EvalError(Code)
Unassign the variable name. Name should evaluate to a variable.



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.