|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.codehaus.commons.compiler.Cookable
org.codehaus.janino.SimpleCompiler
org.codehaus.janino.ClassBodyEvaluator
org.codehaus.janino.ScriptEvaluator
org.codehaus.janino.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
createFastExpressionEvaluator(String, Class, String[], ClassLoader)
. 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.janino.ScriptEvaluator |
---|
optionalMethodNames, optionalParameterNames, optionalParameterTypes, optionalReturnTypes, optionalStaticMethod, optionalThrownExceptions |
Fields inherited from class org.codehaus.janino.ClassBodyEvaluator |
---|
className, ZERO_CLASSES |
Fields inherited from class org.codehaus.janino.SimpleCompiler |
---|
debugLines, debugSource, debugVars |
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(Scanner scanner,
java.lang.String className,
java.lang.Class optionalExtendedType,
java.lang.Class[] implementedTypes,
boolean staticMethod,
java.lang.Class expressionType,
java.lang.String methodName,
java.lang.String[] parameterNames,
java.lang.Class[] parameterTypes,
java.lang.Class[] thrownExceptions,
java.lang.ClassLoader optionalParentClassLoader)
Equivalent to |
|
ExpressionEvaluator(java.lang.String expression,
java.lang.Class expressionType,
java.lang.String[] parameterNames,
java.lang.Class[] parameterTypes)
Equivalent to |
|
ExpressionEvaluator(java.lang.String expression,
java.lang.Class expressionType,
java.lang.String[] parameterNames,
java.lang.Class[] parameterTypes,
java.lang.Class[] thrownExceptions,
java.lang.Class optionalExtendedType,
java.lang.Class[] implementedTypes,
java.lang.ClassLoader optionalParentClassLoader)
Equivalent to |
|
ExpressionEvaluator(java.lang.String expression,
java.lang.Class expressionType,
java.lang.String[] parameterNames,
java.lang.Class[] parameterTypes,
java.lang.Class[] thrownExceptions,
java.lang.ClassLoader optionalParentClassLoader)
Equivalent to |
Method Summary | |
---|---|
static java.lang.Object |
createFastExpressionEvaluator(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)
Deprecated. |
static java.lang.Object |
createFastExpressionEvaluator(Scanner scanner,
java.lang.String className,
java.lang.Class optionalExtendedType,
java.lang.Class interfaceToImplement,
java.lang.String[] parameterNames,
java.lang.ClassLoader optionalParentClassLoader)
Deprecated. |
static java.lang.Object |
createFastExpressionEvaluator(java.lang.String expression,
java.lang.Class interfaceToImplement,
java.lang.String[] parameterNames,
java.lang.ClassLoader optionalParentClassLoader)
Deprecated. |
protected java.lang.Class |
getDefaultReturnType()
|
static java.lang.String[] |
guessParameterNames(Scanner scanner)
Guess the names of the parameters used in the given expression. |
protected java.util.List |
makeStatements(int idx,
Scanner scanner)
Fill the given block by parsing statements until EOF and adding
them to the block. |
void |
setExpressionType(java.lang.Class expressionType)
Define the type of the expression. |
void |
setExpressionTypes(java.lang.Class[] expressionTypes)
|
Methods inherited from class org.codehaus.janino.ScriptEvaluator |
---|
compileToMethods, cook, cook, cook, cook, cook, cook, createFastEvaluator, createFastEvaluator, createFastEvaluator, createFastScriptEvaluator, createFastScriptEvaluator, createFastScriptEvaluator, createFastScriptEvaluator, createInstance, evaluate, evaluate, getMethod, getMethod, makeMethodDeclaration, setMethodName, setMethodNames, setParameters, setParameters, setReturnType, setReturnTypes, setStaticMethod, setStaticMethod, setThrownExceptions, setThrownExceptions |
Methods inherited from class org.codehaus.janino.ClassBodyEvaluator |
---|
addPackageMemberClassDeclaration, compileToClass, createFastClassBodyEvaluator, createFastClassBodyEvaluator, getClazz, makeCompilationUnit, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypes |
Methods inherited from class org.codehaus.janino.SimpleCompiler |
---|
assertNotCooked, classesToTypes, classToType, compileToClassLoader, cook, cook, equals, getClassLoader, hashCode, main, setDebuggingInformation, setParentClassLoader, setParentClassLoader, setUpClassLoaders |
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.Object |
---|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface org.codehaus.commons.compiler.IExpressionEvaluator |
---|
createFastEvaluator, createFastEvaluator, evaluate |
Methods inherited from interface org.codehaus.commons.compiler.IScriptEvaluator |
---|
cook, cook, cook, cook, evaluate, getMethod, getMethod, setMethodName, setMethodNames, setParameters, setParameters, setReturnType, setReturnTypes, 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, setDebuggingInformation, setParentClassLoader |
Constructor Detail |
---|
public ExpressionEvaluator(java.lang.String expression, java.lang.Class expressionType, java.lang.String[] parameterNames, java.lang.Class[] 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(java.lang.String expression, java.lang.Class expressionType, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes, java.lang.Class[] thrownExceptions, java.lang.ClassLoader 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(java.lang.String expression, java.lang.Class expressionType, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes, java.lang.Class[] thrownExceptions, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, java.lang.ClassLoader 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(Scanner scanner, java.lang.String className, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, boolean staticMethod, java.lang.Class expressionType, java.lang.String methodName, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes, java.lang.Class[] thrownExceptions, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, java.io.IOException
ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setClassName(className); ee.setExtendedType(optionalExtendedType); ee.setImplementedTypes(implementedTypes); ee.setStaticMethod(staticMethod); ee.setExpressionType(expressionType); ee.setMethodName(methodName); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setParentClassLoader(optionalParentClassLoader); ee.cook(scanner);
CompileException
java.io.IOException
IExpressionEvaluator
,
IClassBodyEvaluator.setClassName(String)
,
IClassBodyEvaluator.setExtendedClass(Class)
,
IClassBodyEvaluator.setImplementedInterfaces(Class[])
,
IScriptEvaluator.setStaticMethod(boolean)
,
IExpressionEvaluator.setExpressionType(Class)
,
IScriptEvaluator.setMethodName(String)
,
IScriptEvaluator.setParameters(String[], Class[])
,
IScriptEvaluator.setThrownExceptions(Class[])
,
ICookable.setParentClassLoader(ClassLoader)
,
ICookable.cook(Reader)
public ExpressionEvaluator()
Method Detail |
---|
public void setExpressionType(java.lang.Class expressionType)
IExpressionEvaluator
IExpressionEvaluator.ANY_TYPE
allows the expression
to return any type (primitive or reference).
If expressionType
is Void.TYPE
, then the expression must be an
invocation of a void
method.
Defaults to IExpressionEvaluator.ANY_TYPE
.
setExpressionType
in interface IExpressionEvaluator
public void setExpressionTypes(java.lang.Class[] expressionTypes)
setExpressionTypes
in interface IExpressionEvaluator
protected java.lang.Class getDefaultReturnType()
getDefaultReturnType
in class ScriptEvaluator
protected java.util.List makeStatements(int idx, Scanner scanner) throws CompileException, java.io.IOException
ScriptEvaluator
block
by parsing statements until EOF and adding
them to the block.
makeStatements
in class ScriptEvaluator
CompileException
java.io.IOException
public static java.lang.Object createFastExpressionEvaluator(java.lang.String expression, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader optionalParentClassLoader) throws CompileException
ScriptEvaluator.createFastEvaluator(String, Class, String[])
instead:
IExpressionEvaluator
ee =CompilerFactoryFactory
.getDefaultCompilerFactory
().newExpressionEvaluator
(); ee.setParentClassLoader(optionalParentClassLoader); return ee.createFastEvaluator
(expression, interfaceToImplement, parameterNames);
CompileException
ScriptEvaluator.createFastEvaluator(String, Class, String[])
public static java.lang.Object createFastExpressionEvaluator(Scanner scanner, java.lang.String className, java.lang.Class optionalExtendedType, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, java.io.IOException
IExpressionEvaluator
, and is hence only available in this
implementation of org.codehaus.commons.compiler
. To be independent from this particular
implementation, try to switch to ScriptEvaluator.createFastEvaluator(Reader, Class, String[])
.
CompileException
java.io.IOException
ScriptEvaluator.createFastEvaluator(Reader, Class, String[])
public static java.lang.Object createFastExpressionEvaluator(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, java.io.IOException
IExpressionEvaluator
, and is hence only available in this
implementation of org.codehaus.commons.compiler
. To be independent from this particular
implementation, try to switch to ScriptEvaluator.createFastEvaluator(Reader, Class, String[])
.
CompileException
java.io.IOException
ScriptEvaluator.createFastEvaluator(Reader, Class, String[])
public static java.lang.String[] guessParameterNames(Scanner scanner) throws CompileException, java.io.IOException
CompileException
java.io.IOException
Scanner.Scanner(String, Reader)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |