Class ExpressionEvaluator
- java.lang.Object
-
- org.codehaus.commons.compiler.Cookable
-
- org.codehaus.commons.compiler.MultiCookable
-
- org.codehaus.commons.compiler.jdk.ExpressionEvaluator
-
- All Implemented Interfaces:
ICookable,IExpressionEvaluator,IMultiCookable
public class ExpressionEvaluator extends MultiCookable 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
createFastEvaluator(String, Class, String[]). Expression evaluation is faster than throughevaluate(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 (How can it be that interface method invocation is slower than reflection for the server JVM?)
-
-
Field Summary
-
Fields inherited from interface org.codehaus.commons.compiler.IExpressionEvaluator
ANY_TYPE, DEFAULT_CLASS_NAME, DEFAULT_EXPRESSION_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<?> extendedType, java.lang.Class<?>[] implementedTypes, java.lang.ClassLoader parentClassLoader)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 parentClassLoader)Equivalent to
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcook(java.lang.String[] fileNames, java.io.Reader[] readers)Same asCookable.cook(String, Reader), but cooks a set of documents into one class.voidcook(java.lang.String fileName, java.io.Reader reader)Reads, scans, parses and compiles Java tokens from the givenReader.<T> TcreateFastEvaluator(java.io.Reader reader, java.lang.Class<? extends T> interfaceToImplement, java.lang.String... parameterNames)<T> TcreateFastEvaluator(java.lang.String expression, java.lang.Class<? extends T> interfaceToImplement, java.lang.String... parameterNames)If the parameter and return types of the expression are known at compile time, then a "fast" expression evaluator can be instantiated throughIExpressionEvaluator.createFastEvaluator(String, Class, String[]).java.lang.Objectevaluate()Shorthand forevaluate(new Object[0]).java.lang.Objectevaluate(int idx, java.lang.Object[] arguments)Same asIExpressionEvaluator.evaluate(Object[]), but for multiple expressions.java.lang.Objectevaluate(java.lang.Object[] arguments)Evaluates the expression with concrete parameter values.java.util.Map<java.lang.String,byte[]>getBytecodes()java.lang.Class<?>getClazz()java.lang.Class<?>getDefaultExpressionType()java.lang.String[]getDefaultImports()java.lang.reflect.MethodgetMethod()java.lang.reflect.MethodgetMethod(int idx)Same asIExpressionEvaluator.getMethod(), but for multiple expressions.java.lang.reflect.Method[]getResult()voidsetClassName(java.lang.String className)Sets the name of the generated class.voidsetCompileErrorHandler(ErrorHandler compileErrorHandler)Installs anErrorHandlerwhich is invoked during compilation on each error.voidsetDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)Determines what kind of debugging information is included in the generates classes.voidsetDefaultExpressionType(java.lang.Class<?> defaultExpressionType)Reconfigures the "default expression type"; if no expression type is configured for an expression, then, when cooking thisIExpressionEvaluator, the "default expression type" is used for the expressionvoidsetDefaultImports(java.lang.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.voidsetExpressionType(java.lang.Class<?> expressionType)Defines the type of the expression.voidsetExpressionTypes(java.lang.Class<?>[] expressionTypes)Configures the types of the expressions.voidsetExtendedClass(java.lang.Class<?> extendedType)Sets a particular superclass that the generated class will extend.voidsetImplementedInterfaces(java.lang.Class<?>[] implementedTypes)Configures the interfaces that the generated class implements.voidsetMethodName(java.lang.String methodName)Defines the name of the generated method.voidsetMethodNames(java.lang.String[] methodNames)Same asIExpressionEvaluator.setMethodName(String), but for multiple expressions.voidsetOverrideMethod(boolean overrideMethod)Defines whether the generated method overrides a methods declared in a supertype.voidsetOverrideMethod(boolean[] overrideMethod)Same asIExpressionEvaluator.setOverrideMethod(boolean), but for multiple expressions.voidsetParameters(java.lang.String[][] parameterNames, java.lang.Class<?>[][] parameterTypes)Same asIExpressionEvaluator.setParameters(String[], Class[]), but for multiple expressions.voidsetParameters(java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes)Defines the names and types of the parameters of the generated method.voidsetParentClassLoader(java.lang.ClassLoader parentClassLoader)The "parent class loader" is used to load referenced classes.voidsetReturnType(java.lang.Class<?> returnType)voidsetSourceVersion(int version)Specifies the version of source code accepted, in analogy with JAVAC's-sourcecommand line option.voidsetStaticMethod(boolean staticMethod)Defines whether the generated method should be STATIC or not.voidsetStaticMethod(boolean[] staticMethod)Same asIExpressionEvaluator.setStaticMethod(boolean), but for multiple expressions.voidsetTargetVersion(int version)Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-targetcommand line option.voidsetThrownExceptions(java.lang.Class<?>[] thrownExceptions)Defines the exceptions that the generated method may throw.voidsetThrownExceptions(java.lang.Class<?>[][] thrownExceptions)Same asIExpressionEvaluator.setThrownExceptions(Class[]), but for multiple expressions.voidsetWarningHandler(WarningHandler warningHandler)By default, warnings are discarded, but an application my install a customWarningHandler.-
Methods inherited from class org.codehaus.commons.compiler.MultiCookable
cook, cook, cook, cook, cook, cook, cook, cookFiles, cookFiles, cookFiles, cookFiles
-
Methods inherited from class org.codehaus.commons.compiler.Cookable
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
-
-
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, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileExceptionEquivalent toExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setParentClassLoader(parentClassLoader); 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, @Nullable java.lang.Class<?> extendedType, java.lang.Class<?>[] implementedTypes, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileExceptionEquivalent toExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setExtendedType(extendedType); ee.setImplementedTypes(implementedTypes); ee.setParentClassLoader(parentClassLoader); 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
-
setParentClassLoader
public void setParentClassLoader(@Nullable java.lang.ClassLoader parentClassLoader)
Description copied from interface:IExpressionEvaluatorThe "parent class loader" is used to load referenced classes. Useful values are:System.getSystemClassLoader()The running JVM's class path Thread.currentThread().getContextClassLoader()ornullThe class loader effective for the invoking thread ClassLoaders.BOOTCLASSPATH_CLASS_LOADERThe running JVM's boot class path The parent class loader defaults to the current thread's context class loader.
- Specified by:
setParentClassLoaderin interfaceIExpressionEvaluator
-
setDebuggingInformation
public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)Description copied from interface:IExpressionEvaluatorDetermines what kind of debugging information is included in the generates classes. The default is typically "-g:none".- Specified by:
setDebuggingInformationin interfaceIExpressionEvaluator
-
setSourceVersion
public void setSourceVersion(int version)
Description copied from interface:ICookableSpecifies the version of source code accepted, in analogy with JAVAC's-sourcecommand line option. May be ignored by an implementation (e.g. thejaninoimplementation always accepts the language features as described on the home page). Allowed values, and the default value, depend on the implementation.-1means to use a default version.- Specified by:
setSourceVersionin interfaceICookable
-
setTargetVersion
public void setTargetVersion(int version)
Description copied from interface:ICookableGenerates class files that target a specified release of the virtual machine, in analogy with JAVAC's-targetcommand line option. Allowed values depend on the implementation. The default value also depends on the implementation. The only invariant is that the generated class files are suitable for the currently executing JVM.-1means to use a default version.- Specified by:
setTargetVersionin interfaceICookable
-
setCompileErrorHandler
public void setCompileErrorHandler(@Nullable ErrorHandler compileErrorHandler)
Description copied from interface:IExpressionEvaluatorInstalls anErrorHandlerwhich is invoked during compilation on each error. (By default, the compilation throws aCompileExceptionon the first error and terminates.)If the given
ErrorHandlerthrows aCompileException, then the compilation terminates and the exception is propagated.If the given
ErrorHandlerdoes not throw aCompileExceptionbut completes normally, then the compilation may or may not continue, depending on the error. Iff the compilation completes normally but errors were reported, then it will throw aCompileExceptionindicating the number of errors.In other words: The
ErrorHandlermay throw aCompileExceptionor not, but the compilation will definitely throw aCompileExceptionif one or more compile errors have occurred.- Specified by:
setCompileErrorHandlerin interfaceIExpressionEvaluator- Parameters:
compileErrorHandler-nullto restore the default behavior (throwing aCompileException)
-
setWarningHandler
public void setWarningHandler(@Nullable WarningHandler warningHandler)
Description copied from interface:IExpressionEvaluatorBy default, warnings are discarded, but an application my install a customWarningHandler.- Specified by:
setWarningHandlerin interfaceIExpressionEvaluator- Parameters:
warningHandler-nullto indicate that no warnings be issued
-
setDefaultImports
public void setDefaultImports(java.lang.String... defaultImports)
Description copied from interface:IExpressionEvaluator"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 );- Specified by:
setDefaultImportsin interfaceIExpressionEvaluator
-
getDefaultImports
public java.lang.String[] getDefaultImports()
- Specified by:
getDefaultImportsin interfaceIExpressionEvaluator- Returns:
- The default imports that were previously configured with
IExpressionEvaluator.setDefaultImports(String...)
-
setDefaultExpressionType
public void setDefaultExpressionType(java.lang.Class<?> defaultExpressionType)
Description copied from interface:IExpressionEvaluatorReconfigures the "default expression type"; if no expression type is configured for an expression, then, when cooking thisIExpressionEvaluator, the "default expression type" is used for the expression- Specified by:
setDefaultExpressionTypein interfaceIExpressionEvaluator
-
getDefaultExpressionType
public java.lang.Class<?> getDefaultExpressionType()
- Specified by:
getDefaultExpressionTypein interfaceIExpressionEvaluator- Returns:
- The currently configured "default expression type"
- See Also:
IExpressionEvaluator.setDefaultExpressionType(Class)
-
setImplementedInterfaces
public void setImplementedInterfaces(java.lang.Class<?>[] implementedTypes)
Description copied from interface:IExpressionEvaluatorConfigures the interfaces that the generated class implements.- Specified by:
setImplementedInterfacesin interfaceIExpressionEvaluator
-
setReturnType
public void setReturnType(java.lang.Class<?> returnType)
- Specified by:
setReturnTypein interfaceIExpressionEvaluator
-
setExpressionType
public void setExpressionType(java.lang.Class<?> expressionType)
Description copied from interface:IExpressionEvaluatorDefines the type of the expression.Defaults to
Object.class, which, thanks to autoboxing, allows for any expression type (including primitive types).If
expressionTypeisvoid.class, then the expression must be an invocation of avoidmethod.- Specified by:
setExpressionTypein interfaceIExpressionEvaluator
-
setExpressionTypes
public void setExpressionTypes(java.lang.Class<?>[] expressionTypes)
Description copied from interface:IExpressionEvaluatorConfigures the types of the expressions.Unless this method is called, all expressions have type
Object.class.If any expression has type
void.class, then it must be an invocation of avoidmethod.- Specified by:
setExpressionTypesin interfaceIExpressionEvaluator
-
setOverrideMethod
public void setOverrideMethod(boolean overrideMethod)
Description copied from interface:IExpressionEvaluatorDefines whether the generated method overrides a methods declared in a supertype.- Specified by:
setOverrideMethodin interfaceIExpressionEvaluator
-
setOverrideMethod
public void setOverrideMethod(boolean[] overrideMethod)
Description copied from interface:IExpressionEvaluatorSame asIExpressionEvaluator.setOverrideMethod(boolean), but for multiple expressions.- Specified by:
setOverrideMethodin interfaceIExpressionEvaluator
-
setParameters
public void setParameters(java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes)Description copied from interface:IExpressionEvaluatorDefines the names and types of the parameters of the generated method.names
.lengthand types.lengthmust be equal. This invariant may be checked immediately, or later when the expression is cooked.The parameters can be of primitive type, e.g.
double.class.The default is to have zero parameters.
- Specified by:
setParametersin interfaceIExpressionEvaluator
-
setParameters
public void setParameters(java.lang.String[][] parameterNames, java.lang.Class<?>[][] parameterTypes)Description copied from interface:IExpressionEvaluatorSame asIExpressionEvaluator.setParameters(String[], Class[]), but for multiple expressions.- Specified by:
setParametersin interfaceIExpressionEvaluator
-
setClassName
public void setClassName(java.lang.String className)
Description copied from interface:IExpressionEvaluatorSets the name of the generated class. Defaults toIExpressionEvaluator.DEFAULT_CLASS_NAME. In most cases, there is no need to set this name, because the generated class is loaded into its ownClassLoaderwhere its name cannot collide with classes generated by other evaluators.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.
- Specified by:
setClassNamein interfaceIExpressionEvaluator
-
setExtendedClass
public void setExtendedClass(@Nullable java.lang.Class<?> extendedType)
Description copied from interface:IExpressionEvaluatorSets a particular superclass that the generated class will extend. If extendedClass isnull, the generated class will extendObject.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.
- Specified by:
setExtendedClassin interfaceIExpressionEvaluator
-
setStaticMethod
public void setStaticMethod(boolean staticMethod)
Description copied from interface:IExpressionEvaluatorDefines whether the generated method should be STATIC or not. Defaults totrue.- Specified by:
setStaticMethodin interfaceIExpressionEvaluator
-
setStaticMethod
public void setStaticMethod(boolean[] staticMethod)
Description copied from interface:IExpressionEvaluatorSame asIExpressionEvaluator.setStaticMethod(boolean), but for multiple expressions.- Specified by:
setStaticMethodin interfaceIExpressionEvaluator
-
setMethodName
public void setMethodName(java.lang.String methodName)
Description copied from interface:IExpressionEvaluatorDefines the name of the generated method. Initially, the method name is "eval*".- Specified by:
setMethodNamein interfaceIExpressionEvaluator- Parameters:
methodName-nullmeans reset to default name- See Also:
IScriptEvaluator.DEFAULT_METHOD_NAME
-
setMethodNames
public void setMethodNames(java.lang.String[] methodNames)
Description copied from interface:IExpressionEvaluatorSame asIExpressionEvaluator.setMethodName(String), but for multiple expressions.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
IExpressionEvaluator.setParameters(String[][], Class[][])).- Specified by:
setMethodNamesin interfaceIExpressionEvaluator
-
setThrownExceptions
public void setThrownExceptions(java.lang.Class<?>[] thrownExceptions)
Description copied from interface:IExpressionEvaluatorDefines the exceptions that the generated method may throw.- Specified by:
setThrownExceptionsin interfaceIExpressionEvaluator
-
setThrownExceptions
public void setThrownExceptions(java.lang.Class<?>[][] thrownExceptions)
Description copied from interface:IExpressionEvaluatorSame asIExpressionEvaluator.setThrownExceptions(Class[]), but for multiple expressions.- Specified by:
setThrownExceptionsin interfaceIExpressionEvaluator
-
cook
public void cook(@Nullable java.lang.String fileName, java.io.Reader reader) throws CompileException, java.io.IOException
Description copied from interface:ICookableReads, scans, parses and compiles Java tokens from the givenReader.- Specified by:
cookin interfaceICookable- Specified by:
cookin classCookable- Parameters:
fileName- Used when reporting errors and warnings- Throws:
CompileExceptionjava.io.IOException
-
cook
public void cook(java.lang.String[] fileNames, java.io.Reader[] readers) throws CompileException, java.io.IOExceptionDescription copied from interface:IMultiCookableSame asCookable.cook(String, Reader), but cooks a set of documents into one class. Notice that if any of the documents causes trouble, the entire compilation will fail. If you need to report which of the documents causes the exception, you may want to use thefileNamesparameter to distinguish between the individual token sources.- Specified by:
cookin interfaceIMultiCookable- Specified by:
cookin classMultiCookable- Throws:
CompileExceptionjava.io.IOException
-
evaluate
@Nullable public java.lang.Object evaluate() throws java.lang.reflect.InvocationTargetException
Description copied from interface:IExpressionEvaluatorShorthand forevaluate(new Object[0]).- Specified by:
evaluatein interfaceIExpressionEvaluator- Throws:
java.lang.reflect.InvocationTargetException
-
evaluate
@Nullable public java.lang.Object evaluate(@Nullable java.lang.Object[] arguments) throws java.lang.reflect.InvocationTargetException
Description copied from interface:IExpressionEvaluatorEvaluates the expression with concrete parameter values.Each argument value must have the same type as specified through the "parameterTypes" parameter of
IExpressionEvaluator.setParameters(String[], Class[]).Arguments of primitive type must passed with their wrapper class objects.
The object returned has the class as specified through
IExpressionEvaluator.setExpressionType(Class).This method is thread-safe.
Nullarguments is equivalent withnew Object[0].Notice: In version 3.1.8, the arguments parameter was changed from
Object[]toObject..., which turned out to be a really bad decision because it caused a very ugly invocation ambiguity withIExpressionEvaluator.evaluate(int, Object[]). Thus, with version 3.1.10, the parameter was changed back toObject[].- Specified by:
evaluatein interfaceIExpressionEvaluator- Parameters:
arguments- The actual parameter values- Throws:
java.lang.reflect.InvocationTargetException
-
evaluate
@Nullable public java.lang.Object evaluate(int idx, @Nullable java.lang.Object[] arguments) throws java.lang.reflect.InvocationTargetException
Description copied from interface:IExpressionEvaluatorSame asIExpressionEvaluator.evaluate(Object[]), but for multiple expressions.Notice: In version 3.1.8, the arguments parameter was changed from
Object[]toObject..., which turned out to be a really bad decision because it caused a very ugly invocation ambiguity withIExpressionEvaluator.evaluate(int, Object[]). Thus, with version 3.1.10, the parameter was changed back toObject[].- Specified by:
evaluatein interfaceIExpressionEvaluator- Throws:
java.lang.reflect.InvocationTargetException
-
getMethod
public java.lang.reflect.Method getMethod()
- Specified by:
getMethodin interfaceIExpressionEvaluator- Returns:
- The generated and loaded
Method
-
getMethod
public java.lang.reflect.Method getMethod(int idx)
Description copied from interface:IExpressionEvaluatorSame asIExpressionEvaluator.getMethod(), but for multiple expressions.- Specified by:
getMethodin interfaceIExpressionEvaluator
-
getClazz
public java.lang.Class<?> getClazz()
- Specified by:
getClazzin interfaceIExpressionEvaluator- Returns:
- The generated
Class
-
getResult
public java.lang.reflect.Method[] getResult()
- Specified by:
getResultin interfaceIExpressionEvaluator- Returns:
- The generated and loaded methods that implement the cooked expressions
-
getBytecodes
public java.util.Map<java.lang.String,byte[]> getBytecodes()
- Specified by:
getBytecodesin interfaceICookable- Returns:
- The generated Java bytecode; maps class name to bytes
-
createFastEvaluator
public <T> T createFastEvaluator(java.lang.String expression, java.lang.Class<? extends T> interfaceToImplement, java.lang.String... parameterNames) throws CompileExceptionDescription copied from interface:IExpressionEvaluatorIf the parameter and return types of the expression are known at compile time, then a "fast" expression evaluator can be instantiated throughIExpressionEvaluator.createFastEvaluator(String, Class, String[]). Expression evaluation is faster than throughIExpressionEvaluator.evaluate(Object[]), because it is not done through reflection but through direct method invocation.Example:
public interface Foo { int bar(int a, int b); } ... ExpressionEvaluator ee = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator(); // Optionally configure the EE here... ee.setClassName("Bar"); ee.setDefaultImports(new String[] { "java.util.*" }); ee.setExtendedClass(SomeOtherClass.class); ee.setParentClassLoader(someClassLoader); // Optionally configure the EE here... Foo f = (Foo) ee.createFastEvaluator( "a + b", // expression to evaluate Foo.class, // interface that describes the expression's signature new String[] { "a", "b" } // the parameters' names ); System.out.println("1 + 2 = " + f.bar(1, 2)); // Evaluate the expressionAll other configuration (implemented type, static method, return type, method name, parameter names and types, thrown exceptions) are predetermined by the interfaceToImplement.
Notice: The
interfaceToImplementmust be accessible by the compiled class, i.e. either be declaredpublic, or withprotectedor default access in the package of the compiled class (seeIExpressionEvaluator.setClassName(String).- Specified by:
createFastEvaluatorin interfaceIExpressionEvaluator- Throws:
CompileException
-
createFastEvaluator
public <T> T createFastEvaluator(java.io.Reader reader, java.lang.Class<? extends T> interfaceToImplement, java.lang.String... parameterNames) throws CompileException, java.io.IOException- Specified by:
createFastEvaluatorin interfaceIExpressionEvaluator- Throws:
CompileExceptionjava.io.IOException- See Also:
IExpressionEvaluator.createFastEvaluator(String, Class, String[])
-
-