Web Site

org.codehaus.janino
Class ScriptEvaluator

java.lang.Object
  extended by org.codehaus.janino.Cookable
      extended by org.codehaus.janino.SimpleCompiler
          extended by org.codehaus.janino.ClassBodyEvaluator
              extended by org.codehaus.janino.ScriptEvaluator
Direct Known Subclasses:
ExpressionEvaluator

public class ScriptEvaluator
extends ClassBodyEvaluator

An engine that executes a script in JavaTM bytecode.

The syntax of the script to compile is a sequence of import declarations (not allowed if you compile many scripts at a time, see below) followed by a sequence of statements, as defined in the Java Language Specification, 2nd edition, sections 7.5 and 14.

Example:

   import java.text.*;
 
   System.out.println("HELLO");
   System.out.println(new DecimalFormat("####,###.##").format(a));
 
(Notice that this expression refers to a parameter "a", as explained below.)

The script may complete abnormally, e.g. through a RETURN statement:

   if (a == null) {
       System.out.println("Oops!");
       return;
   }
 
Optionally, the script may be declared with a non-void return type. In this case, the last statement of the script must be a RETURN statement (or a THROW statement), and all RETURN statements in the script must return a value with the given type.

The script evaluator is implemented by creating and compiling a temporary compilation unit defining one class with one method the body of which consists of the statements of the script.

To set up a ScriptEvaluator object, proceed as follows:

  1. Create the ScriptEvaluator using ScriptEvaluator()
  2. Configure the ScriptEvaluator by calling any of the following methods:
  3. Call any of the Cookable.cook(Scanner) methods to scan, parse, compile and load the script into the JVM.
After the ScriptEvaluator object is created, the script can be executed as often with different parameter values (see evaluate(Object[])). This execution is very fast, compared to the compilation.

Less common methods exist that allow for the specification of the name of the generated class, the class it extends, the interfaces it implements, the name of the method that executes the script, the exceptions that this method (i.e. the script) is allowed to throw, and the ClassLoader that is used to define the generated class and to load classes referenced by the script.

Alternatively, a number of "convenience constructors" exist that execute the steps described above instantly. Their use is discouraged.

If you want to compile many scripts at the same time, you have the option to cook an array of scripts in one ScriptEvaluator by using the following methods:

Notice that these methods have array parameters in contrast to their one-script brethren.


Field Summary
protected  java.lang.String[] optionalMethodNames
           
protected  java.lang.String[][] optionalParameterNames
           
protected  java.lang.Class[][] optionalParameterTypes
           
protected  java.lang.Class[] optionalReturnTypes
           
protected  boolean[] optionalStaticMethod
           
protected  java.lang.Class[][] optionalThrownExceptions
           
 
Fields inherited from class org.codehaus.janino.ClassBodyEvaluator
className, DEFAULT_CLASS_NAME, ZERO_CLASSES
 
Fields inherited from class org.codehaus.janino.SimpleCompiler
BOOT_CLASS_LOADER
 
Constructor Summary
ScriptEvaluator()
           
ScriptEvaluator(Scanner scanner, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, java.lang.Class returnType, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes, java.lang.Class[] thrownExceptions, java.lang.ClassLoader optionalParentClassLoader)
          Equivalent to
ScriptEvaluator(Scanner scanner, java.lang.Class returnType, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes, java.lang.Class[] thrownExceptions, java.lang.ClassLoader optionalParentClassLoader)
          Equivalent to
ScriptEvaluator(Scanner scanner, java.lang.String className, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, boolean staticMethod, java.lang.Class returnType, java.lang.String methodName, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes, java.lang.Class[] thrownExceptions, java.lang.ClassLoader optionalParentClassLoader)
          Equivalent to
ScriptEvaluator(java.lang.String script)
          Equivalent to
ScriptEvaluator(java.lang.String script, java.lang.Class returnType)
          Equivalent to
ScriptEvaluator(java.lang.String script, java.lang.Class returnType, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes)
          Equivalent to
ScriptEvaluator(java.lang.String script, java.lang.Class returnType, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes, java.lang.Class[] thrownExceptions)
          Equivalent to
ScriptEvaluator(java.lang.String optionalFileName, java.io.InputStream is, java.lang.Class returnType, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes, java.lang.Class[] thrownExceptions, java.lang.ClassLoader optionalParentClassLoader)
          Equivalent to
ScriptEvaluator(java.lang.String optionalFileName, java.io.Reader reader, java.lang.Class returnType, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes, java.lang.Class[] thrownExceptions, java.lang.ClassLoader optionalParentClassLoader)
          Equivalent to
 
Method Summary
protected  void compileToMethods(Java.CompilationUnit compilationUnit, java.lang.String[] methodNames, java.lang.Class[][] parameterTypes)
           
 void cook(java.io.Reader[] readers)
           
 void cook(Scanner scanner)
          To be implemented by the derived classes.
 void cook(Scanner[] scanners)
          Like cook(Scanner), but cooks a set of scripts into one class.
 void cook(java.lang.String[] strings)
          Cook tokens from Strings.
 void cook(java.lang.String[] optionalFileNames, java.io.Reader[] readers)
           
static java.lang.Object createFastEvaluator(ScriptEvaluator se, Scanner scanner, java.lang.String[] parameterNames, java.lang.Class interfaceToImplement)
          Create and return an object that implements the exactly one method of the given interfaceToImplement.
static java.lang.Object createFastEvaluator(ScriptEvaluator se, java.lang.String s, java.lang.String[] parameterNames, java.lang.Class interfaceToImplement)
           
static java.lang.Object createFastScriptEvaluator(Scanner scanner, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader optionalParentClassLoader)
          If the parameter and return types of the expression are known at compile time, then a "fast" script evaluator can be instantiated through this method.
static java.lang.Object createFastScriptEvaluator(Scanner scanner, java.lang.String[] optionalDefaultImports, java.lang.String className, java.lang.Class optionalExtendedType, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader optionalParentClassLoader)
           
static java.lang.Object createFastScriptEvaluator(Scanner scanner, java.lang.String className, java.lang.Class optionalExtendedType, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader optionalParentClassLoader)
          Like createFastScriptEvaluator(Scanner, Class, String[], ClassLoader), but gives you more control over the generated class (rarely needed in practice).
static java.lang.Object createFastScriptEvaluator(java.lang.String script, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames)
          Simplified version of createFastScriptEvaluator(Scanner, Class, String[], ClassLoader).
 java.lang.Object evaluate(int idx, java.lang.Object[] parameterValues)
          Calls the generated method with concrete parameter values.
 java.lang.Object evaluate(java.lang.Object[] parameterValues)
          Calls the generated method with concrete parameter values.
protected  java.lang.Class getDefaultReturnType()
           
 java.lang.reflect.Method getMethod()
          Returns the loaded Method.
 java.lang.reflect.Method getMethod(int idx)
          Returns the loaded Method.
static java.lang.String[] guessParameterNames(Scanner scanner)
          Guess the names of the parameters used in the given expression.
protected  Java.Block makeBlock(int idx, Scanner scanner)
          Fill the given block by parsing statements until EOF and adding them to the block.
protected  Java.MethodDeclarator makeMethodDeclaration(Location location, boolean staticMethod, java.lang.Class returnType, java.lang.String methodName, java.lang.Class[] parameterTypes, java.lang.String[] parameterNames, java.lang.Class[] thrownExceptions, Java.Block optionalBody)
          To the given Java.ClassDeclaration, add A public method declaration with the given return type, name, parameter names and values and thrown exceptions A block
 void setMethodName(java.lang.String methodName)
          Define the name of the generated method.
 void setMethodNames(java.lang.String[] methodNames)
          Define the names of the generated methods.
 void setParameters(java.lang.String[][] parameterNames, java.lang.Class[][] parameterTypes)
          Define the names and types of the parameters of the generated methods.
 void setParameters(java.lang.String[] parameterNames, java.lang.Class[] parameterTypes)
          Define the names and types of the parameters of the generated method.
 void setReturnType(java.lang.Class returnType)
          Define the return type of the generated method.
 void setReturnTypes(java.lang.Class[] returnTypes)
          Define the return types of the scripts.
 void setStaticMethod(boolean staticMethod)
          Define whether the generated method should be STATIC or not.
 void setStaticMethod(boolean[] staticMethod)
          Define whether the methods implementing each script should be STATIC or not.
 void setThrownExceptions(java.lang.Class[] thrownExceptions)
          Define the exceptions that the generated method may throw.
 void setThrownExceptions(java.lang.Class[][] thrownExceptions)
          Define the exceptions that the generated methods may throw.
 
Methods inherited from class org.codehaus.janino.ClassBodyEvaluator
addPackageMemberClassDeclaration, compileToClass, createFastClassBodyEvaluator, createFastClassBodyEvaluator, getClazz, makeCompilationUnit, setClassName, setDefaultImports, setExtendedType, setImplementedTypes
 
Methods inherited from class org.codehaus.janino.SimpleCompiler
classesToTypes, classToType, compileToClassLoader, equals, getClassLoader, hashCode, main, setParentClassLoader, setParentClassLoader, setUpClassLoaders
 
Methods inherited from class org.codehaus.janino.Cookable
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

optionalStaticMethod

protected boolean[] optionalStaticMethod

optionalReturnTypes

protected java.lang.Class[] optionalReturnTypes

optionalMethodNames

protected java.lang.String[] optionalMethodNames

optionalParameterNames

protected java.lang.String[][] optionalParameterNames

optionalParameterTypes

protected java.lang.Class[][] optionalParameterTypes

optionalThrownExceptions

protected java.lang.Class[][] optionalThrownExceptions
Constructor Detail

ScriptEvaluator

public ScriptEvaluator(java.lang.String script)
                throws CompileException,
                       Parser.ParseException,
                       Scanner.ScanException
Equivalent to
 ScriptEvaluator se = new ScriptEvaluator();
 se.cook(script);

Throws:
CompileException
Parser.ParseException
Scanner.ScanException
See Also:
ScriptEvaluator(), Cookable.cook(String)

ScriptEvaluator

public ScriptEvaluator(java.lang.String script,
                       java.lang.Class returnType)
                throws CompileException,
                       Parser.ParseException,
                       Scanner.ScanException
Equivalent to
 ScriptEvaluator se = new ScriptEvaluator();
 se.setReturnType(returnType);
 se.cook(script);

Throws:
CompileException
Parser.ParseException
Scanner.ScanException
See Also:
ScriptEvaluator(), setReturnType(Class), Cookable.cook(String)

ScriptEvaluator

public ScriptEvaluator(java.lang.String script,
                       java.lang.Class returnType,
                       java.lang.String[] parameterNames,
                       java.lang.Class[] parameterTypes)
                throws CompileException,
                       Parser.ParseException,
                       Scanner.ScanException
Equivalent to
 ScriptEvaluator se = new ScriptEvaluator();
 se.setReturnType(returnType);
 se.setParameters(parameterNames, parameterTypes);
 se.cook(script);

Throws:
CompileException
Parser.ParseException
Scanner.ScanException
See Also:
ScriptEvaluator(), setReturnType(Class), setParameters(String[], Class[]), Cookable.cook(String)

ScriptEvaluator

public ScriptEvaluator(java.lang.String script,
                       java.lang.Class returnType,
                       java.lang.String[] parameterNames,
                       java.lang.Class[] parameterTypes,
                       java.lang.Class[] thrownExceptions)
                throws CompileException,
                       Parser.ParseException,
                       Scanner.ScanException
Equivalent to
 ScriptEvaluator se = new ScriptEvaluator();
 se.setReturnType(returnType);
 se.setParameters(parameterNames, parameterTypes);
 se.setThrownExceptions(thrownExceptions);
 se.cook(script);

Throws:
CompileException
Parser.ParseException
Scanner.ScanException
See Also:
ScriptEvaluator(), setReturnType(Class), setParameters(String[], Class[]), setThrownExceptions(Class[]), Cookable.cook(String)

ScriptEvaluator

public ScriptEvaluator(java.lang.String optionalFileName,
                       java.io.InputStream is,
                       java.lang.Class returnType,
                       java.lang.String[] parameterNames,
                       java.lang.Class[] parameterTypes,
                       java.lang.Class[] thrownExceptions,
                       java.lang.ClassLoader optionalParentClassLoader)
                throws CompileException,
                       Parser.ParseException,
                       Scanner.ScanException,
                       java.io.IOException
Equivalent to
 ScriptEvaluator se = new ScriptEvaluator();
 se.setReturnType(returnType);
 se.setParameters(parameterNames, parameterTypes);
 se.setThrownExceptions(thrownExceptions);
 se.setParentClassLoader(optionalParentClassLoader);
 se.cook(optionalFileName, is);

Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException
See Also:
ScriptEvaluator(), setReturnType(Class), setParameters(String[], Class[]), setThrownExceptions(Class[]), SimpleCompiler.setParentClassLoader(ClassLoader), Cookable.cook(String, InputStream)

ScriptEvaluator

public ScriptEvaluator(java.lang.String optionalFileName,
                       java.io.Reader reader,
                       java.lang.Class returnType,
                       java.lang.String[] parameterNames,
                       java.lang.Class[] parameterTypes,
                       java.lang.Class[] thrownExceptions,
                       java.lang.ClassLoader optionalParentClassLoader)
                throws CompileException,
                       Parser.ParseException,
                       Scanner.ScanException,
                       java.io.IOException
Equivalent to
 ScriptEvaluator se = new ScriptEvaluator();
 se.setReturnType(returnType);
 se.setParameters(parameterNames, parameterTypes);
 se.setThrownExceptions(thrownExceptions);
 se.setParentClassLoader(optionalParentClassLoader);
 se.cook(reader);

Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException
See Also:
ScriptEvaluator(), setReturnType(Class), setParameters(String[], Class[]), setThrownExceptions(Class[]), SimpleCompiler.setParentClassLoader(ClassLoader), Cookable.cook(String, Reader)

ScriptEvaluator

public ScriptEvaluator(Scanner scanner,
                       java.lang.Class returnType,
                       java.lang.String[] parameterNames,
                       java.lang.Class[] parameterTypes,
                       java.lang.Class[] thrownExceptions,
                       java.lang.ClassLoader optionalParentClassLoader)
                throws CompileException,
                       Parser.ParseException,
                       Scanner.ScanException,
                       java.io.IOException
Equivalent to
 ScriptEvaluator se = new ScriptEvaluator();
 se.setReturnType(returnType);
 se.setParameters(parameterNames, parameterTypes);
 se.setThrownExceptions(thrownExceptions);
 se.setParentClassLoader(optionalParentClassLoader);
 se.cook(scanner);

Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException
See Also:
ScriptEvaluator(), setReturnType(Class), setParameters(String[], Class[]), setThrownExceptions(Class[]), SimpleCompiler.setParentClassLoader(ClassLoader), Cookable.cook(Scanner)

ScriptEvaluator

public ScriptEvaluator(Scanner scanner,
                       java.lang.Class optionalExtendedType,
                       java.lang.Class[] implementedTypes,
                       java.lang.Class returnType,
                       java.lang.String[] parameterNames,
                       java.lang.Class[] parameterTypes,
                       java.lang.Class[] thrownExceptions,
                       java.lang.ClassLoader optionalParentClassLoader)
                throws CompileException,
                       Parser.ParseException,
                       Scanner.ScanException,
                       java.io.IOException
Equivalent to
 ScriptEvaluator se = new ScriptEvaluator();
 se.setExtendedType(optionalExtendedType);
 se.setImplementedTypes(implementedTypes);
 se.setReturnType(returnType);
 se.setParameters(parameterNames, parameterTypes);
 se.setThrownExceptions(thrownExceptions);
 se.setParentClassLoader(optionalParentClassLoader);
 se.cook(scanner);

Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException
See Also:
ScriptEvaluator(), ClassBodyEvaluator.setExtendedType(Class), ClassBodyEvaluator.setImplementedTypes(Class[]), setReturnType(Class), setParameters(String[], Class[]), setThrownExceptions(Class[]), SimpleCompiler.setParentClassLoader(ClassLoader), Cookable.cook(Scanner)

ScriptEvaluator

public ScriptEvaluator(Scanner scanner,
                       java.lang.String className,
                       java.lang.Class optionalExtendedType,
                       java.lang.Class[] implementedTypes,
                       boolean staticMethod,
                       java.lang.Class returnType,
                       java.lang.String methodName,
                       java.lang.String[] parameterNames,
                       java.lang.Class[] parameterTypes,
                       java.lang.Class[] thrownExceptions,
                       java.lang.ClassLoader optionalParentClassLoader)
                throws Scanner.ScanException,
                       Parser.ParseException,
                       CompileException,
                       java.io.IOException
Equivalent to
 ScriptEvaluator se = new ScriptEvaluator();
 se.setClassName(className);
 se.setExtendedType(optionalExtendedType);
 se.setImplementedTypes(implementedTypes);
 se.setStaticMethod(staticMethod);
 se.setReturnType(returnType);
 se.setMethodName(methodName);
 se.setParameters(parameterNames, parameterTypes);
 se.setThrownExceptions(thrownExceptions);
 se.setParentClassLoader(optionalParentClassLoader);
 se.cook(scanner);

Throws:
Scanner.ScanException
Parser.ParseException
CompileException
java.io.IOException
See Also:
ScriptEvaluator(), ClassBodyEvaluator.setClassName(String), ClassBodyEvaluator.setExtendedType(Class), ClassBodyEvaluator.setImplementedTypes(Class[]), setStaticMethod(boolean), setReturnType(Class), setMethodName(String), setParameters(String[], Class[]), setThrownExceptions(Class[]), SimpleCompiler.setParentClassLoader(ClassLoader), Cookable.cook(Scanner)

ScriptEvaluator

public ScriptEvaluator()
Method Detail

setStaticMethod

public void setStaticMethod(boolean staticMethod)
Define whether the generated method should be STATIC or not. Defaults to true.


setReturnType

public void setReturnType(java.lang.Class returnType)
Define the return type of the generated method. Defaults to void.class.


setMethodName

public void setMethodName(java.lang.String methodName)
Define the name of the generated method. Defaults to an unspecified name.


setParameters

public void setParameters(java.lang.String[] parameterNames,
                          java.lang.Class[] parameterTypes)
Define the names and types of the parameters of the generated method.


setThrownExceptions

public void setThrownExceptions(java.lang.Class[] thrownExceptions)
Define the exceptions that the generated method may throw.


cook

public final void cook(Scanner scanner)
                throws CompileException,
                       Parser.ParseException,
                       Scanner.ScanException,
                       java.io.IOException
Description copied from class: Cookable
To be implemented by the derived classes.

Overrides:
cook in class ClassBodyEvaluator
Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException

evaluate

public java.lang.Object evaluate(java.lang.Object[] parameterValues)
                          throws java.lang.reflect.InvocationTargetException
Calls the generated method with concrete parameter values.

Each parameter value must have the same type as specified through the "parameterTypes" parameter of setParameters(String[], Class[]).

Parameters of primitive type must passed with their wrapper class objects.

The object returned has the class as specified through setReturnType(Class).

This method is thread-safe.

Parameters:
parameterValues - The concrete parameter values.
Throws:
java.lang.reflect.InvocationTargetException

getMethod

public java.lang.reflect.Method getMethod()
Returns the loaded Method.

This method must only be called after cook(Scanner).

This method must not be called for instances of derived classes.


setStaticMethod

public void setStaticMethod(boolean[] staticMethod)
Define whether the methods implementing each script should be STATIC or not. By default all scripts are compiled into STATIC methods.


setReturnTypes

public void setReturnTypes(java.lang.Class[] returnTypes)
Define the return types of the scripts. By default all scripts have VOID return type.


setMethodNames

public void setMethodNames(java.lang.String[] methodNames)
Define the names of the generated methods. By default the methods have distinct and implementation-specific names.

If two scripts have the same name, then they must have different parameter types (see setParameters(String[][], Class[][])).


setParameters

public void setParameters(java.lang.String[][] parameterNames,
                          java.lang.Class[][] parameterTypes)
Define the names and types of the parameters of the generated methods.


setThrownExceptions

public void setThrownExceptions(java.lang.Class[][] thrownExceptions)
Define the exceptions that the generated methods may throw.


cook

public final void cook(Scanner[] scanners)
                throws CompileException,
                       Parser.ParseException,
                       Scanner.ScanException,
                       java.io.IOException
Like cook(Scanner), but cooks a set of scripts into one class. Notice that if any of the scripts causes trouble, the entire compilation will fail. If you need to report which of the scripts causes the exception, you may want to use the optionalFileName argument of Scanner.Scanner(String, Reader) to distinguish between the individual token sources.

On a 2 GHz Intel Pentium Core Duo under Windows XP with an IBM 1.4.2 JDK, compiling 10000 expressions "a + b" (integer) takes about 4 seconds and 56 MB of main memory. The generated class file is 639203 bytes large.

The number and the complexity of the scripts is restricted by the Limitations of the Java Virtual Machine, where the most limiting factor is the 64K entries limit of the constant pool. Since every method with a distinct name requires one entry there, you can define at best 32K (very simple) scripts. If and only if the number of scanners is one, then that single script may contain leading IMPORT directives.

Throws:
java.lang.IllegalStateException - if any of the preceeding set...() had an array size different from that of scanners
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException

cook

public final void cook(java.io.Reader[] readers)
                throws CompileException,
                       Parser.ParseException,
                       Scanner.ScanException,
                       java.io.IOException
Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException

cook

public final void cook(java.lang.String[] optionalFileNames,
                       java.io.Reader[] readers)
                throws CompileException,
                       Parser.ParseException,
                       Scanner.ScanException,
                       java.io.IOException
Parameters:
optionalFileNames - Used when reporting errors and warnings.
Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException

cook

public final void cook(java.lang.String[] strings)
                throws CompileException,
                       Parser.ParseException,
                       Scanner.ScanException
Cook tokens from Strings.

Throws:
CompileException
Parser.ParseException
Scanner.ScanException

getDefaultReturnType

protected java.lang.Class getDefaultReturnType()

makeBlock

protected Java.Block makeBlock(int idx,
                               Scanner scanner)
                        throws Parser.ParseException,
                               Scanner.ScanException,
                               java.io.IOException
Fill the given block by parsing statements until EOF and adding them to the block.

Throws:
Parser.ParseException
Scanner.ScanException
java.io.IOException

compileToMethods

protected void compileToMethods(Java.CompilationUnit compilationUnit,
                                java.lang.String[] methodNames,
                                java.lang.Class[][] parameterTypes)
                         throws CompileException
Throws:
CompileException

makeMethodDeclaration

protected Java.MethodDeclarator makeMethodDeclaration(Location location,
                                                      boolean staticMethod,
                                                      java.lang.Class returnType,
                                                      java.lang.String methodName,
                                                      java.lang.Class[] parameterTypes,
                                                      java.lang.String[] parameterNames,
                                                      java.lang.Class[] thrownExceptions,
                                                      Java.Block optionalBody)
To the given Java.ClassDeclaration, add

Parameters:
returnType - Return type of the declared method

createFastScriptEvaluator

public static java.lang.Object createFastScriptEvaluator(java.lang.String script,
                                                         java.lang.Class interfaceToImplement,
                                                         java.lang.String[] parameterNames)
                                                  throws CompileException,
                                                         Parser.ParseException,
                                                         Scanner.ScanException
Simplified version of createFastScriptEvaluator(Scanner, Class, String[], ClassLoader).

Parameters:
script - Contains the sequence of script tokens
interfaceToImplement - Must declare exactly the one method that defines the expression's signature
parameterNames - The expression references the parameters through these names
Returns:
an object that implements the given interface and extends the optionalExtendedType
Throws:
CompileException
Parser.ParseException
Scanner.ScanException

createFastScriptEvaluator

public static java.lang.Object createFastScriptEvaluator(Scanner scanner,
                                                         java.lang.Class interfaceToImplement,
                                                         java.lang.String[] parameterNames,
                                                         java.lang.ClassLoader optionalParentClassLoader)
                                                  throws CompileException,
                                                         Parser.ParseException,
                                                         Scanner.ScanException,
                                                         java.io.IOException
If the parameter and return types of the expression are known at compile time, then a "fast" script evaluator can be instantiated through this method.

Script evaluation is faster than through evaluate(Object[]), because it is not done through reflection but through direct method invocation.

Example:

 public interface Foo {
     int bar(int a, int b);
 }
 ...
 Foo f = (Foo) ScriptEvaluator.createFastScriptEvaluator(
     new Scanner(null, new StringReader("return a + b;")),
     Foo.class,
     new String[] { "a", "b" },
     (ClassLoader) null          // Use current thread's context class loader
 );
 System.out.println("1 + 2 = " + f.bar(1, 2));
 
Notice: The interfaceToImplement must either be declared public, or with package scope in the root package (i.e. "no" package).

Parameters:
scanner - Source of script tokens
interfaceToImplement - Must declare exactly one method
parameterNames -
optionalParentClassLoader -
Returns:
an object that implements the given interface
Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException

createFastScriptEvaluator

public static java.lang.Object createFastScriptEvaluator(Scanner scanner,
                                                         java.lang.String className,
                                                         java.lang.Class optionalExtendedType,
                                                         java.lang.Class interfaceToImplement,
                                                         java.lang.String[] parameterNames,
                                                         java.lang.ClassLoader optionalParentClassLoader)
                                                  throws CompileException,
                                                         Parser.ParseException,
                                                         Scanner.ScanException,
                                                         java.io.IOException
Like createFastScriptEvaluator(Scanner, Class, String[], ClassLoader), but gives you more control over the generated class (rarely needed in practice).

Notice: The interfaceToImplement must either be declared public, or with package scope in the same package as className.

Parameters:
scanner - Source of script tokens
className - Name of generated class
optionalExtendedType - Class to extend
interfaceToImplement - Must declare exactly the one method that defines the expression's signature
parameterNames - The expression references the parameters through these names
optionalParentClassLoader - Used to load referenced classes, defaults to the current thread's "context class loader"
Returns:
an object that implements the given interface and extends the optionalExtendedType
Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException

createFastScriptEvaluator

public static java.lang.Object createFastScriptEvaluator(Scanner scanner,
                                                         java.lang.String[] optionalDefaultImports,
                                                         java.lang.String className,
                                                         java.lang.Class optionalExtendedType,
                                                         java.lang.Class interfaceToImplement,
                                                         java.lang.String[] parameterNames,
                                                         java.lang.ClassLoader optionalParentClassLoader)
                                                  throws CompileException,
                                                         Parser.ParseException,
                                                         Scanner.ScanException,
                                                         java.io.IOException
Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException

createFastEvaluator

public static java.lang.Object createFastEvaluator(ScriptEvaluator se,
                                                   java.lang.String s,
                                                   java.lang.String[] parameterNames,
                                                   java.lang.Class interfaceToImplement)
                                            throws CompileException,
                                                   Parser.ParseException,
                                                   Scanner.ScanException
Throws:
CompileException
Parser.ParseException
Scanner.ScanException

createFastEvaluator

public static java.lang.Object createFastEvaluator(ScriptEvaluator se,
                                                   Scanner scanner,
                                                   java.lang.String[] parameterNames,
                                                   java.lang.Class interfaceToImplement)
                                            throws CompileException,
                                                   Parser.ParseException,
                                                   Scanner.ScanException,
                                                   java.io.IOException
Create and return an object that implements the exactly one method of the given interfaceToImplement.

Parameters:
se - A pre-configured ScriptEvaluator object
scanner - Source of tokens to read
parameterNames - The names of the parameters of the one abstract method of interfaceToImplement
interfaceToImplement - A type with exactly one abstract method
Returns:
an instance of the created Object
Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException

guessParameterNames

public static java.lang.String[] guessParameterNames(Scanner scanner)
                                              throws Parser.ParseException,
                                                     Scanner.ScanException,
                                                     java.io.IOException
Guess the names of the parameters used in the given expression. The strategy is to look at all "ambiguous names" in the expression (e.g. in "a.b.c.d()", the ambiguous name is "a.b.c"), and then at the components of the ambiguous name.

Throws:
Parser.ParseException
Scanner.ScanException
java.io.IOException
See Also:
Scanner.Scanner(String, Reader)

evaluate

public java.lang.Object evaluate(int idx,
                                 java.lang.Object[] parameterValues)
                          throws java.lang.reflect.InvocationTargetException
Calls the generated method with concrete parameter values.

Each parameter value must have the same type as specified through the "parameterTypes" parameter of setParameters(String[], Class[]).

Parameters of primitive type must passed with their wrapper class objects.

The object returned has the class as specified through setReturnType(Class).

This method is thread-safe.

Parameters:
idx - The index of the script (0 ... scripts.length - 1)
parameterValues - The concrete parameter values.
Throws:
java.lang.reflect.InvocationTargetException

getMethod

public java.lang.reflect.Method getMethod(int idx)
Returns the loaded Method.

This method must only be called after cook(Scanner).

This method must not be called for instances of derived classes.

Parameters:
idx - The index of the script (0 ... scripts.length - 1)

Web Site