Class ExpressionEvaluator
- java.lang.Object
-
- org.codehaus.commons.compiler.Cookable
-
- org.codehaus.commons.compiler.MultiCookable
-
- org.codehaus.janino.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 expression type and the parameters' types 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
interfaceToImplementmust 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:
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 interface org.codehaus.commons.compiler.IExpressionEvaluator
ANY_TYPE, DEFAULT_CLASS_NAME, DEFAULT_EXPRESSION_TYPE
-
-
Constructor Summary
Constructors Constructor Description ExpressionEvaluator()ExpressionEvaluator(String expression, Class<?> expressionType, String[] parameterNames, Class<?>[] parameterTypes)Equivalent toExpressionEvaluator(String expression, Class<?> expressionType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, Class<?> extendedClass, Class<?>[] implementedTypes, ClassLoader parentClassLoader)Equivalent toExpressionEvaluator(String expression, Class<?> expressionType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, ClassLoader parentClassLoader)Equivalent toExpressionEvaluator(Scanner scanner, String className, Class<?> extendedType, Class<?>[] implementedTypes, boolean staticMethod, Class<?> expressionType, String methodName, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, ClassLoader parentClassLoader)Creates an expression evaluator with the full configurability.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected Java.Type[]classesToTypes(Location location, Class<?>... classes)protected Java.TypeclassToType(Location location, Class<?> clazz)voidcook(String[] fileNames, Reader[] readers)Same asCookable.cook(String, Reader), but cooks a set of documents into one class.voidcook(String fileName, Reader reader)Reads, scans, parses and compiles Java tokens from the givenReader.voidcook(Parser... parsers)voidcook(Scanner scanner)voidcook(Scanner... scanners)Likecook(Scanner), but cooks a set of scripts into one class.<T> TcreateFastEvaluator(Reader reader, Class<? extends T> interfaceToImplement, String... parameterNames)<T> TcreateFastEvaluator(String script, 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 throughIExpressionEvaluator.createFastEvaluator(String, Class, String[]).<T> TcreateFastEvaluator(Scanner scanner, Class<T> interfaceToImplement, String... parameterNames)Notice: This method is not declared inIScriptEvaluator, and is hence only available in this implementation oforg.codehaus.commons.compiler.static ObjectcreateFastExpressionEvaluator(String expression, Class<?> interfaceToImplement, String[] parameterNames, ClassLoader parentClassLoader)Deprecated.UsecreateFastEvaluator(String, Class, String[])instead:static ObjectcreateFastExpressionEvaluator(Scanner scanner, String[] defaultImports, String className, Class<?> extendedType, Class<?> interfaceToImplement, String[] parameterNames, ClassLoader parentClassLoader)Deprecated.UsecreateFastEvaluator(Reader, Class, String[])insteadstatic ObjectcreateFastExpressionEvaluator(Scanner scanner, String className, Class<?> extendedType, Class<?> interfaceToImplement, String[] parameterNames, ClassLoader parentClassLoader)Deprecated.UsecreateFastEvaluator(Reader, Class, String[])insteadObjectevaluate(int idx, Object... arguments)Nullarguments is equivalent withnew Object[0].Objectevaluate(Object... arguments)Evaluates the expression with concrete parameter values.Map<String,byte[]>getBytecodes()Class<?>getClazz()Class<?>getDefaultExpressionType()String[]getDefaultImports()MethodgetMethod()MethodgetMethod(int idx)Method[]getResult()static String[]guessParameterNames(Scanner scanner)Guess the names of the parameters used in the given expression.protected Java.TypeoptionalClassToType(Location location, Class<?> clazz)EnumSet<JaninoOption>options()ExpressionEvaluatoroptions(EnumSet<JaninoOption> options)Sets the options for all future compilations.voidsetClassName(String className)voidsetCompileErrorHandler(ErrorHandler compileErrorHandler)By default,CompileExceptions are thrown on compile errors, but an application my install its ownErrorHandler.voidsetDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)Determines what kind of debugging information is included in the generates classes.voidsetDefaultExpressionType(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(String... defaultImports)voidsetExpressionType(Class<?> expressionType)Defines the type of the expression.voidsetExpressionTypes(Class<?>[] expressionTypes)Configures the types of the expressions.voidsetExtendedClass(Class<?> extendedType)voidsetImplementedInterfaces(Class<?>[] implementedTypes)Configures the interfaces that the generated class implements.voidsetMethodName(String methodName)voidsetMethodNames(String[] methodNames)voidsetOverrideMethod(boolean overrideMethod)voidsetOverrideMethod(boolean[] overrideMethod)voidsetParameters(String[][] parameterNames, Class<?>[][] parameterTypes)voidsetParameters(String[] parameterNames, Class<?>[] parameterTypes)voidsetParentClassLoader(ClassLoader parentClassLoader)The "parent class loader" is used to load referenced classes.voidsetReturnType(Class<?> returnType)voidsetSourceVersion(int version)Specifies the version of source code accepted, in analogy with JAVAC's-sourcecommand line option.voidsetStaticMethod(boolean staticMethod)voidsetStaticMethod(boolean[] staticMethod)voidsetTargetVersion(int version)Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-targetcommand line option.voidsetThrownExceptions(Class<?>[] thrownExceptions)voidsetThrownExceptions(Class<?>[][] thrownExceptions)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(String expression, Class<?> expressionType, String[] parameterNames, Class<?>[] parameterTypes) throws CompileException
Equivalent toExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.cook(expression);
-
ExpressionEvaluator
public ExpressionEvaluator(String expression, Class<?> expressionType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, @Nullable ClassLoader parentClassLoader) throws CompileException
Equivalent toExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setParentClassLoader(parentClassLoader); ee.cook(expression);
-
ExpressionEvaluator
public ExpressionEvaluator(String expression, Class<?> expressionType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, Class<?> extendedClass, Class<?>[] implementedTypes, @Nullable ClassLoader parentClassLoader) throws CompileException
Equivalent toExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setExtendedClass(extendedClass); 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(Scanner scanner, String className, @Nullable Class<?> extendedType, Class<?>[] implementedTypes, boolean staticMethod, Class<?> expressionType, String methodName, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
Creates an expression evaluator with the full configurability.Equivalent to:
ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setClassName(className); ee.setExtendedType(extendedType); ee.setImplementedTypes(implementedTypes); ee.setStaticMethod(staticMethod); ee.setExpressionType(expressionType); ee.setMethodName(methodName); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setParentClassLoader(parentClassLoader); ee.cook(scanner);- Throws:
CompileExceptionIOException- See Also:
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[]),ISimpleCompiler.setParentClassLoader(ClassLoader),ICookable.cook(Reader)
-
ExpressionEvaluator
public ExpressionEvaluator()
-
-
Method Detail
-
setParentClassLoader
public void setParentClassLoader(@Nullable 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:IExpressionEvaluatorBy default,CompileExceptions are thrown on compile errors, but an application my install its ownErrorHandler.Be aware that a single problem during compilation often causes a bunch of compile errors, so a good
ErrorHandlercounts errors and throws aCompileExceptionwhen a limit is reached.If the given
ErrorHandlerthrowsCompileExceptions, then the compilation is terminated and the exception is propagated.If the given
ErrorHandlerdoes not throwCompileExceptions, then the compiler may or may not continue compilation, but must eventually throw aCompileException.In other words: The
ErrorHandlermay throw aCompileExceptionor not, but the compiler must 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(String... defaultImports)
- Specified by:
setDefaultImportsin interfaceIExpressionEvaluator- See Also:
IClassBodyEvaluator.setDefaultImports(String...)
-
getDefaultImports
public String[] getDefaultImports()
- Specified by:
getDefaultImportsin interfaceIExpressionEvaluator- See Also:
IClassBodyEvaluator.getDefaultImports()
-
options
public EnumSet<JaninoOption> options()
- Returns:
- A reference to the currently effective compilation options; changes to it take effect immediately
-
options
public ExpressionEvaluator options(EnumSet<JaninoOption> options)
Sets the options for all future compilations.
-
setDefaultExpressionType
public void setDefaultExpressionType(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 Class<?> getDefaultExpressionType()
- Specified by:
getDefaultExpressionTypein interfaceIExpressionEvaluator- Returns:
- The currently configured "default expression type"
- See Also:
IExpressionEvaluator.setDefaultExpressionType(Class)
-
setImplementedInterfaces
public void setImplementedInterfaces(Class<?>[] implementedTypes)
Description copied from interface:IExpressionEvaluatorConfigures the interfaces that the generated class implements.- Specified by:
setImplementedInterfacesin interfaceIExpressionEvaluator
-
setReturnType
public void setReturnType(Class<?> returnType)
- Specified by:
setReturnTypein interfaceIExpressionEvaluator
-
setExpressionType
public void setExpressionType(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(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)
- Specified by:
setOverrideMethodin interfaceIExpressionEvaluator- See Also:
IScriptEvaluator.setOverrideMethod(boolean)
-
setOverrideMethod
public void setOverrideMethod(boolean[] overrideMethod)
- Specified by:
setOverrideMethodin interfaceIExpressionEvaluator- See Also:
IScriptEvaluator.setOverrideMethod(boolean[])
-
setParameters
public void setParameters(String[] parameterNames, Class<?>[] parameterTypes)
- Specified by:
setParametersin interfaceIExpressionEvaluator- See Also:
IScriptEvaluator.setParameters(String[], Class[])
-
setParameters
public void setParameters(String[][] parameterNames, Class<?>[][] parameterTypes)
- Specified by:
setParametersin interfaceIExpressionEvaluator- See Also:
IScriptEvaluator.setParameters(String[][], Class[][])
-
setClassName
public void setClassName(String className)
- Specified by:
setClassNamein interfaceIExpressionEvaluator- See Also:
IClassBodyEvaluator.setClassName(String)
-
setExtendedClass
public void setExtendedClass(@Nullable Class<?> extendedType)
- Specified by:
setExtendedClassin interfaceIExpressionEvaluator- See Also:
IClassBodyEvaluator.setExtendedClass(Class)
-
setStaticMethod
public void setStaticMethod(boolean staticMethod)
- Specified by:
setStaticMethodin interfaceIExpressionEvaluator- See Also:
IScriptEvaluator.setStaticMethod(boolean)
-
setStaticMethod
public void setStaticMethod(boolean[] staticMethod)
- Specified by:
setStaticMethodin interfaceIExpressionEvaluator- See Also:
IScriptEvaluator.setStaticMethod(boolean[])
-
setMethodName
public void setMethodName(String methodName)
- Specified by:
setMethodNamein interfaceIExpressionEvaluator- See Also:
IScriptEvaluator.setMethodName(String)
-
setMethodNames
public void setMethodNames(String[] methodNames)
- Specified by:
setMethodNamesin interfaceIExpressionEvaluator- See Also:
IScriptEvaluator.setMethodNames(String[])
-
setThrownExceptions
public void setThrownExceptions(Class<?>[] thrownExceptions)
- Specified by:
setThrownExceptionsin interfaceIExpressionEvaluator- See Also:
IScriptEvaluator.setThrownExceptions(Class[])
-
setThrownExceptions
public void setThrownExceptions(Class<?>[][] thrownExceptions)
- Specified by:
setThrownExceptionsin interfaceIExpressionEvaluator- See Also:
IScriptEvaluator.setThrownExceptions(Class[][])
-
getMethod
public Method getMethod()
- Specified by:
getMethodin interfaceIExpressionEvaluator- See Also:
IScriptEvaluator.getMethod()
-
getMethod
public Method getMethod(int idx)
- Specified by:
getMethodin interfaceIExpressionEvaluator- See Also:
IScriptEvaluator.getMethod(int)
-
getResult
public Method[] getResult()
- Specified by:
getResultin interfaceIExpressionEvaluator- Returns:
- The generated method
- Throws:
IllegalStateException- TheScriptEvaluatorhas not yet be cooked- See Also:
IScriptEvaluator.getResult()
-
cook
public void cook(@Nullable String fileName, Reader reader) throws CompileException, 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:
CompileExceptionIOException
-
cook
public void cook(String[] fileNames, Reader[] readers) throws CompileException, IOException
Description 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:
CompileExceptionIOException
-
cook
public final void cook(Scanner scanner) throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
cook
public final void cook(Scanner... scanners) throws CompileException, IOException
Likecook(Scanner), 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 thefileNameargument ofScanner(String, Reader)to distinguish between the individual token sources.On a 2 GHz Intel Pentium Core Duo under Windows XP with an IBM 1.4.2 JDK, compiling 10000 expressions "a + b" (integer) takes about 4 seconds and 56 MB of main memory. The generated class file is 639203 bytes large.
The number and the complexity of the scripts is restricted by the Limitations of the Java Virtual Machine, where the most limiting factor is the 64K entries limit of the constant pool. Since every method with a distinct name requires one entry there, you can define at best 32K (very simple) scripts.
If and only if the number of scanners is one, then that single script may contain leading IMPORT directives.
- Throws:
IllegalStateException- Any of the precedingset...()had an array size different from that ofscannersCompileExceptionIOException
-
cook
public final void cook(Parser... parsers) throws CompileException, IOException
- Throws:
CompileExceptionIOException- See Also:
cook(Scanner[])
-
classesToTypes
protected Java.Type[] classesToTypes(Location location, @Nullable Class<?>... classes)
-
optionalClassToType
@Nullable protected Java.Type optionalClassToType(Location location, @Nullable Class<?> clazz)
-
evaluate
@Nullable public Object evaluate(@Nullable Object... arguments) throws 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].- Specified by:
evaluatein interfaceIExpressionEvaluator- Parameters:
arguments- The actual parameter values- Throws:
InvocationTargetException
-
evaluate
@Nullable public Object evaluate(int idx, @Nullable Object... arguments) throws InvocationTargetException
Description copied from interface:IExpressionEvaluatorNullarguments is equivalent withnew Object[0].- Specified by:
evaluatein interfaceIExpressionEvaluator- Throws:
InvocationTargetException
-
getClazz
public Class<?> getClazz()
- Specified by:
getClazzin interfaceIExpressionEvaluator- See Also:
IClassBodyEvaluator.getClazz()
-
getBytecodes
public Map<String,byte[]> getBytecodes()
- Specified by:
getBytecodesin interfaceICookable- Returns:
- The generated Java bytecode; maps class name to bytes
-
createFastExpressionEvaluator
@Deprecated public static Object createFastExpressionEvaluator(String expression, Class<?> interfaceToImplement, String[] parameterNames, @Nullable ClassLoader parentClassLoader) throws CompileException
Deprecated.UsecreateFastEvaluator(String, Class, String[])instead:- Throws:
CompileException
-
createFastExpressionEvaluator
@Deprecated public static Object createFastExpressionEvaluator(Scanner scanner, String className, @Nullable Class<?> extendedType, Class<?> interfaceToImplement, String[] parameterNames, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
Deprecated.UsecreateFastEvaluator(Reader, Class, String[])instead- Throws:
CompileExceptionIOException
-
createFastExpressionEvaluator
@Deprecated public static Object createFastExpressionEvaluator(Scanner scanner, String[] defaultImports, String className, @Nullable Class<?> extendedType, Class<?> interfaceToImplement, String[] parameterNames, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
Deprecated.UsecreateFastEvaluator(Reader, Class, String[])instead- Throws:
CompileExceptionIOException
-
createFastEvaluator
public <T> T createFastEvaluator(Reader reader, Class<? extends T> interfaceToImplement, String... parameterNames) throws CompileException, IOException
- Specified by:
createFastEvaluatorin interfaceIExpressionEvaluator- Throws:
CompileExceptionIOException- See Also:
IExpressionEvaluator.createFastEvaluator(String, Class, String[])
-
createFastEvaluator
public <T> T createFastEvaluator(String script, Class<? extends T> interfaceToImplement, String... parameterNames) throws CompileException
Description 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(Scanner scanner, Class<T> interfaceToImplement, String... parameterNames) throws CompileException, IOException
Notice: This method is not declared inIScriptEvaluator, and is hence only available in this implementation oforg.codehaus.commons.compiler. To be independent from this particular implementation, try to switch tocreateFastEvaluator(Reader, Class, String[]).- Parameters:
scanner- Source of tokens to read- Throws:
CompileExceptionIOException- See Also:
createFastEvaluator(Reader, Class, String[])
-
guessParameterNames
public static String[] guessParameterNames(Scanner scanner) throws CompileException, IOException
Guess the names of the parameters used in the given expression. The strategy is to look at all "ambiguous names" in the expression (e.g. in "a.b.c.d()", the ambiguous name is "a.b.c"), and then at the first components of the ambiguous name.- If any component starts with an upper-case letter, then ambiguous name is assumed to be a type name.
- Otherwise, it is assumed to be a parameter name.
- Throws:
CompileExceptionIOException- See Also:
Scanner(String, Reader)
-
-