The syntax of the expression to compile is that of a Java expression, as defined in JLS7, section 15. Notice that a Java expression does not have a concluding semicolon.
Example:
a + 7 * b
(Notice that this expression refers to two parameters "a" and "b", as explained below.)
The expression may optionally be preceeded with a sequence of import directives like
import java.text.*;
new DecimalFormat("####,###.##").format(10200020.345345)
(Notice that the import directive is concluded with a semicolon, while the expression is not.) This feature is not available if you compile many expressions at a time (see below).
To set up an IExpressionEvaluator object, proceed as follows:
IExpressionEvaluator-derived classIExpressionEvaluator by calling any of the following methods:setExpressionType(Class)
setParameters(String[], Class[])
setThrownExceptions(Class[])
setParentClassLoader(ClassLoader)
setDefaultImports(String[])
cook(String, java.io.Reader) methods to scan, parse, compile and load the expression
into the JVM.
After the IExpressionEvaluator object is set up, the expression can be evaluated as often with different
parameter values (see evaluate(Object[])). This evaluation 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 expression, the exceptions that
this method (i.e. the expression) is allowed to throw, and the ClassLoader that is used to define the
generated class and to load classes referenced by the expression.
If you want to compile many expressions at the same time, you have the option to cook an array of
expressions in one IExpressionEvaluator by using the following methods:
setMethodNames(String[])
setParameters(String[][], Class[][])
setExpressionTypes(Class[])
setStaticMethod(boolean[])
setThrownExceptions(Class[][])
cook(String[], Reader[])
evaluate(int, Object[])
Notice that these methods have array parameters in contrast to their one-expression brethren.
Notice that for functionally identical IExpressionEvaluators, java. will return true. E.g. "a+b" and "c + d" are functionally
identical if "a" and "c" have the same type, and so do "b" and "d".
'JLS7' refers to the Java Language Specification, Java SE 7 Edition.
| Modifier and Type | Field and Description |
|---|---|
| public static final Class | ANY_TYPE
Special value for |
| public static final String | DEFAULT_CLASS_NAME
The fully qualified name of the generated class, iff not reconfigured by |
| public static final Class | DEFAULT_EXPRESSION_TYPE
The type of all expressions that were not reconfigured with |
| Modifier and Type | Method and Description |
|---|---|
| public <T> T | createFastEvaluator(String expression, Class<? extends T> interfaceToImplement, String... parameterNames)
If the parameter and return types of the expression are known at compile time, then a "fast" expression evaluator
can be instantiated through |
| public <T> T | createFastEvaluator(Reader reader, Class<? extends T> interfaceToImplement, String... parameterNames)
|
| public Object | |
| public Object | |
| public Object | |
| public Class | |
| public Class | |
| public String[] | Returns: The default imports that were previously configured withsetDefaultImports(String.
|
| public Method | |
| public Method | |
| public Method[] | |
| public void | |
| public void | setCompileErrorHandler(ErrorHandler
null to restore the default behavior (throwing a CompileException)Installs an |
| public void | setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
Determines what kind of debugging information is included in the generates classes. |
| public void | setDefaultExpressionType(Class<?> defaultExpressionType)
Reconfigures the "default expression type"; if no expression type is configured for an expression, then, when
cooking this |
| public void | setDefaultImports(String... defaultImports)
"Default imports" add to the system import "java.lang", i.e. the evaluator may refer to classes imported by default imports without having to explicitly declare IMPORT statements. |
| public void | |
| public void | |
| public void | setExtendedClass(Class<?> extendedType)
Sets a particular superclass that the generated class will extend. |
| public void | setImplementedInterfaces(Class<?>[] implementedTypes)
Configures the interfaces that the generated class implements. |
| public void | setMethodName(String
null means reset to default nameDefines the name of the generated method. |
| public void | |
| public void | setOverrideMethod(boolean overrideMethod)
Defines whether the generated method overrides a methods declared in a supertype. |
| public void | setOverrideMethod(boolean[] overrideMethod)
Same as |
| public void | setParameters(String[] parameterNames, Class<?>[] parameterTypes)
Defines the names and types of the parameters of the generated method. |
| public void | setParameters(String[][] parameterNames, Class<?>[][] parameterTypes)
Same as |
| public void | setParentClassLoader(ClassLoader parentClassLoader)
The "parent class loader" is used to load referenced classes. |
| public void | |
| public void | |
| public void | setStaticMethod(boolean[] staticMethod)
Same as |
| public void | setThrownExceptions(Class<?>[] thrownExceptions)
Defines the exceptions that the generated method may throw. |
| public void | setThrownExceptions(Class<?>[][] thrownExceptions)
Same as |
| public void | setWarningHandler(WarningHandler
null to indicate that no warnings be issuedBy default, warnings are discarded, but an application my install a custom |
| ANY_TYPE | back to summary |
|---|---|
| public static final Class<?> ANY_TYPE
Deprecated Since autoboxing was introduced in JANINO, this feature is no longer necessary because you can use
expression type Special value for | |
| DEFAULT_CLASS_NAME | back to summary |
|---|---|
| public static final String DEFAULT_CLASS_NAME The fully qualified name of the generated class, iff not reconfigured by | |
| DEFAULT_EXPRESSION_TYPE | back to summary |
|---|---|
| public static final Class<?> DEFAULT_EXPRESSION_TYPE The type of all expressions that were not reconfigured with | |
| createFastEvaluator | back to summary |
|---|---|
| public <T> T createFastEvaluator(String expression, Class<? extends T> interfaceToImplement, String... parameterNames) throws CompileException If the parameter and return types of the expression are known at compile time, then a "fast" expression evaluator
can be instantiated through Example:
public interface Foo {
int bar(int a, int b);
}
...
ExpressionEvaluator ee = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator();
// Optionally configure the EE here...
ee.
All other configuration (implemented type, static method, return type, method name, parameter names and types, thrown exceptions) are predetermined by the interfaceToImplement.
Notice: The | |
| createFastEvaluator | back to summary |
|---|---|
| public <T> T createFastEvaluator(Reader reader, Class<? extends T> interfaceToImplement, String... parameterNames) throws CompileException, IOException | |
| evaluate | back to summary |
|---|---|
| public Object evaluate() throws InvocationTargetException Shorthand for
| |
| evaluate | back to summary |
|---|---|
| public Object evaluate(Object[] arguments) throws InvocationTargetException Evaluates the expression with concrete parameter values.
Each argument value must have the same type as specified through the "parameterTypes" parameter of Arguments of primitive type must passed with their wrapper class objects.
The object returned has the class as specified through This method is thread-safe.
Notice: In version 3.1.8, the arguments parameter was changed from | |
| evaluate | back to summary |
|---|---|
| public Object evaluate(int idx, Object[] arguments) throws InvocationTargetException Same as
Notice: In version 3.1.8, the arguments parameter was changed from
| |
| getClazz | back to summary |
|---|---|
public Class
| |
| getDefaultExpressionType | back to summary |
|---|---|
public Class
| |
| getDefaultImports | back to summary |
|---|---|
public String[] getDefaultImports()
| |
| getMethod | back to summary |
|---|---|
public Method getMethod()
| |
| getMethod | back to summary |
|---|---|
| public Method getMethod(int idx) Same as | |
| getResult | back to summary |
|---|---|
public Method[] getResult()
| |
| setClassName | back to summary |
|---|---|
| public void setClassName(String className) Sets the name of the generated class. Defaults to One reason to use this function is to have a class name in a non-default package, which can be relevant when types and members with DEFAULT accessibility are accessed. | |
| setCompileErrorHandler | back to summary |
|---|---|
| public void setCompileErrorHandler(ErrorHandler compileErrorHandler) Installs an
If the given
If the given
In other words: The
| |
| setDebuggingInformation | back to summary |
|---|---|
| public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars) Determines what kind of debugging information is included in the generates classes. The default is typically
" | |
| setDefaultExpressionType | back to summary |
|---|---|
| public void setDefaultExpressionType(Class<?> defaultExpressionType) Reconfigures the "default expression type"; if no expression type is configured for an expression, then, when
cooking this | |
| setDefaultImports | back to summary |
|---|---|
| public void setDefaultImports(String... defaultImports) "Default imports" add to the system import "java.lang", i.e. the evaluator may refer to classes imported by default imports without having to explicitly declare IMPORT statements. Notice that JDK 5 "static imports" are also supported, as shown here:
sc.setDefaultImports(
"java.util.Map", // Single type import
"java.io.*", // Type-import-on-demand
"static java.util.Collections.EMPTY_MAP", // Single static import
"static java.util.Collections.*", // Static-import-on-demand
);
| |
| setExpressionType | back to summary |
|---|---|
| public void setExpressionType(Class<?> expressionType) Defines the type of the expression.
Defaults to
If | |
| setExpressionTypes | back to summary |
|---|---|
| public void setExpressionTypes(Class<?>[] expressionTypes) Configures the types of the expressions.
Unless this method is called, all expressions have type
If any expression has type | |
| setExtendedClass | back to summary |
|---|---|
| public void setExtendedClass(Class<?> extendedType) Sets a particular superclass that the generated class will extend. If extendedClass is The usual reason to set a base class for an evaluator is that the generated class can directly access the superclass's (non-private) members. | |
| setImplementedInterfaces | back to summary |
|---|---|
| public void setImplementedInterfaces(Class<?>[] implementedTypes) Configures the interfaces that the generated class implements. | |
| setMethodName | back to summary |
|---|---|
| public void setMethodName(String methodName) Defines the name of the generated method. Initially, the method name is eval*.
| |
| setMethodNames | back to summary |
|---|---|
| public void setMethodNames(String[] methodNames) Same as Define the names of the generated methods. By default the methods have distinct and implementation-specific names.
If two expressions have the same name, then they must have different parameter types (see | |
| setOverrideMethod | back to summary |
|---|---|
| public void setOverrideMethod(boolean overrideMethod) Defines whether the generated method overrides a methods declared in a supertype. | |
| setOverrideMethod | back to summary |
|---|---|
| public void setOverrideMethod(boolean[] overrideMethod) Same as | |
| setParameters | back to summary |
|---|---|
| public void setParameters(String[] parameterNames, Class<?>[] parameterTypes) Defines the names and types of the parameters of the generated method.
names
The parameters can be of primitive type, e.g. The default is to have zero parameters. | |
| setParameters | back to summary |
|---|---|
| public void setParameters(String[][] parameterNames, Class<?>[][] parameterTypes) Same as | |
| setParentClassLoader | back to summary | ||||||
|---|---|---|---|---|---|---|---|
| public void setParentClassLoader(ClassLoader parentClassLoader) The "parent class loader" is used to load referenced classes. Useful values are:
The parent class loader defaults to the current thread's context class loader. | |||||||
| setReturnType | back to summary |
|---|---|
| public void setReturnType(Class<?> returnType)
Deprecated Use
| |
| setStaticMethod | back to summary |
|---|---|
| public void setStaticMethod(boolean staticMethod) Defines whether the generated method should be STATIC or not. Defaults to | |
| setStaticMethod | back to summary |
|---|---|
| public void setStaticMethod(boolean[] staticMethod) Same as | |
| setThrownExceptions | back to summary |
|---|---|
| public void setThrownExceptions(Class<?>[] thrownExceptions) Defines the exceptions that the generated method may throw. | |
| setThrownExceptions | back to summary |
|---|---|
| public void setThrownExceptions(Class<?>[][] thrownExceptions) Same as | |
| setWarningHandler | back to summary |
|---|---|
| public void setWarningHandler(WarningHandler warningHandler) By default, warnings are discarded, but an application my install a custom
| |