Interface IScriptEvaluator

All Superinterfaces:
ICookable, IMultiCookable
All Known Implementing Classes:
ScriptEvaluator, ScriptEvaluator

public interface IScriptEvaluator extends ICookable, IMultiCookable
An engine that executes a script in JVM 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, Java SE 7 Edition, sections 7.5 and 14.

An implementation may or may not implement the concept of "local methods", i.e. method declarations being freely intermixed with statements.

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 IScriptEvaluator object, proceed as follows:

  1. Create an IScriptEvaluator-implementing class.
  2. Configure the IScriptEvaluator by calling any of the following methods:
  3. Call any of the Cookable.cook(Reader) methods to scan, parse, compile and load the script into the JVM.

After the IScriptEvaluator 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.

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

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