koala.dynamicjava.interpreter
Class TreeInterpreter

java.lang.Object
  |
  +--koala.dynamicjava.interpreter.TreeInterpreter
All Implemented Interfaces:
Interpreter

public class TreeInterpreter
extends java.lang.Object
implements Interpreter

This class contains method to interpret the constructs of the language.


Inner Class Summary
protected  class TreeInterpreter.ConstructorParametersDescriptor
          Used to store the informations about explicit constructors invocation
protected  class TreeInterpreter.MethodDescriptor
          Used to store the informations about dynamically created methods
 
Field Summary
protected  boolean accessible
          Track the state of calls to 'setAccessible'
protected  Context checkVisitorContext
           
protected  TreeClassLoader classLoader
          The class loader
protected static java.util.Map constructorParameters
          The explicit constructor call parameters
protected  Context evalVisitorContext
           
protected  LibraryFinder libraryFinder
          The library finder
protected static java.util.Map methods
          The methods
protected  Context nameVisitorContext
           
protected static int nClass
          Used to generate classes
protected  ParserFactory parserFactory
          The parser
 
Constructor Summary
TreeInterpreter(ParserFactory pf)
          Creates a new interpreter
TreeInterpreter(ParserFactory pf, java.lang.ClassLoader cl)
          Creates a new interpreter
 
Method Summary
 void addClassPath(java.lang.String path)
          Adds a class search path
 void addClassURL(java.net.URL url)
          Adds a class search URL
 void addLibraryPath(java.lang.String path)
          Adds a library search path
 void addLibrarySuffix(java.lang.String s)
          Adds a library file suffix
 java.util.List buildStatementList(java.io.Reader r, java.lang.String fname)
          Parses a script and creates the associated syntax trees.
 java.lang.Class defineClass(java.lang.String name, byte[] code)
          Converts an array of bytes into an instance of the class Class
 void defineVariable(java.lang.String name, boolean value)
          Defines a boolean variable in the interpreter environment
 void defineVariable(java.lang.String name, byte value)
          Defines a byte variable in the interpreter environment
 void defineVariable(java.lang.String name, char value)
          Defines a char variable in the interpreter environment
 void defineVariable(java.lang.String name, double value)
          Defines an double variable in the interpreter environment
 void defineVariable(java.lang.String name, float value)
          Defines an float variable in the interpreter environment
 void defineVariable(java.lang.String name, int value)
          Defines an int variable in the interpreter environment
 void defineVariable(java.lang.String name, long value)
          Defines an long variable in the interpreter environment
 void defineVariable(java.lang.String name, java.lang.Object value)
          Defines a variable in the interpreter environment
 void defineVariable(java.lang.String name, java.lang.Object value, java.lang.Class c)
          Defines a variable in the interpreter environment
 void defineVariable(java.lang.String name, short value)
          Defines a short variable in the interpreter environment
protected  void finalize()
          Called before the destruction of the interpreter
 boolean getAccessible()
          Observe the state of calls to setAccessible()
 java.lang.ClassLoader getClassLoader()
          Gets the class loader
 java.util.Set getClassNames()
          Returns the defined class names
 java.lang.Class getExceptionClass()
          Returns the class of the execution exception
 LibraryFinder getLibraryFinder()
          Gets the library finder
 ParserFactory getParserFactory()
          Gets the parser factory
 java.lang.Object getVariable(java.lang.String name)
          Gets the value of a variable
 java.lang.Class getVariableClass(java.lang.String name)
          Gets the class of a variable
 java.util.Set getVariableNames()
          Returns the defined variable names
 java.lang.Object interpret(java.io.InputStream is, java.lang.String fname)
          Runs the interpreter
 java.lang.Object interpret(java.util.List statements)
          Runs the interpreter on a statement list.
 java.lang.Object interpret(java.io.Reader r, java.lang.String fname)
          Runs the interpreter
 java.lang.Object interpret(java.lang.String fname)
          Runs the interpreter
protected  java.lang.Object[] interpretArguments(java.lang.Class c, TreeInterpreter.ConstructorParametersDescriptor cpd, java.lang.Object[] args)
          This method is used to implement constructor invocation.
static java.lang.Object[] interpretArguments(java.lang.String key, java.lang.Object[] args)
          This method is used to implement constructor invocation.
protected  java.lang.Object interpretMethod(java.lang.Class c, TreeInterpreter.MethodDescriptor md, java.lang.Object obj, java.lang.Object[] params)
          Interprets the body of a method
static java.lang.Object invokeMethod(java.lang.String key, java.lang.Object obj, java.lang.Object[] params)
          Interprets the body of a method
 java.lang.Class loadClass(java.lang.String name)
          Loads an interpreted class
 void registerConstructorArguments(java.lang.String sig, java.util.List params, java.util.List exprs, ImportationManager im)
          Registers a constructor arguments
 void registerMethod(java.lang.String sig, MethodDeclaration md, ImportationManager im)
          Registers a method.
 void setAccessible(boolean accessible)
          Set the interpreter contexts to override public/protected/private access restrictions on the methods and fields it handles.
 void setVariable(java.lang.String name, java.lang.Object value)
          Sets the value of a variable
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

parserFactory

protected ParserFactory parserFactory
The parser

libraryFinder

protected LibraryFinder libraryFinder
The library finder

classLoader

protected TreeClassLoader classLoader
The class loader

methods

protected static java.util.Map methods
The methods

constructorParameters

protected static java.util.Map constructorParameters
The explicit constructor call parameters

nClass

protected static int nClass
Used to generate classes

nameVisitorContext

protected Context nameVisitorContext

checkVisitorContext

protected Context checkVisitorContext

evalVisitorContext

protected Context evalVisitorContext

accessible

protected boolean accessible
Track the state of calls to 'setAccessible'
See Also:
setAccessible(boolean)
Constructor Detail

TreeInterpreter

public TreeInterpreter(ParserFactory pf)
Creates a new interpreter
Parameters:
pf - the parser factory

TreeInterpreter

public TreeInterpreter(ParserFactory pf,
                       java.lang.ClassLoader cl)
Creates a new interpreter
Parameters:
pf - the parser factory
cl - the auxiliary class loader used to load external classes
Method Detail

interpret

public java.lang.Object interpret(java.io.Reader r,
                                  java.lang.String fname)
                           throws InterpreterException
Runs the interpreter
Specified by:
interpret in interface Interpreter
Parameters:
is - the reader from which the statements are read
fname - the name of the parsed stream
Returns:
the result of the evaluation of the last statement

interpret

public java.lang.Object interpret(java.io.InputStream is,
                                  java.lang.String fname)
                           throws InterpreterException
Runs the interpreter
Specified by:
interpret in interface Interpreter
Parameters:
is - the input stream from which the statements are read
fname - the name of the parsed stream
Returns:
the result of the evaluation of the last statement

interpret

public java.lang.Object interpret(java.lang.String fname)
                           throws InterpreterException,
                                  java.io.IOException
Runs the interpreter
Specified by:
interpret in interface Interpreter
Parameters:
fname - the name of a file to interpret
Returns:
the result of the evaluation of the last statement

buildStatementList

public java.util.List buildStatementList(java.io.Reader r,
                                         java.lang.String fname)
                                  throws InterpreterException
Parses a script and creates the associated syntax trees.
Parameters:
is - the reader from which the statements are read
fname - the name of the parsed stream
Returns:
list of statements

interpret

public java.lang.Object interpret(java.util.List statements)
                           throws InterpreterException
Runs the interpreter on a statement list.
Parameters:
statements - the statement list to evaluate
fname - the name of the parsed stream
Returns:
the result of the evaluation of the last statement

defineVariable

public void defineVariable(java.lang.String name,
                           java.lang.Object value,
                           java.lang.Class c)
Defines a variable in the interpreter environment
Parameters:
name - the variable's name
value - the initial value of the variable
c - the variable's type.
Throws:
java.lang.IllegalStateException - if name is already defined

defineVariable

public void defineVariable(java.lang.String name,
                           java.lang.Object value)
Defines a variable in the interpreter environment
Specified by:
defineVariable in interface Interpreter
Parameters:
name - the variable's name
value - the initial value of the variable
Throws:
java.lang.IllegalStateException - if name is already defined

defineVariable

public void defineVariable(java.lang.String name,
                           boolean value)
Defines a boolean variable in the interpreter environment
Parameters:
name - the variable's name
value - the initial value of the variable
Throws:
java.lang.IllegalStateException - if name is already defined

defineVariable

public void defineVariable(java.lang.String name,
                           byte value)
Defines a byte variable in the interpreter environment
Parameters:
name - the variable's name
value - the initial value of the variable
Throws:
java.lang.IllegalStateException - if name is already defined

defineVariable

public void defineVariable(java.lang.String name,
                           short value)
Defines a short variable in the interpreter environment
Parameters:
name - the variable's name
value - the initial value of the variable
Throws:
java.lang.IllegalStateException - if name is already defined

defineVariable

public void defineVariable(java.lang.String name,
                           char value)
Defines a char variable in the interpreter environment
Parameters:
name - the variable's name
value - the initial value of the variable
Throws:
java.lang.IllegalStateException - if name is already defined

defineVariable

public void defineVariable(java.lang.String name,
                           int value)
Defines an int variable in the interpreter environment
Parameters:
name - the variable's name
value - the initial value of the variable
Throws:
java.lang.IllegalStateException - if name is already defined

defineVariable

public void defineVariable(java.lang.String name,
                           long value)
Defines an long variable in the interpreter environment
Parameters:
name - the variable's name
value - the initial value of the variable
Throws:
java.lang.IllegalStateException - if name is already defined

defineVariable

public void defineVariable(java.lang.String name,
                           float value)
Defines an float variable in the interpreter environment
Parameters:
name - the variable's name
value - the initial value of the variable
Throws:
java.lang.IllegalStateException - if name is already defined

defineVariable

public void defineVariable(java.lang.String name,
                           double value)
Defines an double variable in the interpreter environment
Parameters:
name - the variable's name
value - the initial value of the variable
Throws:
java.lang.IllegalStateException - if name is already defined

setVariable

public void setVariable(java.lang.String name,
                        java.lang.Object value)
Sets the value of a variable
Specified by:
setVariable in interface Interpreter
Parameters:
name - the variable's name
value - the value of the variable
Throws:
java.lang.IllegalStateException - if the assignment is invalid

getVariable

public java.lang.Object getVariable(java.lang.String name)
Gets the value of a variable
Specified by:
getVariable in interface Interpreter
Parameters:
name - the variable's name
Throws:
java.lang.IllegalStateException - if the variable do not exist

getVariableClass

public java.lang.Class getVariableClass(java.lang.String name)
Gets the class of a variable
Specified by:
getVariableClass in interface Interpreter
Parameters:
name - the variable's name
Throws:
java.lang.IllegalStateException - if the variable do not exist

getVariableNames

public java.util.Set getVariableNames()
Returns the defined variable names
Specified by:
getVariableNames in interface Interpreter
Returns:
a set of strings

setAccessible

public void setAccessible(boolean accessible)
Description copied from interface: Interpreter
Set the interpreter contexts to override public/protected/private access restrictions on the methods and fields it handles. Default should be false, i.e. enforce Java access. Setting to true should override this and allow access to all fields.
Specified by:
setAccessible in interface Interpreter

getAccessible

public boolean getAccessible()
Description copied from interface: Interpreter
Observe the state of calls to setAccessible()
Specified by:
getAccessible in interface Interpreter

getClassNames

public java.util.Set getClassNames()
Returns the defined class names
Specified by:
getClassNames in interface Interpreter
Returns:
a set of strings

addClassPath

public void addClassPath(java.lang.String path)
Adds a class search path
Specified by:
addClassPath in interface Interpreter
Parameters:
path - the path to add

addClassURL

public void addClassURL(java.net.URL url)
Adds a class search URL
Specified by:
addClassURL in interface Interpreter
Parameters:
url - the url to add

addLibraryPath

public void addLibraryPath(java.lang.String path)
Adds a library search path
Specified by:
addLibraryPath in interface Interpreter
Parameters:
path - the path to add

addLibrarySuffix

public void addLibrarySuffix(java.lang.String s)
Adds a library file suffix
Specified by:
addLibrarySuffix in interface Interpreter
Parameters:
s - the suffix to add

loadClass

public java.lang.Class loadClass(java.lang.String name)
                          throws java.lang.ClassNotFoundException
Loads an interpreted class
Specified by:
loadClass in interface Interpreter
Parameters:
s - the fully qualified name of the class to load
Throws:
java.lang.ClassNotFoundException - if the class cannot be find

defineClass

public java.lang.Class defineClass(java.lang.String name,
                                   byte[] code)
Converts an array of bytes into an instance of the class Class
Specified by:
defineClass in interface Interpreter
Throws:
java.lang.ClassFormatError - if the class cannot be defined

getClassLoader

public java.lang.ClassLoader getClassLoader()
Gets the class loader
Specified by:
getClassLoader in interface Interpreter

getLibraryFinder

public LibraryFinder getLibraryFinder()
Gets the library finder
Specified by:
getLibraryFinder in interface Interpreter

getParserFactory

public ParserFactory getParserFactory()
Gets the parser factory
Specified by:
getParserFactory in interface Interpreter

getExceptionClass

public java.lang.Class getExceptionClass()
Returns the class of the execution exception

registerMethod

public void registerMethod(java.lang.String sig,
                           MethodDeclaration md,
                           ImportationManager im)
Registers a method.
Parameters:
sig - the method's signature
md - the method declaration
im - the importation manager

invokeMethod

public static java.lang.Object invokeMethod(java.lang.String key,
                                            java.lang.Object obj,
                                            java.lang.Object[] params)
Interprets the body of a method
Parameters:
key - the key used to find the body of a method
obj - the object (this)
params - the arguments

interpretMethod

protected java.lang.Object interpretMethod(java.lang.Class c,
                                           TreeInterpreter.MethodDescriptor md,
                                           java.lang.Object obj,
                                           java.lang.Object[] params)
Interprets the body of a method
Parameters:
c - the declaring class of the method
md - the method descriptor
obj - the object (this)
params - the arguments

registerConstructorArguments

public void registerConstructorArguments(java.lang.String sig,
                                         java.util.List params,
                                         java.util.List exprs,
                                         ImportationManager im)
Registers a constructor arguments

interpretArguments

public static java.lang.Object[] interpretArguments(java.lang.String key,
                                                    java.lang.Object[] args)
This method is used to implement constructor invocation.
Parameters:
key - the key used to find the informations about the constructor
args - the arguments passed to this constructor
Returns:
the arguments to give to the 'super' or 'this' constructor followed by the new values of the constructor arguments

interpretArguments

protected java.lang.Object[] interpretArguments(java.lang.Class c,
                                                TreeInterpreter.ConstructorParametersDescriptor cpd,
                                                java.lang.Object[] args)
This method is used to implement constructor invocation.
Parameters:
c - the declaring class of the constructor
cpd - the parameter descriptor
args - the arguments passed to this constructor
Returns:
the arguments to give to the 'super' or 'this' constructor followed by the new values of the constructor arguments

finalize

protected void finalize()
                 throws java.lang.Throwable
Called before the destruction of the interpreter
Overrides:
finalize in class java.lang.Object


Copyright © 2001 Stephane Hillion. All Rights Reserved.