Class ExpressionEvaluator
- java.lang.Object
-
- 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
-
- All Implemented Interfaces:
IClassBodyEvaluator,ICookable,IExpressionEvaluator,IScriptEvaluator,ISimpleCompiler
public class ExpressionEvaluator extends ScriptEvaluator implements IExpressionEvaluator
ThisIExpressionEvaluatoris 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
IExpressionEvaluatorinstantly.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 throughScriptEvaluator.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: TheinterfaceToImplementmust either be declaredpublic, 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:
(How can it be that interface method invocation is slower than reflection for the server JVM?)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
optionalMethodNames, optionalOverrideMethod, optionalParameterNames, optionalParameterTypes, optionalReturnTypes, optionalStaticMethod, optionalThrownExceptions
-
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
-
Fields inherited from interface org.codehaus.commons.compiler.IExpressionEvaluator
ANY_TYPE
-
-
Constructor Summary
Constructors Constructor Description ExpressionEvaluator()ExpressionEvaluator(java.lang.String expression, java.lang.Class<?> expressionType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes)Equivalent toExpressionEvaluator(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 toExpressionEvaluator(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
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidcook(java.lang.String[] optionalFileNames, java.io.Reader[] readers)Same asICookable.cook(String, Reader), but cooks a set of scripts into one class.protected java.lang.Class<?>getDefaultReturnType()voidsetExpressionType(java.lang.Class expressionType)Define the type of the expression.voidsetExpressionTypes(java.lang.Class[] expressionTypes)Same asIExpressionEvaluator.setExpressionType(Class), but for multiple expressions.voidsetReturnType(java.lang.Class returnType)Deprecated.voidsetReturnTypes(java.lang.Class[] 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, cook, getClassLoader, setDebuggingInformation, setParentClassLoader, setParentClassLoader
-
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, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
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
-
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
-
-
-
-
Constructor Detail
-
ExpressionEvaluator
public ExpressionEvaluator(java.lang.String expression, java.lang.Class<?> expressionType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes) throws CompileExceptionEquivalent toExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.cook(expression);
-
ExpressionEvaluator
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 CompileExceptionEquivalent toExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setParentClassLoader(optionalParentClassLoader); ee.cook(expression);
-
ExpressionEvaluator
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 CompileExceptionEquivalent toExpressionEvaluator 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);
- Throws:
CompileException- See Also:
ExpressionEvaluator(),setExpressionType(Class),ScriptEvaluator.setParameters(String[], Class[]),ScriptEvaluator.setThrownExceptions(Class[]),ClassBodyEvaluator.setExtendedClass(Class),ClassBodyEvaluator.setImplementedInterfaces(Class[]),SimpleCompiler.setParentClassLoader(ClassLoader),Cookable.cook(String)
-
ExpressionEvaluator
public ExpressionEvaluator()
-
-
Method Detail
-
setExpressionType
public void setExpressionType(java.lang.Class expressionType)
Description copied from interface:IExpressionEvaluatorDefine the type of the expression. The special typeIExpressionEvaluator.ANY_TYPEallows the expression to return any type (primitive or reference).If
expressionTypeisVoid.TYPE, then the expression must be an invocation of avoidmethod.Defaults to
IExpressionEvaluator.ANY_TYPE.- Specified by:
setExpressionTypein interfaceIExpressionEvaluator
-
setExpressionTypes
public void setExpressionTypes(java.lang.Class[] expressionTypes)
Description copied from interface:IExpressionEvaluatorSame asIExpressionEvaluator.setExpressionType(Class), but for multiple expressions.- Specified by:
setExpressionTypesin interfaceIExpressionEvaluator
-
setReturnType
@Deprecated public final void setReturnType(java.lang.Class returnType)
Deprecated.Description copied from interface:IScriptEvaluatorDefines the return type of the generated method. The meaning of anullvalue is implementation-dependent.- Specified by:
setReturnTypein interfaceIExpressionEvaluator- Specified by:
setReturnTypein interfaceIScriptEvaluator- Overrides:
setReturnTypein classScriptEvaluator
-
setReturnTypes
@Deprecated public final void setReturnTypes(java.lang.Class[] returnTypes)
Deprecated.Description copied from interface:IScriptEvaluatorDefines the return types of the generated methods. The meaning ofnullvalues is implementation-dependent.- Specified by:
setReturnTypesin interfaceIExpressionEvaluator- Specified by:
setReturnTypesin interfaceIScriptEvaluator- Overrides:
setReturnTypesin classScriptEvaluator
-
getDefaultReturnType
protected java.lang.Class<?> getDefaultReturnType()
- Overrides:
getDefaultReturnTypein classScriptEvaluator
-
cook
public void cook(java.lang.String[] optionalFileNames, java.io.Reader[] readers) throws CompileException, java.io.IOExceptionDescription copied from interface:IScriptEvaluatorSame asICookable.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 theoptionalFileNamesparameter 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.
- Specified by:
cookin interfaceIScriptEvaluator- Overrides:
cookin classScriptEvaluator- Throws:
CompileExceptionjava.io.IOException
-
-