Package com.googlecode.aviator
Class AviatorEvaluatorInstance
java.lang.Object
com.googlecode.aviator.AviatorEvaluatorInstance
A aviator evaluator instance
- Since:
- 4.0.0
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<OperatorType, String> alias operator tokenprivate AviatorClassLoaderprivate intGenerated java class version,default 1.7private booleanprivate EnvProcessorprivate final ConcurrentHashMap<String, FutureTask<Expression>> Compiled Expression cacheprivate LRUMap<String, FutureTask<Expression>> private List<FunctionLoader> function loader listprivate FunctionMissingprivate static Map<String, AviatorFunction> cached compiled internal ASM lib functionsprivate static Map<String, AviatorFunction> cached compiled internal interpred lib functionsprivate static final String[]internal libs in main resourcesprivate final ConcurrentHashMap<String, Object> private final Map<OperatorType, AviatorFunction> private Map<Options, Options.Value> Optionsprivate OutputStream -
Constructor Summary
ConstructorsConstructorDescriptionAviatorEvaluatorInstance(EvalMode evalMode) Create a aviator evaluator instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddFunction(AviatorFunction function) Add an aviator function,it's not thread-safe.voidaddFunction(String name, AviatorFunction function) Adds a function with the namevoidaddFunctionLoader(FunctionLoader loader) Adds a function loaderaddInstanceFunctions(String namespace, Class<?> clazz) addMethodFunctions(String namespace, boolean isStatic, Class<?> clazz) Adds a module class and import it's public static methods as module's exports into module cache, return the exports map.voidaddOpFunction(OperatorType opType, AviatorFunction function) Add an operator aviator function,it's not thread-safe.addStaticFunctions(String namespace, Class<?> clazz) voidaliasFunction(String name, String aliasName) Set alias name for function specified by namevoidaliasOperator(OperatorType type, String token) Set a alias token for the operator, only supports AND and OR operator right now.Class<?> checkIfClassIsAllowed(boolean checkIfAllow, Class<?> clazz) check if class is in Options.ALLOWED_CLASS_SETvoidClear all cached compiled expressionCompile a text expression to Expression Object without cachingCompile a text expression to Expression objectCompile a text expression to Expression objectprivate ExpressioncompileScript(String path) Compile a script file into expression, it doesn't cache the compiled result.compileScript(String path, boolean cached) Compile a script file into expression.compileScript(String cacheKey, File file, boolean cached) Compile a script into expression.compileStringSegments(String lexeme) Compile a string to string segments, if string doesn't have a interpolation,returns an empty list.compileStringSegments(String lexeme, String sourceFile, int lineNo) Compile a string to string segments, if string doesn't have a interpolation,returns an empty list.booleancontainsFunction(String name) Check if the function exists in the evaluator.voiddefineFunction(String name, String expression) Define a function by name and expression.voidDefine a function by name and expression with the execution env.voiddisableFeature(Feature feature) Disable a script engine feature.voidenableFeature(Feature feature) Enable a script engine feature.voidConfigure the evaluator into sandbox mode for security, it means: Disable syntax feature: Module, NewInstance, StaticMethods and InternalVars, Disable reflection invocation by function missing, Set the single maximum loop counter to 65535, Set ALLOWED_CLASS_SET and ASSIGNABLE_ALLOWED_CLASS_SET to be empty, disable all classes to be accessed via static fields or methods, Set the EVAL_TIMEOUT_MS to be 1000 milliseconds(1 second), which means the execution timeout.private voidvoidensureFeatureEnabled(Feature feature) Deprecated.Execute a text expression without caching and env map.Execute a text expression with environmentExecute a text expression without cachingExecute a text expression with environmentexecuteModule(Expression exp, String abPath) private voidReturns classloadergetAviatorClassLoader(boolean cached) Returns classloaderintReturns the generated java classes byte code version, 1.6 by defualt.getCachedExpression(String expression) Returns a compiled expression in cachegetCachedExpressionByKey(String cacheKey) Returns a compiled expression in cache by cacheKey.private ExpressiongetCompiledExpression(String cacheKey, FutureTask<Expression> task) Retrieve current env processor, default is null.private EvalModeintReturns the number of cached expressions.Returns current valid syntax feature set.Returns the functions mapgetFunction(String name) getFunction(String name, SymbolTable symbolTable) Retrieve an aviator function by name,throw exception if not found or null.It's not thread-safe.Returns the function missing handler, null if not set.getOpFunction(OperatorType opType) Retrieve an operator aviator function by op type, return null if not found.It's not thread-safe.Returns the operators map.private int<T> TDeprecated.Get the evaluator instance optionsgetOptionValue(Options opt) Returns the current evaluator option value union, returns null if missing.Get current trace output stream,default is System.outimportFunctions(Class<?> clazz) Import the class public methods into aviator evaluator as custom functions.private AviatorClassLoaderprivate ExpressioninnerCompile(String expression, String sourceFile, boolean cached) voidinvalidateCache(String expression) Invalidate expression cachevoidinvalidateCacheByKey(String cacheKey) Invalidate expression cache by cacheKeybooleanReturns true when caching compiled expression result by default.booleanisExpressionCached(String expression) Returns true when the expression is in cache.booleanisFeatureEnabled(Feature feature) Returns true when a syntax feature is enabled.private voidprivate Map<String, AviatorFunction> private voidprivate voidloadLib()private voidprivate voidprivate EnvloadModule(Class<?> moduleClazz) loadScript(String path) Loads a script from path and return its exports.loadScript0(String abPath) private voidprivate voidprivate voidnewCodeGenerator(AviatorClassLoader classLoader, String sourceFile) newCodeGenerator(String sourceFile, boolean cached) private FutureTask<Expression> newCompileTask(String expression, String sourceFile, boolean cached) newEvalCodeGenerator(AviatorClassLoader classLoader, String sourceFile) Create an ObjectInputStream from an input stream for deserialize an expression.Create an ObjectOutputStream to serialize an expression.removeFunction(AviatorFunction function) Remove a aviator functionremoveFunction(String name) Remove an aviator function by name,it's not thread-safe.voidremoveFunctionLoader(FunctionLoader loader) Remove a function loadervoidremoveModule(String ns) Remove a module by namespace name.removeOpFunction(OperatorType opType) Remove an operator aviator function by op type, it's not thread-safe.requireScript(String path) Loads a script from path and return its exports with module caching.voidReset the classloader to a new instance.voidsetAviatorClassLoader(AviatorClassLoader aviatorClassLoader) Set a custom aviator class loadervoidsetBytecodeVersion(int bytecodeVersion) Set the generated java classes java byte code version.voidsetCachedExpressionByDefault(boolean cachedExpressionByDefault) Set true to cache the compiled expression result by default when invokecompile(String),compileScript(String),execute(String)andexecute(String, Map).voidsetEnvProcessor(EnvProcessor envProcessor) Set an env processor.voidsetFunctionMissing(FunctionMissing functionMissing) Configure a function missing handler.the handler can be null.voidAdds a evaluator optionvoidsetTraceOutputStream(OutputStream traceOutputStream) Set trace output streamprivate FiletryFindFileFromClassLoader(String path, ClassLoader contextLoader) tryFindScriptFile(String path) useLRUExpressionCache(int capacity) UseLRUMapas expression caching.It should be called when initializing the evaluator instance.voidValidate a script text whether is a legal aviatorscript text, throw exception if not.
-
Field Details
-
aviatorClassLoader
-
traceOutputStream
-
functionMissing
-
bytecodeVersion
private int bytecodeVersionGenerated java class version,default 1.7 -
envProcessor
-
options
Options -
functionLoaders
function loader list -
libs
internal libs in main resources -
internalASMLibFunctions
cached compiled internal ASM lib functions -
internalInterpretedLibFunctions
cached compiled internal interpred lib functions -
aliasOperatorTokens
alias operator token -
funcMap
-
moduleCache
-
opsMap
-
expressionCache
Compiled Expression cache -
expressionLRUCache
-
cachedExpressionByDefault
private boolean cachedExpressionByDefault
-
-
Constructor Details
-
AviatorEvaluatorInstance
AviatorEvaluatorInstance(EvalMode evalMode) Create a aviator evaluator instance.
-
-
Method Details
-
aliasOperator
Set a alias token for the operator, only supports AND and OR operator right now. It's not thread-safe, and you must call it before using this instance- Parameters:
type- the operator typetoken- the alias token- Since:
- 5.3.1
-
getOperatorAliasToken
-
newObjectInputStream
Create an ObjectInputStream from an input stream for deserialize an expression.- Returns:
- the stream
- Throws:
IOException- Since:
- 5.3.4
-
ensureEnableSerializableOption
private void ensureEnableSerializableOption() -
newObjectOutputStream
Create an ObjectOutputStream to serialize an expression.- Returns:
- Throws:
IOException- Since:
- 5.3.4
-
addFunctionLoader
Adds a function loader- Parameters:
loader-- Since:
- 4.0.0
- See Also:
-
getEnvProcessor
Retrieve current env processor, default is null.- Returns:
- Since:
- 5.1.3
-
setEnvProcessor
Set an env processor. Note, this method should be called before using the evaluator instance.- Parameters:
envProcessor-- Since:
- 5.1.3
-
compileScript
Compile a script file into expression.- Parameters:
path- the script file pathcached- whether to cache the compiled result with key is script file's absolute path.- Returns:
- the compiled expression instance.
- Throws:
IOException- Since:
- 5.0.0
-
compileScript
Compile a script into expression.- Parameters:
cacheKey- caching key when cached is true.file- the script filecached- whether to cache the expression instance by cacheKey.- Returns:
- the compiled expression instance.
- Throws:
IOException- Since:
- 5.0.0
-
tryFindScriptFile
- Throws:
IOException
-
setAviatorClassLoader
Set a custom aviator class loader- Parameters:
aviatorClassLoader-- Since:
- 5.0.0
-
tryFindFileFromClassLoader
-
loadScript
Loads a script from path and return its exports.- Parameters:
path- the script file path- Returns:
- the exports map.
- Throws:
IOException- Since:
- 5.0.0
-
loadScript0
- Throws:
IOException
-
executeModule
-
requireScript
Loads a script from path and return its exports with module caching.- Parameters:
path- the script file path- Returns:
- the exports map
- Throws:
IOException- Since:
- 5.0.0
-
enableSandboxMode
public void enableSandboxMode()Configure the evaluator into sandbox mode for security, it means:- Disable syntax feature: Module, NewInstance, StaticMethods and InternalVars,
- Disable reflection invocation by function missing,
- Set the single maximum loop counter to 65535,
- Set ALLOWED_CLASS_SET and ASSIGNABLE_ALLOWED_CLASS_SET to be empty, disable all classes to be accessed via static fields or methods,
- Set the EVAL_TIMEOUT_MS to be 1000 milliseconds(1 second), which means the execution timeout.
- Since:
- 5.4.3
-
addModule
Adds a module class and import it's public static methods as module's exports into module cache, return the exports map.- Parameters:
moduleClazz-- Returns:
- the exports map
- Throws:
NoSuchMethodExceptionIllegalAccessException- Since:
- 5.0.0
-
removeModule
Remove a module by namespace name.- Parameters:
ns-- Since:
- 5.1.4
-
loadModule
-
getModuleCache
-
compileScript
Compile a script file into expression, it doesn't cache the compiled result.- Parameters:
file- the script file path- Returns:
- Throws:
IOException
-
getFunctionMissing
Returns the function missing handler, null if not set.- Returns:
- Since:
- 4.2.5
-
setFunctionMissing
Configure a function missing handler.the handler can be null.- Parameters:
functionMissing-- Since:
- 4.2.5
-
addInstanceFunctions
public List<String> addInstanceFunctions(String namespace, Class<?> clazz) throws IllegalAccessException, NoSuchMethodException Adds all public instance methods in the class as custom functions into evaluator except those haveIgnoreannotation, all these functions will keep the same name as method name, but prefixed with namespace, the function name can be renamed byFunctionannotation. And the function will have more than one argument than the method, the function's first argument is always the instance object(this pointer).- Parameters:
namespace- the functions namespaceclazz- the class- Returns:
- the added function list.
- Throws:
IllegalAccessExceptionNoSuchMethodException- Since:
- 4.2.3
- See Also:
-
addMethodFunctions
private List<String> addMethodFunctions(String namespace, boolean isStatic, Class<?> clazz) throws IllegalAccessException, NoSuchMethodException -
addStaticFunctions
public List<String> addStaticFunctions(String namespace, Class<?> clazz) throws IllegalAccessException, NoSuchMethodException Adds all public static methods in the class as custom functions into evaluator except those haveIgnoreannotation, all these functions will keep the same name as method name, but prefixed with namespace, the function name can be renamed byFunctionannotation.- Parameters:
namespace- the functions namespaceclazz- the class- Returns:
- the added function list.
- Throws:
IllegalAccessExceptionNoSuchMethodException- Since:
- 4.2.2
- See Also:
-
importFunctions
public List<String> importFunctions(Class<?> clazz) throws IllegalAccessException, NoSuchMethodException Import the class public methods into aviator evaluator as custom functions. The function's namespace is the class name by default, and the scopes are both static and instance methods. The namespace and scope can be set byImportannotation.- Parameters:
clazz- the class- Returns:
- the added function list.
- Throws:
NoSuchMethodExceptionIllegalAccessException- Since:
- 4.2.2
- See Also:
-
removeFunctionLoader
Remove a function loader- Parameters:
loader-- Since:
- 4.0.0
-
setOption
Adds a evaluator option- Parameters:
opt-val-- Since:
- 2.3.4
- See Also:
-
enableFeature
Enable a script engine feature.- Parameters:
feature-- Since:
- 5.0.0
- See Also:
-
getFeatures
Returns current valid syntax feature set.- Returns:
-
isFeatureEnabled
Returns true when a syntax feature is enabled.- Parameters:
feature-- Returns:
-
disableFeature
Disable a script engine feature.- Parameters:
feature-- Since:
- 5.0.0
- See Also:
-
getOption
Deprecated.Returns the current evaluator option value, returns null if missing.- Parameters:
opt-- Returns:
-
getOptionValue
Returns the current evaluator option value union, returns null if missing.- Parameters:
opt-- Returns:
- the option value, null if missing.
-
getBytecodeVersion
public int getBytecodeVersion()Returns the generated java classes byte code version, 1.6 by defualt.- Returns:
- the bytecode version.
-
setBytecodeVersion
public void setBytecodeVersion(int bytecodeVersion) Set the generated java classes java byte code version.- Parameters:
bytecodeVersion-- See Also:
-
getOptions
Get the evaluator instance options- Returns:
-
getFuncMap
Returns the functions map- Returns:
-
getOpsMap
Returns the operators map.- Returns:
-
getTraceOutputStream
Get current trace output stream,default is System.out- Returns:
-
setTraceOutputStream
Set trace output stream- Parameters:
traceOutputStream-
-
initAviatorClassLoader
-
loadModule
private void loadModule() -
aliasFunction
Set alias name for function specified by name- Parameters:
name- the origin function namealiasName- the alias function name- Since:
- 5.0.0
-
loadLib
private void loadLib() -
loadSeqFunctions
private void loadSeqFunctions() -
loadMathFunctions
private void loadMathFunctions() -
loadStringFunctions
private void loadStringFunctions() -
loadSystemFunctions
private void loadSystemFunctions() -
loadInternalLibs
private void loadInternalLibs() -
loadInternalFunctions
-
fillDefaultOpts
private void fillDefaultOpts() -
isCachedExpressionByDefault
public boolean isCachedExpressionByDefault()Returns true when caching compiled expression result by default.- Returns:
- Since:
- 5.2.2
-
setCachedExpressionByDefault
public void setCachedExpressionByDefault(boolean cachedExpressionByDefault) Set true to cache the compiled expression result by default when invokecompile(String),compileScript(String),execute(String)andexecute(String, Map). Default is false.- Parameters:
cachedExpressionByDefault-- Since:
- 5.2.2
-
loadFeatureFunctions
private void loadFeatureFunctions() -
useLRUExpressionCache
UseLRUMapas expression caching.It should be called when initializing the evaluator instance.- Parameters:
capacity-- Returns:
- the evaluator instance itself.
- Since:
- 5.0.0
-
clearExpressionCache
public void clearExpressionCache()Clear all cached compiled expression -
resetClassLoader
public void resetClassLoader()Reset the classloader to a new instance.- Since:
- 5.0.0
-
getAviatorClassLoader
Returns classloader- Returns:
-
getAviatorClassLoader
Returns classloader- Returns:
-
addFunction
Add an aviator function,it's not thread-safe.- Parameters:
function-
-
addFunction
Adds a function with the name- Parameters:
name-function-
-
defineFunction
Define a function by name and expression.- Parameters:
name- the function nameexpression- the expression to be executed and it's result must be a function.- Since:
- 4.0.0
-
defineFunction
Define a function by name and expression with the execution env.- Parameters:
name- the function nameexpression- the expression to be executed and it's result must be a function.env- the expression execution env- Since:
- 4.0.0
-
removeFunction
Remove an aviator function by name,it's not thread-safe.- Parameters:
name-- Returns:
-
getFunction
- Parameters:
name-- Returns:
- See Also:
-
getFunction
Retrieve an aviator function by name,throw exception if not found or null.It's not thread-safe.- Parameters:
name-symbolTable-- Returns:
-
addOpFunction
Add an operator aviator function,it's not thread-safe.- Parameters:
function-
-
getOpFunction
Retrieve an operator aviator function by op type, return null if not found.It's not thread-safe.- Parameters:
opType-- Returns:
- Since:
- 3.3
-
removeOpFunction
Remove an operator aviator function by op type, it's not thread-safe.- Parameters:
opType-- Returns:
- Since:
- 3.3
-
containsFunction
Check if the function exists in the evaluator. Note: it doesn't check the runtime defined functions.- Parameters:
name-- Returns:
-
removeFunction
Remove a aviator function- Parameters:
function-- Returns:
-
getCachedExpression
Returns a compiled expression in cache- Parameters:
expression-- Returns:
-
getCachedExpressionByKey
Returns a compiled expression in cache by cacheKey.- Parameters:
cacheKey-- Returns:
-
isExpressionCached
Returns true when the expression is in cache.- Parameters:
expression-- Returns:
- Since:
- 4.0.0
-
getExpressionCacheSize
public int getExpressionCacheSize()Returns the number of cached expressions.- Returns:
- Since:
- 4.0.0
-
compile
Compile a text expression to Expression object- Parameters:
expression- text expressioncached- Whether to cache the compiled result,make true to cache it.- Returns:
-
compile
Compile a text expression to Expression object- Parameters:
cacheKey- unique key for caching.expression- text expressioncached- Whether to cache the compiled result,make true to cache it.- Returns:
-
compile
-
newCompileTask
-
getCompiledExpression
-
innerCompile
-
getEvalMode
-
getOptimizeLevel
private int getOptimizeLevel() -
newCodeGenerator
-
newEvalCodeGenerator
-
newCodeGenerator
-
compile
Compile a text expression to Expression Object without caching- Parameters:
expression-- Returns:
-
validate
Validate a script text whether is a legal aviatorscript text, throw exception if not.- Parameters:
script- the script text- Since:
- 5.0.2
-
exec
Deprecated.Execute a text expression with values that are variables order in the expression.It only runs in EVAL mode,and it will cache the compiled expression.It's deprecated, please useexecute(String, Map)instead.- Parameters:
expression-values-- Returns:
-
execute
Execute a text expression with environment- Parameters:
cacheKey- unique key for cachingexpression- text expressionenv- Binding variable environmentcached- Whether to cache the compiled result,make true to cache it.
-
execute
Execute a text expression with environment- Parameters:
expression- text expressionenv- Binding variable environmentcached- Whether to cache the compiled result,make true to cache it.
-
execute
Execute a text expression without caching- Parameters:
expression-env-- Returns:
-
invalidateCache
Invalidate expression cache- Parameters:
expression-
-
invalidateCacheByKey
Invalidate expression cache by cacheKey- Parameters:
cacheKey-
-
execute
Execute a text expression without caching and env map.- Parameters:
expression-- Returns:
-
ensureFeatureEnabled
-
compileStringSegments
Compile a string to string segments, if string doesn't have a interpolation,returns an empty list.- Parameters:
lexeme-- Returns:
-
compileStringSegments
public AviatorEvaluatorInstance.StringSegments compileStringSegments(String lexeme, String sourceFile, int lineNo) Compile a string to string segments, if string doesn't have a interpolation,returns an empty list.- Parameters:
lexeme-sourceFile-lineNo- ;- Returns:
-
checkIfClassIsAllowed
check if class is in Options.ALLOWED_CLASS_SET- Parameters:
checkIfAllow- check or notclazz- the class for check- Returns:
- the class for check
-