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
ThisIExpressionEvaluator
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 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 expression
Notice: The
interfaceToImplement
must 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 Modifier and Type Field Description private ScriptEvaluator
se
private int
sourceVersion
private WarningHandler
warningHandler
-
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<?> extendedClass, 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 toExpressionEvaluator(Scanner scanner, java.lang.String className, java.lang.Class<?> extendedType, 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 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, java.lang.Class<?>... classes)
Converts an array ofClass
es into an array ofJava.Type
s.protected Java.Type
classToType(Location location, java.lang.Class<?> clazz)
Wraps a reflectionClass
in aJava.Type
object.void
cook(java.lang.String[] fileNames, java.io.Reader[] readers)
Same asCookable.cook(String, Reader)
, but cooks a set of documents into one class.void
cook(java.lang.String fileName, java.io.Reader reader)
Reads, scans, parses and compiles Java tokens from the givenReader
.void
cook(Parser... parsers)
void
cook(Scanner scanner)
void
cook(Scanner... scanners)
Likecook(Scanner)
, but cooks a set of scripts into one class.<T> T
createFastEvaluator(java.io.Reader reader, java.lang.Class<? extends T> interfaceToImplement, java.lang.String... parameterNames)
<T> T
createFastEvaluator(java.lang.String script, 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[])
.<T> T
createFastEvaluator(Scanner scanner, java.lang.Class<T> interfaceToImplement, java.lang.String... parameterNames)
Notice: This method is not declared inIScriptEvaluator
, and is hence only available in this implementation oforg.codehaus.commons.compiler
.static java.lang.Object
createFastExpressionEvaluator(java.lang.String expression, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader parentClassLoader)
Deprecated.UsecreateFastEvaluator(String, Class, String[])
instead:static java.lang.Object
createFastExpressionEvaluator(Scanner scanner, java.lang.String[] defaultImports, java.lang.String className, java.lang.Class<?> extendedType, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader parentClassLoader)
Deprecated.UsecreateFastEvaluator(Reader, Class, String[])
insteadstatic java.lang.Object
createFastExpressionEvaluator(Scanner scanner, java.lang.String className, java.lang.Class<?> extendedType, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader parentClassLoader)
Deprecated.UsecreateFastEvaluator(Reader, Class, String[])
insteadjava.lang.Object
evaluate(int idx, java.lang.Object... arguments)
Null
arguments is equivalent withnew Object[0]
.java.lang.Object
evaluate(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.Method
getMethod()
java.lang.reflect.Method
getMethod(int idx)
java.lang.reflect.Method[]
getResult()
static java.lang.String[]
guessParameterNames(Scanner scanner)
Guess the names of the parameters used in the given expression.protected Java.Type
optionalClassToType(Location location, java.lang.Class<?> clazz)
Wraps a reflectionClass
in aJava.Type
object.java.util.EnumSet<JaninoOption>
options()
ExpressionEvaluator
options(java.util.EnumSet<JaninoOption> options)
Sets the options for all future compilations.void
setClassName(java.lang.String className)
void
setCompileErrorHandler(ErrorHandler compileErrorHandler)
Installs anErrorHandler
which is invoked during compilation on each error.void
setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
Determines what kind of debugging information is included in the generates classes.void
setDefaultExpressionType(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 expressionvoid
setDefaultImports(java.lang.String... defaultImports)
void
setExpressionType(java.lang.Class<?> expressionType)
Defines the type of the expression.void
setExpressionTypes(java.lang.Class<?>[] expressionTypes)
Configures the types of the expressions.void
setExtendedClass(java.lang.Class<?> extendedType)
void
setImplementedInterfaces(java.lang.Class<?>[] implementedTypes)
Configures the interfaces that the generated class implements.void
setMethodName(java.lang.String methodName)
void
setMethodNames(java.lang.String[] methodNames)
void
setOverrideMethod(boolean overrideMethod)
void
setOverrideMethod(boolean[] overrideMethod)
void
setParameters(java.lang.String[][] parameterNames, java.lang.Class<?>[][] parameterTypes)
void
setParameters(java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes)
void
setParentClassLoader(java.lang.ClassLoader parentClassLoader)
The "parent class loader" is used to load referenced classes.void
setReturnType(java.lang.Class<?> returnType)
void
setSourceVersion(int version)
Specifies the version of source code accepted, in analogy with JAVAC's-source
command line option.void
setStaticMethod(boolean staticMethod)
void
setStaticMethod(boolean[] staticMethod)
void
setTargetVersion(int version)
Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-target
command line option.void
setThrownExceptions(java.lang.Class<?>[] thrownExceptions)
void
setThrownExceptions(java.lang.Class<?>[][] thrownExceptions)
void
setWarningHandler(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
-
-
-
-
Field Detail
-
sourceVersion
private int sourceVersion
-
warningHandler
@Nullable private WarningHandler warningHandler
-
se
private final ScriptEvaluator se
-
-
Constructor Detail
-
ExpressionEvaluator
public ExpressionEvaluator(java.lang.String expression, java.lang.Class<?> expressionType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes) throws CompileException
Equivalent 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 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(java.lang.String expression, java.lang.Class<?> expressionType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, java.lang.Class<?> extendedClass, java.lang.Class<?>[] implementedTypes, @Nullable java.lang.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, java.lang.String className, @Nullable java.lang.Class<?> extendedType, 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, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.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:
CompileException
java.io.IOException
- 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 java.lang.ClassLoader parentClassLoader)
Description copied from interface:IExpressionEvaluator
The "parent class loader" is used to load referenced classes. Useful values are:System.getSystemClassLoader()
The running JVM's class path Thread.currentThread().getContextClassLoader()
ornull
The class loader effective for the invoking thread ClassLoaders.BOOTCLASSPATH_CLASS_LOADER
The running JVM's boot class path The parent class loader defaults to the current thread's context class loader.
- Specified by:
setParentClassLoader
in interfaceIExpressionEvaluator
-
setDebuggingInformation
public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
Description copied from interface:IExpressionEvaluator
Determines what kind of debugging information is included in the generates classes. The default is typically "-g:none
".- Specified by:
setDebuggingInformation
in interfaceIExpressionEvaluator
-
setSourceVersion
public void setSourceVersion(int version)
Description copied from interface:ICookable
Specifies the version of source code accepted, in analogy with JAVAC's-source
command line option. May be ignored by an implementation (e.g. thejanino
implementation always accepts the language features as described on the home page). Allowed values, and the default value, depend on the implementation.-1
means to use a default version.- Specified by:
setSourceVersion
in interfaceICookable
-
setTargetVersion
public void setTargetVersion(int version)
Description copied from interface:ICookable
Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-target
command 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.-1
means to use a default version.- Specified by:
setTargetVersion
in interfaceICookable
-
setCompileErrorHandler
public void setCompileErrorHandler(@Nullable ErrorHandler compileErrorHandler)
Description copied from interface:IExpressionEvaluator
Installs anErrorHandler
which is invoked during compilation on each error. (By default, the compilation throws aCompileException
on the first error and terminates.)If the given
ErrorHandler
throws aCompileException
, then the compilation terminates and the exception is propagated.If the given
ErrorHandler
does not throw aCompileException
but 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 aCompileException
indicating the number of errors.In other words: The
ErrorHandler
may throw aCompileException
or not, but the compilation will definitely throw aCompileException
if one or more compile errors have occurred.- Specified by:
setCompileErrorHandler
in interfaceIExpressionEvaluator
- Parameters:
compileErrorHandler
-null
to restore the default behavior (throwing aCompileException
)
-
setWarningHandler
public void setWarningHandler(@Nullable WarningHandler warningHandler)
Description copied from interface:IExpressionEvaluator
By default, warnings are discarded, but an application my install a customWarningHandler
.- Specified by:
setWarningHandler
in interfaceIExpressionEvaluator
- Parameters:
warningHandler
-null
to indicate that no warnings be issued
-
setDefaultImports
public void setDefaultImports(java.lang.String... defaultImports)
- Specified by:
setDefaultImports
in interfaceIExpressionEvaluator
- See Also:
IClassBodyEvaluator.setDefaultImports(String...)
-
getDefaultImports
public java.lang.String[] getDefaultImports()
- Specified by:
getDefaultImports
in interfaceIExpressionEvaluator
- See Also:
IClassBodyEvaluator.getDefaultImports()
-
options
public java.util.EnumSet<JaninoOption> options()
- Returns:
- A reference to the currently effective compilation options; changes to it take effect immediately
-
options
public ExpressionEvaluator options(java.util.EnumSet<JaninoOption> options)
Sets the options for all future compilations.
-
setDefaultExpressionType
public void setDefaultExpressionType(java.lang.Class<?> defaultExpressionType)
Description copied from interface:IExpressionEvaluator
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 expression- Specified by:
setDefaultExpressionType
in interfaceIExpressionEvaluator
-
getDefaultExpressionType
public java.lang.Class<?> getDefaultExpressionType()
- Specified by:
getDefaultExpressionType
in 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:IExpressionEvaluator
Configures the interfaces that the generated class implements.- Specified by:
setImplementedInterfaces
in interfaceIExpressionEvaluator
-
setReturnType
public void setReturnType(java.lang.Class<?> returnType)
- Specified by:
setReturnType
in interfaceIExpressionEvaluator
-
setExpressionType
public void setExpressionType(java.lang.Class<?> expressionType)
Description copied from interface:IExpressionEvaluator
Defines the type of the expression.Defaults to
Object
.class
, which, thanks to autoboxing, allows for any expression type (including primitive types).If
expressionType
isvoid.class
, then the expression must be an invocation of avoid
method.- Specified by:
setExpressionType
in interfaceIExpressionEvaluator
-
setExpressionTypes
public void setExpressionTypes(java.lang.Class<?>[] expressionTypes)
Description copied from interface:IExpressionEvaluator
Configures 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 avoid
method.- Specified by:
setExpressionTypes
in interfaceIExpressionEvaluator
-
setOverrideMethod
public void setOverrideMethod(boolean overrideMethod)
- Specified by:
setOverrideMethod
in interfaceIExpressionEvaluator
- See Also:
IScriptEvaluator.setOverrideMethod(boolean)
-
setOverrideMethod
public void setOverrideMethod(boolean[] overrideMethod)
- Specified by:
setOverrideMethod
in interfaceIExpressionEvaluator
- See Also:
IScriptEvaluator.setOverrideMethod(boolean[])
-
setParameters
public void setParameters(java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes)
- Specified by:
setParameters
in interfaceIExpressionEvaluator
- See Also:
IScriptEvaluator.setParameters(String[], Class[])
-
setParameters
public void setParameters(java.lang.String[][] parameterNames, java.lang.Class<?>[][] parameterTypes)
- Specified by:
setParameters
in interfaceIExpressionEvaluator
- See Also:
IScriptEvaluator.setParameters(String[][], Class[][])
-
setClassName
public void setClassName(java.lang.String className)
- Specified by:
setClassName
in interfaceIExpressionEvaluator
- See Also:
IClassBodyEvaluator.setClassName(String)
-
setExtendedClass
public void setExtendedClass(@Nullable java.lang.Class<?> extendedType)
- Specified by:
setExtendedClass
in interfaceIExpressionEvaluator
- See Also:
IClassBodyEvaluator.setExtendedClass(Class)
-
setStaticMethod
public void setStaticMethod(boolean staticMethod)
- Specified by:
setStaticMethod
in interfaceIExpressionEvaluator
- See Also:
IScriptEvaluator.setStaticMethod(boolean)
-
setStaticMethod
public void setStaticMethod(boolean[] staticMethod)
- Specified by:
setStaticMethod
in interfaceIExpressionEvaluator
- See Also:
IScriptEvaluator.setStaticMethod(boolean[])
-
setMethodName
public void setMethodName(java.lang.String methodName)
- Specified by:
setMethodName
in interfaceIExpressionEvaluator
- See Also:
IScriptEvaluator.setMethodName(String)
-
setMethodNames
public void setMethodNames(java.lang.String[] methodNames)
- Specified by:
setMethodNames
in interfaceIExpressionEvaluator
- See Also:
IScriptEvaluator.setMethodNames(String[])
-
setThrownExceptions
public void setThrownExceptions(java.lang.Class<?>[] thrownExceptions)
- Specified by:
setThrownExceptions
in interfaceIExpressionEvaluator
- See Also:
IScriptEvaluator.setThrownExceptions(Class[])
-
setThrownExceptions
public void setThrownExceptions(java.lang.Class<?>[][] thrownExceptions)
- Specified by:
setThrownExceptions
in interfaceIExpressionEvaluator
- See Also:
IScriptEvaluator.setThrownExceptions(Class[][])
-
getMethod
public java.lang.reflect.Method getMethod()
- Specified by:
getMethod
in interfaceIExpressionEvaluator
- See Also:
IScriptEvaluator.getMethod()
-
getMethod
public java.lang.reflect.Method getMethod(int idx)
- Specified by:
getMethod
in interfaceIExpressionEvaluator
- See Also:
IScriptEvaluator.getMethod(int)
-
getResult
public java.lang.reflect.Method[] getResult()
- Specified by:
getResult
in interfaceIExpressionEvaluator
- Returns:
- The generated method
- Throws:
java.lang.IllegalStateException
- TheScriptEvaluator
has not yet be cooked- See Also:
IScriptEvaluator.getResult()
-
cook
public void cook(@Nullable java.lang.String fileName, java.io.Reader reader) throws CompileException, java.io.IOException
Description copied from interface:ICookable
Reads, scans, parses and compiles Java tokens from the givenReader
.- Specified by:
cook
in interfaceICookable
- Specified by:
cook
in classCookable
- Parameters:
fileName
- Used when reporting errors and warnings- Throws:
CompileException
java.io.IOException
-
cook
public void cook(java.lang.String[] fileNames, java.io.Reader[] readers) throws CompileException, java.io.IOException
Description copied from interface:IMultiCookable
Same 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 thefileNames
parameter to distinguish between the individual token sources.- Specified by:
cook
in interfaceIMultiCookable
- Specified by:
cook
in classMultiCookable
- Throws:
CompileException
java.io.IOException
-
cook
public final void cook(Scanner scanner) throws CompileException, java.io.IOException
- Throws:
CompileException
java.io.IOException
-
cook
public final void cook(Scanner... scanners) throws CompileException, java.io.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 thefileName
argument 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:
java.lang.IllegalStateException
- Any of the precedingset...()
had an array size different from that ofscanners
CompileException
java.io.IOException
-
cook
public final void cook(Parser... parsers) throws CompileException, java.io.IOException
- Throws:
CompileException
java.io.IOException
- See Also:
cook(Scanner[])
-
classesToTypes
protected Java.Type[] classesToTypes(Location location, @Nullable java.lang.Class<?>... classes)
Converts an array ofClass
es into an array ofJava.Type
s.
-
optionalClassToType
@Nullable protected Java.Type optionalClassToType(Location location, @Nullable java.lang.Class<?> clazz)
Wraps a reflectionClass
in aJava.Type
object.
-
classToType
protected Java.Type classToType(Location location, java.lang.Class<?> clazz)
Wraps a reflectionClass
in aJava.Type
object.
-
evaluate
@Nullable public java.lang.Object evaluate(@Nullable java.lang.Object... arguments) throws java.lang.reflect.InvocationTargetException
Description copied from interface:IExpressionEvaluator
Evaluates 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.
Null
arguments is equivalent withnew Object[0]
.- Specified by:
evaluate
in 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:IExpressionEvaluator
Null
arguments is equivalent withnew Object[0]
.- Specified by:
evaluate
in interfaceIExpressionEvaluator
- Throws:
java.lang.reflect.InvocationTargetException
-
getClazz
public java.lang.Class<?> getClazz()
- Specified by:
getClazz
in interfaceIExpressionEvaluator
- See Also:
IClassBodyEvaluator.getClazz()
-
getBytecodes
public java.util.Map<java.lang.String,byte[]> getBytecodes()
- Specified by:
getBytecodes
in interfaceICookable
- Returns:
- The generated Java bytecode; maps class name to bytes
-
createFastExpressionEvaluator
@Deprecated public static java.lang.Object createFastExpressionEvaluator(java.lang.String expression, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException
Deprecated.UsecreateFastEvaluator(String, Class, String[])
instead:- Throws:
CompileException
-
createFastExpressionEvaluator
@Deprecated public static java.lang.Object createFastExpressionEvaluator(Scanner scanner, java.lang.String className, @Nullable java.lang.Class<?> extendedType, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Deprecated.UsecreateFastEvaluator(Reader, Class, String[])
instead- Throws:
CompileException
java.io.IOException
-
createFastExpressionEvaluator
@Deprecated public static java.lang.Object createFastExpressionEvaluator(Scanner scanner, java.lang.String[] defaultImports, java.lang.String className, @Nullable java.lang.Class<?> extendedType, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Deprecated.UsecreateFastEvaluator(Reader, Class, String[])
instead- Throws:
CompileException
java.io.IOException
-
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:
createFastEvaluator
in interfaceIExpressionEvaluator
- Throws:
CompileException
java.io.IOException
- See Also:
IExpressionEvaluator.createFastEvaluator(String, Class, String[])
-
createFastEvaluator
public <T> T createFastEvaluator(java.lang.String script, java.lang.Class<? extends T> interfaceToImplement, java.lang.String... parameterNames) throws CompileException
Description copied from interface:IExpressionEvaluator
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[])
. 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
interfaceToImplement
must be accessible by the compiled class, i.e. either be declaredpublic
, or withprotected
or default access in the package of the compiled class (seeIExpressionEvaluator.setClassName(String)
.- Specified by:
createFastEvaluator
in interfaceIExpressionEvaluator
- Throws:
CompileException
-
createFastEvaluator
public <T> T createFastEvaluator(Scanner scanner, java.lang.Class<T> interfaceToImplement, java.lang.String... parameterNames) throws CompileException, java.io.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:
CompileException
java.io.IOException
- See Also:
createFastEvaluator(Reader, Class, String[])
-
guessParameterNames
public static java.lang.String[] guessParameterNames(Scanner scanner) throws CompileException, java.io.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:
CompileException
java.io.IOException
- See Also:
Scanner(String, Reader)
-
-