|
janino.net | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
org.codehaus.commons.compiler.Cookable org.codehaus.commons.compiler.jdk.SimpleCompiler org.codehaus.commons.compiler.jdk.ClassBodyEvaluator org.codehaus.commons.compiler.jdk.ScriptEvaluator org.codehaus.commons.compiler.jdk.ExpressionEvaluator
public class ExpressionEvaluator
This IExpressionEvaluator
is implemented by creating and compiling a temporary compilation unit defining one
class with one static method with one RETURN statement.
A number of "convenience constructors" exist that execute the set-up steps described for IExpressionEvaluator
instantly.
If the parameter and return types of the expression are known at compile time, then a "fast" expression evaluator can
be instantiated through ScriptEvaluator.createFastEvaluator(String, Class, String[])
. Expression evaluation is faster than
through ScriptEvaluator.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) ExpressionEvaluator.createFastExpressionEvaluator( "a + b", // expression to evaluate Foo.class, // interface that describes the expression's signature new String[] { "a", "b" }, // the parameters' names (ClassLoader) null // Use current thread's context class loader ); System.out.println("1 + 2 = " + f.bar(1, 2)); // Evaluate the expressionNotice: The
interfaceToImplement
must either be declared public
,
or with package scope in the root package (i.e. "no" package).
On my system (Intel P4, 2 GHz, MS Windows XP, JDK 1.4.1), expression "x + 1" evaluates as follows:
Server JVM | Client JVM | |
---|---|---|
Normal EE | 23.7 ns | 64.0 ns |
Fast EE | 31.2 ns | 42.2 ns |
Field Summary |
---|
Fields inherited from class org.codehaus.commons.compiler.jdk.ScriptEvaluator |
---|
optionalOverrideMethod, optionalStaticMethod |
Fields inherited from interface org.codehaus.commons.compiler.IExpressionEvaluator |
---|
ANY_TYPE |
Fields inherited from interface org.codehaus.commons.compiler.IClassBodyEvaluator |
---|
DEFAULT_CLASS_NAME |
Fields inherited from interface org.codehaus.commons.compiler.ICookable |
---|
BOOT_CLASS_LOADER, SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR, SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE |
Constructor Summary | |
---|---|
ExpressionEvaluator()
|
|
ExpressionEvaluator( expression,
<?> expressionType,
[] parameterNames,
<?>[] parameterTypes)
Equivalent to ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.cook(expression); |
|
ExpressionEvaluator( expression,
<?> expressionType,
[] parameterNames,
<?>[] parameterTypes,
<?>[] thrownExceptions,
<?> optionalExtendedType,
<?>[] implementedTypes,
optionalParentClassLoader)
Equivalent to ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setExtendedType(optionalExtendedType); ee.setImplementedTypes(implementedTypes); ee.setParentClassLoader(optionalParentClassLoader); ee.cook(expression); |
|
ExpressionEvaluator( expression,
<?> expressionType,
[] parameterNames,
<?>[] parameterTypes,
<?>[] thrownExceptions,
optionalParentClassLoader)
Equivalent to ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setParentClassLoader(optionalParentClassLoader); ee.cook(expression); |
Method Summary | |
---|---|
void |
cook([] optionalFileNames,
[] readers)
Same as ICookable.cook(String, Reader) , but cooks a set of scripts into one class. |
protected <?> |
getDefaultReturnType()
The default return type of an expression is Object . |
void |
setExpressionType( expressionType)
Define the type of the expression. |
void |
setExpressionTypes([] expressionTypes)
Same as IExpressionEvaluator.setExpressionType(Class) , but for multiple expressions. |
void |
setReturnType( returnType)
Deprecated. |
void |
setReturnTypes([] returnTypes)
Deprecated. |
Methods inherited from class org.codehaus.commons.compiler.jdk.ScriptEvaluator |
---|
cook, cook, cook, cook, cook, createFastEvaluator, createFastEvaluator, createInstance, evaluate, evaluate, getMethod, getMethod, setMethodName, setMethodNames, setOverrideMethod, setOverrideMethod, setParameters, setParameters, setStaticMethod, setStaticMethod, setThrownExceptions, setThrownExceptions |
Methods inherited from class org.codehaus.commons.compiler.jdk.ClassBodyEvaluator |
---|
cook, getClazz, parseImportDeclarations, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypes |
Methods inherited from class org.codehaus.commons.compiler.jdk.SimpleCompiler |
---|
assertCooked, assertNotCooked, getClassLoader, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setParentClassLoader, setWarningHandler |
Methods inherited from class org.codehaus.commons.compiler.Cookable |
---|
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, readString |
Methods inherited from class java.lang. |
---|
, , , , , , , , , , |
Methods inherited from interface org.codehaus.commons.compiler.IExpressionEvaluator |
---|
createFastEvaluator, createFastEvaluator, evaluate |
Methods inherited from interface org.codehaus.commons.compiler.IScriptEvaluator |
---|
cook, cook, cook, evaluate, getMethod, getMethod, setMethodName, setMethodNames, setOverrideMethod, setOverrideMethod, setParameters, setParameters, setStaticMethod, setStaticMethod, setThrownExceptions, setThrownExceptions |
Methods inherited from interface org.codehaus.commons.compiler.IClassBodyEvaluator |
---|
createInstance, getClazz, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypes |
Methods inherited from interface org.codehaus.commons.compiler.ICookable |
---|
cook, cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setWarningHandler |
Constructor Detail |
---|
public ExpressionEvaluator( expression, <?> expressionType, [] parameterNames, <?>[] parameterTypes) throws CompileException
ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.cook(expression);
CompileException
ExpressionEvaluator()
,
setExpressionType(Class)
,
ScriptEvaluator.setParameters(String[], Class[])
,
Cookable.cook(String)
public ExpressionEvaluator( expression, <?> expressionType, [] parameterNames, <?>[] parameterTypes, <?>[] thrownExceptions, optionalParentClassLoader) throws CompileException
ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setParentClassLoader(optionalParentClassLoader); ee.cook(expression);
CompileException
ExpressionEvaluator()
,
setExpressionType(Class)
,
ScriptEvaluator.setParameters(String[], Class[])
,
ScriptEvaluator.setThrownExceptions(Class[])
,
SimpleCompiler.setParentClassLoader(ClassLoader)
,
Cookable.cook(String)
public ExpressionEvaluator( expression, <?> expressionType, [] parameterNames, <?>[] parameterTypes, <?>[] thrownExceptions, <?> optionalExtendedType, <?>[] implementedTypes, optionalParentClassLoader) throws CompileException
ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setExtendedType(optionalExtendedType); ee.setImplementedTypes(implementedTypes); ee.setParentClassLoader(optionalParentClassLoader); ee.cook(expression);
CompileException
ExpressionEvaluator()
,
setExpressionType(Class)
,
ScriptEvaluator.setParameters(String[], Class[])
,
ScriptEvaluator.setThrownExceptions(Class[])
,
ClassBodyEvaluator.setExtendedClass(Class)
,
ClassBodyEvaluator.setImplementedInterfaces(Class[])
,
SimpleCompiler.setParentClassLoader(ClassLoader)
,
Cookable.cook(String)
public ExpressionEvaluator()
Method Detail |
---|
public void setExpressionType( expressionType)
IExpressionEvaluator
IExpressionEvaluator.ANY_TYPE
allows the expression
to return any type (primitive or reference).
If expressionType
is , then the expression must be an
invocation of a void
method.
Defaults to IExpressionEvaluator.ANY_TYPE
.
setExpressionType
in interface IExpressionEvaluator
public void setExpressionTypes([] expressionTypes)
IExpressionEvaluator
IExpressionEvaluator.setExpressionType(Class)
, but for multiple expressions.
setExpressionTypes
in interface IExpressionEvaluator
public final void setReturnType( returnType)
IScriptEvaluator
null
value is implementation-dependent.
setReturnType
in interface IExpressionEvaluator
setReturnType
in interface IScriptEvaluator
setReturnType
in class ScriptEvaluator
public final void setReturnTypes([] returnTypes)
IScriptEvaluator
null
elements is
implementation-dependent.
setReturnTypes
in interface IExpressionEvaluator
setReturnTypes
in interface IScriptEvaluator
setReturnTypes
in class ScriptEvaluator
protected <?> getDefaultReturnType()
Object
.
getDefaultReturnType
in class ScriptEvaluator
public void cook([] optionalFileNames, [] readers) throws CompileException,
IScriptEvaluator
ICookable.cook(String, Reader)
, 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
optionalFileNames
parameter to distinguish between the individual token sources.
If and only if the number of scanners is one, then that single script may contain leading IMPORT directives.
cook
in interface IScriptEvaluator
cook
in class ScriptEvaluator
CompileException
|
janino.net | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |