Package com.googlecode.aviator
Class AviatorEvaluatorInstance
- java.lang.Object
-
- com.googlecode.aviator.AviatorEvaluatorInstance
-
public final class AviatorEvaluatorInstance extends java.lang.Object
A aviator evaluator instance- Since:
- 4.0.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AviatorEvaluatorInstance.StringSegments
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<OperatorType,java.lang.String>
aliasOperatorTokens
alias operator tokenprivate AviatorClassLoader
aviatorClassLoader
private int
bytecodeVersion
Generated java class version,default 1.7private boolean
cachedExpressionByDefault
private EnvProcessor
envProcessor
private java.util.concurrent.ConcurrentHashMap<java.lang.String,java.util.concurrent.FutureTask<Expression>>
expressionCache
Compiled Expression cacheprivate LRUMap<java.lang.String,java.util.concurrent.FutureTask<Expression>>
expressionLRUCache
private java.util.Map<java.lang.String,java.lang.Object>
funcMap
private java.util.List<FunctionLoader>
functionLoaders
function loader listprivate FunctionMissing
functionMissing
private static java.util.Map<java.lang.String,AviatorFunction>
internalASMLibFunctions
cached compiled internal ASM lib functionsprivate static java.util.Map<java.lang.String,AviatorFunction>
internalInterpretedLibFunctions
cached compiled internal interpred lib functionsprivate static java.lang.String[]
libs
internal libs in main resourcesprivate java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.Object>
moduleCache
private java.util.Map<OperatorType,AviatorFunction>
opsMap
private java.util.Map<Options,Options.Value>
options
Optionsprivate java.io.OutputStream
traceOutputStream
-
Constructor Summary
Constructors Constructor Description AviatorEvaluatorInstance(EvalMode evalMode)
Create a aviator evaluator instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addFunction(AviatorFunction function)
Add an aviator function,it's not thread-safe.void
addFunction(java.lang.String name, AviatorFunction function)
Adds a function with the namevoid
addFunctionLoader(FunctionLoader loader)
Adds a function loaderjava.util.List<java.lang.String>
addInstanceFunctions(java.lang.String namespace, java.lang.Class<?> clazz)
private java.util.List<java.lang.String>
addMethodFunctions(java.lang.String namespace, boolean isStatic, java.lang.Class<?> clazz)
Env
addModule(java.lang.Class<?> moduleClazz)
Adds a module class and import it's public static methods as module's exports into module cache, return the exports map.void
addOpFunction(OperatorType opType, AviatorFunction function)
Add an operator aviator function,it's not thread-safe.java.util.List<java.lang.String>
addStaticFunctions(java.lang.String namespace, java.lang.Class<?> clazz)
void
aliasFunction(java.lang.String name, java.lang.String aliasName)
Set alias name for function specified by namevoid
aliasOperator(OperatorType type, java.lang.String token)
Set a alias token for the operator, only supports AND and OR operator right now.java.lang.Class<?>
checkIfClassIsAllowed(boolean checkIfAllow, java.lang.Class<?> clazz)
check if class is in Options.ALLOWED_CLASS_SETvoid
clearExpressionCache()
Clear all cached compiled expressionExpression
compile(java.lang.String expression)
Compile a text expression to Expression Object without cachingExpression
compile(java.lang.String expression, boolean cached)
Compile a text expression to Expression objectExpression
compile(java.lang.String cacheKey, java.lang.String expression, boolean cached)
Compile a text expression to Expression objectprivate Expression
compile(java.lang.String cacheKey, java.lang.String expression, java.lang.String sourceFile, boolean cached)
Expression
compileScript(java.lang.String path)
Compile a script file into expression, it doesn't cache the compiled result.Expression
compileScript(java.lang.String path, boolean cached)
Compile a script file into expression.Expression
compileScript(java.lang.String cacheKey, java.io.File file, boolean cached)
Compile a script into expression.AviatorEvaluatorInstance.StringSegments
compileStringSegments(java.lang.String lexeme)
Compile a string to string segments, if string doesn't have a interpolation,returns an empty list.AviatorEvaluatorInstance.StringSegments
compileStringSegments(java.lang.String lexeme, java.lang.String sourceFile, int lineNo)
Compile a string to string segments, if string doesn't have a interpolation,returns an empty list.boolean
containsFunction(java.lang.String name)
Check if the function exists in the evaluator.void
defineFunction(java.lang.String name, java.lang.String expression)
Define a function by name and expression.void
defineFunction(java.lang.String name, java.lang.String expression, java.util.Map<java.lang.String,java.lang.Object> env)
Define a function by name and expression with the execution env.void
disableFeature(Feature feature)
Disable a script engine feature.void
enableFeature(Feature feature)
Enable a script engine feature.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.private void
ensureEnableSerializableOption()
void
ensureFeatureEnabled(Feature feature)
java.lang.Object
exec(java.lang.String expression, java.lang.Object... values)
Deprecated.java.lang.Object
execute(java.lang.String expression)
Execute a text expression without caching and env map.java.lang.Object
execute(java.lang.String cacheKey, java.lang.String expression, java.util.Map<java.lang.String,java.lang.Object> env, boolean cached)
Execute a text expression with environmentjava.lang.Object
execute(java.lang.String expression, java.util.Map<java.lang.String,java.lang.Object> env)
Execute a text expression without cachingjava.lang.Object
execute(java.lang.String expression, java.util.Map<java.lang.String,java.lang.Object> env, boolean cached)
Execute a text expression with environmentprivate java.util.Map<java.lang.String,java.lang.Object>
executeModule(Expression exp, java.lang.String abPath)
private void
fillDefaultOpts()
AviatorClassLoader
getAviatorClassLoader()
Returns classloaderAviatorClassLoader
getAviatorClassLoader(boolean cached)
Returns classloaderint
getBytecodeVersion()
Returns the generated java classes byte code version, 1.6 by defualt.Expression
getCachedExpression(java.lang.String expression)
Returns a compiled expression in cacheExpression
getCachedExpressionByKey(java.lang.String cacheKey)
Returns a compiled expression in cache by cacheKey.private Expression
getCompiledExpression(java.lang.String cacheKey, java.util.concurrent.FutureTask<Expression> task)
EnvProcessor
getEnvProcessor()
Retrieve current env processor, default is null.private EvalMode
getEvalMode()
int
getExpressionCacheSize()
Returns the number of cached expressions.java.util.Set<Feature>
getFeatures()
Returns current valid syntax feature set.java.util.Map<java.lang.String,java.lang.Object>
getFuncMap()
Returns the functions mapAviatorFunction
getFunction(java.lang.String name)
AviatorFunction
getFunction(java.lang.String name, SymbolTable symbolTable)
Retrieve an aviator function by name,throw exception if not found or null.It's not thread-safe.FunctionMissing
getFunctionMissing()
Returns the function missing handler, null if not set.java.util.Map<java.lang.String,java.lang.Object>
getModuleCache()
java.lang.String
getOperatorAliasToken(OperatorType type)
AviatorFunction
getOpFunction(OperatorType opType)
Retrieve an operator aviator function by op type, return null if not found.It's not thread-safe.java.util.Map<OperatorType,AviatorFunction>
getOpsMap()
Returns the operators map.private int
getOptimizeLevel()
<T> T
getOption(Options opt)
Deprecated.java.util.Map<Options,java.lang.Object>
getOptions()
Get the evaluator instance optionsOptions.Value
getOptionValue(Options opt)
Returns the current evaluator option value union, returns null if missing.java.io.OutputStream
getTraceOutputStream()
Get current trace output stream,default is System.outjava.util.List<java.lang.String>
importFunctions(java.lang.Class<?> clazz)
Import the class public methods into aviator evaluator as custom functions.private AviatorClassLoader
initAviatorClassLoader()
private Expression
innerCompile(java.lang.String expression, java.lang.String sourceFile, boolean cached)
void
invalidateCache(java.lang.String expression)
Invalidate expression cachevoid
invalidateCacheByKey(java.lang.String cacheKey)
Invalidate expression cache by cacheKeyboolean
isCachedExpressionByDefault()
Returns true when caching compiled expression result by default.boolean
isExpressionCached(java.lang.String expression)
Returns true when the expression is in cache.boolean
isFeatureEnabled(Feature feature)
Returns true when a syntax feature is enabled.private void
loadFeatureFunctions()
private java.util.Map<java.lang.String,AviatorFunction>
loadInternalFunctions()
private void
loadInternalLibs()
private void
loadLib()
private void
loadMathFunctions()
private void
loadModule()
private Env
loadModule(java.lang.Class<?> moduleClazz)
java.util.Map<java.lang.String,java.lang.Object>
loadScript(java.lang.String path)
Loads a script from path and return its exports.private java.util.Map<java.lang.String,java.lang.Object>
loadScript0(java.lang.String abPath)
private void
loadSeqFunctions()
private void
loadStringFunctions()
private void
loadSystemFunctions()
CodeGenerator
newCodeGenerator(AviatorClassLoader classLoader, java.lang.String sourceFile)
CodeGenerator
newCodeGenerator(java.lang.String sourceFile, boolean cached)
private java.util.concurrent.FutureTask<Expression>
newCompileTask(java.lang.String expression, java.lang.String sourceFile, boolean cached)
EvalCodeGenerator
newEvalCodeGenerator(AviatorClassLoader classLoader, java.lang.String sourceFile)
java.io.ObjectInputStream
newObjectInputStream(java.io.InputStream in)
Create an ObjectInputStream from an input stream for deserialize an expression.java.io.ObjectOutputStream
newObjectOutputStream(java.io.OutputStream out)
Create an ObjectOutputStream to serialize an expression.AviatorFunction
removeFunction(AviatorFunction function)
Remove a aviator functionAviatorFunction
removeFunction(java.lang.String name)
Remove an aviator function by name,it's not thread-safe.void
removeFunctionLoader(FunctionLoader loader)
Remove a function loadervoid
removeModule(java.lang.String ns)
Remove a module by namespace name.AviatorFunction
removeOpFunction(OperatorType opType)
Remove an operator aviator function by op type, it's not thread-safe.java.util.Map<java.lang.String,java.lang.Object>
requireScript(java.lang.String path)
Loads a script from path and return its exports with module caching.void
resetClassLoader()
Reset the classloader to a new instance.void
setAviatorClassLoader(AviatorClassLoader aviatorClassLoader)
Set a custom aviator class loadervoid
setBytecodeVersion(int bytecodeVersion)
Set the generated java classes java byte code version.void
setCachedExpressionByDefault(boolean cachedExpressionByDefault)
Set true to cache the compiled expression result by default when invokecompile(String)
,compileScript(String)
,execute(String)
andexecute(String, Map)
.void
setEnvProcessor(EnvProcessor envProcessor)
Set an env processor.void
setFunctionMissing(FunctionMissing functionMissing)
Configure a function missing handler.the handler can be null.void
setOption(Options opt, java.lang.Object val)
Adds a evaluator optionvoid
setTraceOutputStream(java.io.OutputStream traceOutputStream)
Set trace output streamprivate java.io.File
tryFindFileFromClassLoader(java.lang.String path, java.lang.ClassLoader contextLoader)
java.io.File
tryFindScriptFile(java.lang.String path)
AviatorEvaluatorInstance
useLRUExpressionCache(int capacity)
UseLRUMap
as expression caching.It should be called when initializing the evaluator instance.void
validate(java.lang.String script)
Validate a script text whether is a legal aviatorscript text, throw exception if not.
-
-
-
Field Detail
-
aviatorClassLoader
private volatile AviatorClassLoader aviatorClassLoader
-
traceOutputStream
private java.io.OutputStream traceOutputStream
-
functionMissing
private FunctionMissing functionMissing
-
bytecodeVersion
private int bytecodeVersion
Generated java class version,default 1.7
-
envProcessor
private EnvProcessor envProcessor
-
options
private volatile java.util.Map<Options,Options.Value> options
Options
-
functionLoaders
private java.util.List<FunctionLoader> functionLoaders
function loader list
-
libs
private static final java.lang.String[] libs
internal libs in main resources
-
internalASMLibFunctions
private static volatile java.util.Map<java.lang.String,AviatorFunction> internalASMLibFunctions
cached compiled internal ASM lib functions
-
internalInterpretedLibFunctions
private static volatile java.util.Map<java.lang.String,AviatorFunction> internalInterpretedLibFunctions
cached compiled internal interpred lib functions
-
aliasOperatorTokens
private final java.util.Map<OperatorType,java.lang.String> aliasOperatorTokens
alias operator token
-
funcMap
private final java.util.Map<java.lang.String,java.lang.Object> funcMap
-
moduleCache
private final java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.Object> moduleCache
-
opsMap
private final java.util.Map<OperatorType,AviatorFunction> opsMap
-
expressionCache
private final java.util.concurrent.ConcurrentHashMap<java.lang.String,java.util.concurrent.FutureTask<Expression>> expressionCache
Compiled Expression cache
-
expressionLRUCache
private LRUMap<java.lang.String,java.util.concurrent.FutureTask<Expression>> expressionLRUCache
-
cachedExpressionByDefault
private boolean cachedExpressionByDefault
-
-
Constructor Detail
-
AviatorEvaluatorInstance
AviatorEvaluatorInstance(EvalMode evalMode)
Create a aviator evaluator instance.
-
-
Method Detail
-
aliasOperator
public void aliasOperator(OperatorType type, java.lang.String token)
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
public java.lang.String getOperatorAliasToken(OperatorType type)
-
newObjectInputStream
public java.io.ObjectInputStream newObjectInputStream(java.io.InputStream in) throws java.io.IOException
Create an ObjectInputStream from an input stream for deserialize an expression.- Returns:
- the stream
- Throws:
java.io.IOException
- Since:
- 5.3.4
-
ensureEnableSerializableOption
private void ensureEnableSerializableOption()
-
newObjectOutputStream
public java.io.ObjectOutputStream newObjectOutputStream(java.io.OutputStream out) throws java.io.IOException
Create an ObjectOutputStream to serialize an expression.- Returns:
- Throws:
java.io.IOException
- Since:
- 5.3.4
-
addFunctionLoader
public void addFunctionLoader(FunctionLoader loader)
Adds a function loader- Parameters:
loader
-- Since:
- 4.0.0
- See Also:
FunctionLoader
-
getEnvProcessor
public EnvProcessor getEnvProcessor()
Retrieve current env processor, default is null.- Returns:
- Since:
- 5.1.3
-
setEnvProcessor
public void setEnvProcessor(EnvProcessor envProcessor)
Set an env processor. Note, this method should be called before using the evaluator instance.- Parameters:
envProcessor
-- Since:
- 5.1.3
-
compileScript
public Expression compileScript(java.lang.String path, boolean cached) throws java.io.IOException
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:
java.io.IOException
- Since:
- 5.0.0
-
compileScript
public Expression compileScript(java.lang.String cacheKey, java.io.File file, boolean cached) throws java.io.IOException
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:
java.io.IOException
- Since:
- 5.0.0
-
tryFindScriptFile
public java.io.File tryFindScriptFile(java.lang.String path) throws java.io.IOException
- Throws:
java.io.IOException
-
setAviatorClassLoader
public void setAviatorClassLoader(AviatorClassLoader aviatorClassLoader)
Set a custom aviator class loader- Parameters:
aviatorClassLoader
-- Since:
- 5.0.0
-
tryFindFileFromClassLoader
private java.io.File tryFindFileFromClassLoader(java.lang.String path, java.lang.ClassLoader contextLoader)
-
loadScript
public java.util.Map<java.lang.String,java.lang.Object> loadScript(java.lang.String path) throws java.io.IOException
Loads a script from path and return its exports.- Parameters:
path
- the script file path- Returns:
- the exports map.
- Throws:
java.io.IOException
- Since:
- 5.0.0
-
loadScript0
private java.util.Map<java.lang.String,java.lang.Object> loadScript0(java.lang.String abPath) throws java.io.IOException
- Throws:
java.io.IOException
-
executeModule
private java.util.Map<java.lang.String,java.lang.Object> executeModule(Expression exp, java.lang.String abPath)
-
requireScript
public java.util.Map<java.lang.String,java.lang.Object> requireScript(java.lang.String path) throws java.io.IOException
Loads a script from path and return its exports with module caching.- Parameters:
path
- the script file path- Returns:
- the exports map
- Throws:
java.io.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
public Env addModule(java.lang.Class<?> moduleClazz) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException
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:
java.lang.NoSuchMethodException
java.lang.IllegalAccessException
- Since:
- 5.0.0
-
removeModule
public void removeModule(java.lang.String ns)
Remove a module by namespace name.- Parameters:
ns
-- Since:
- 5.1.4
-
loadModule
private Env loadModule(java.lang.Class<?> moduleClazz) throws java.lang.IllegalAccessException, java.lang.NoSuchMethodException
- Throws:
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
-
getModuleCache
public java.util.Map<java.lang.String,java.lang.Object> getModuleCache()
-
compileScript
public Expression compileScript(java.lang.String path) throws java.io.IOException
Compile a script file into expression, it doesn't cache the compiled result.- Parameters:
file
- the script file path- Returns:
- Throws:
java.io.IOException
-
getFunctionMissing
public FunctionMissing getFunctionMissing()
Returns the function missing handler, null if not set.- Returns:
- Since:
- 4.2.5
-
setFunctionMissing
public void setFunctionMissing(FunctionMissing functionMissing)
Configure a function missing handler.the handler can be null.- Parameters:
functionMissing
-- Since:
- 4.2.5
-
addInstanceFunctions
public java.util.List<java.lang.String> addInstanceFunctions(java.lang.String namespace, java.lang.Class<?> clazz) throws java.lang.IllegalAccessException, java.lang.NoSuchMethodException
Adds all public instance methods in the class as custom functions into evaluator except those haveIgnore
annotation, all these functions will keep the same name as method name, but prefixed with namespace, the function name can be renamed byFunction
annotation. And the function will have more than one argument than the method, the function's first argument is always the instance object(this pointer).
-
addMethodFunctions
private java.util.List<java.lang.String> addMethodFunctions(java.lang.String namespace, boolean isStatic, java.lang.Class<?> clazz) throws java.lang.IllegalAccessException, java.lang.NoSuchMethodException
- Throws:
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
-
addStaticFunctions
public java.util.List<java.lang.String> addStaticFunctions(java.lang.String namespace, java.lang.Class<?> clazz) throws java.lang.IllegalAccessException, java.lang.NoSuchMethodException
-
importFunctions
public java.util.List<java.lang.String> importFunctions(java.lang.Class<?> clazz) throws java.lang.IllegalAccessException, java.lang.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 byImport
annotation.- Parameters:
clazz
- the class- Returns:
- the added function list.
- Throws:
java.lang.NoSuchMethodException
java.lang.IllegalAccessException
- Since:
- 4.2.2
- See Also:
Import
-
removeFunctionLoader
public void removeFunctionLoader(FunctionLoader loader)
Remove a function loader- Parameters:
loader
-- Since:
- 4.0.0
-
setOption
public void setOption(Options opt, java.lang.Object val)
Adds a evaluator option- Parameters:
opt
-val
-- Since:
- 2.3.4
- See Also:
Options
-
enableFeature
public void enableFeature(Feature feature)
Enable a script engine feature.- Parameters:
feature
-- Since:
- 5.0.0
- See Also:
Feature
-
getFeatures
public java.util.Set<Feature> getFeatures()
Returns current valid syntax feature set.- Returns:
-
isFeatureEnabled
public boolean isFeatureEnabled(Feature feature)
Returns true when a syntax feature is enabled.- Parameters:
feature
-- Returns:
-
disableFeature
public void disableFeature(Feature feature)
Disable a script engine feature.- Parameters:
feature
-- Since:
- 5.0.0
- See Also:
Feature
-
getOption
@Deprecated public <T> T getOption(Options opt)
Deprecated.Returns the current evaluator option value, returns null if missing.- Parameters:
opt
-- Returns:
-
getOptionValue
public Options.Value getOptionValue(Options opt)
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:
Opcodes.V1_6
-
getOptions
public java.util.Map<Options,java.lang.Object> getOptions()
Get the evaluator instance options- Returns:
-
getFuncMap
public java.util.Map<java.lang.String,java.lang.Object> getFuncMap()
Returns the functions map- Returns:
-
getOpsMap
public java.util.Map<OperatorType,AviatorFunction> getOpsMap()
Returns the operators map.- Returns:
-
getTraceOutputStream
public java.io.OutputStream getTraceOutputStream()
Get current trace output stream,default is System.out- Returns:
-
setTraceOutputStream
public void setTraceOutputStream(java.io.OutputStream traceOutputStream)
Set trace output stream- Parameters:
traceOutputStream
-
-
initAviatorClassLoader
private AviatorClassLoader initAviatorClassLoader()
-
loadModule
private void loadModule()
-
aliasFunction
public void aliasFunction(java.lang.String name, java.lang.String aliasName)
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
private java.util.Map<java.lang.String,AviatorFunction> 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
public AviatorEvaluatorInstance useLRUExpressionCache(int capacity)
UseLRUMap
as 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
public AviatorClassLoader getAviatorClassLoader()
Returns classloader- Returns:
-
getAviatorClassLoader
public AviatorClassLoader getAviatorClassLoader(boolean cached)
Returns classloader- Returns:
-
addFunction
public void addFunction(AviatorFunction function)
Add an aviator function,it's not thread-safe.- Parameters:
function
-
-
addFunction
public void addFunction(java.lang.String name, AviatorFunction function)
Adds a function with the name- Parameters:
name
-function
-
-
defineFunction
public void defineFunction(java.lang.String name, java.lang.String expression)
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
public void defineFunction(java.lang.String name, java.lang.String expression, java.util.Map<java.lang.String,java.lang.Object> env)
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
public AviatorFunction removeFunction(java.lang.String name)
Remove an aviator function by name,it's not thread-safe.- Parameters:
name
-- Returns:
-
getFunction
public AviatorFunction getFunction(java.lang.String name)
- Parameters:
name
-- Returns:
- See Also:
getFunction(String, SymbolTable)
-
getFunction
public AviatorFunction getFunction(java.lang.String name, SymbolTable symbolTable)
Retrieve an aviator function by name,throw exception if not found or null.It's not thread-safe.- Parameters:
name
-symbolTable
-- Returns:
-
addOpFunction
public void addOpFunction(OperatorType opType, AviatorFunction function)
Add an operator aviator function,it's not thread-safe.- Parameters:
function
-
-
getOpFunction
public AviatorFunction getOpFunction(OperatorType opType)
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
public AviatorFunction removeOpFunction(OperatorType opType)
Remove an operator aviator function by op type, it's not thread-safe.- Parameters:
opType
-- Returns:
- Since:
- 3.3
-
containsFunction
public boolean containsFunction(java.lang.String name)
Check if the function exists in the evaluator. Note: it doesn't check the runtime defined functions.- Parameters:
name
-- Returns:
-
removeFunction
public AviatorFunction removeFunction(AviatorFunction function)
Remove a aviator function- Parameters:
function
-- Returns:
-
getCachedExpression
public Expression getCachedExpression(java.lang.String expression)
Returns a compiled expression in cache- Parameters:
expression
-- Returns:
-
getCachedExpressionByKey
public Expression getCachedExpressionByKey(java.lang.String cacheKey)
Returns a compiled expression in cache by cacheKey.- Parameters:
cacheKey
-- Returns:
-
isExpressionCached
public boolean isExpressionCached(java.lang.String expression)
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
public Expression compile(java.lang.String expression, boolean cached)
Compile a text expression to Expression object- Parameters:
expression
- text expressioncached
- Whether to cache the compiled result,make true to cache it.- Returns:
-
compile
public Expression compile(java.lang.String cacheKey, java.lang.String expression, boolean cached)
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
private Expression compile(java.lang.String cacheKey, java.lang.String expression, java.lang.String sourceFile, boolean cached)
-
newCompileTask
private java.util.concurrent.FutureTask<Expression> newCompileTask(java.lang.String expression, java.lang.String sourceFile, boolean cached)
-
getCompiledExpression
private Expression getCompiledExpression(java.lang.String cacheKey, java.util.concurrent.FutureTask<Expression> task)
-
innerCompile
private Expression innerCompile(java.lang.String expression, java.lang.String sourceFile, boolean cached)
-
getEvalMode
private EvalMode getEvalMode()
-
getOptimizeLevel
private int getOptimizeLevel()
-
newCodeGenerator
public CodeGenerator newCodeGenerator(java.lang.String sourceFile, boolean cached)
-
newEvalCodeGenerator
public EvalCodeGenerator newEvalCodeGenerator(AviatorClassLoader classLoader, java.lang.String sourceFile)
-
newCodeGenerator
public CodeGenerator newCodeGenerator(AviatorClassLoader classLoader, java.lang.String sourceFile)
-
compile
public Expression compile(java.lang.String expression)
Compile a text expression to Expression Object without caching- Parameters:
expression
-- Returns:
-
validate
public void validate(java.lang.String script)
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 public java.lang.Object exec(java.lang.String expression, java.lang.Object... values)
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
public java.lang.Object execute(java.lang.String cacheKey, java.lang.String expression, java.util.Map<java.lang.String,java.lang.Object> env, boolean cached)
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
public java.lang.Object execute(java.lang.String expression, java.util.Map<java.lang.String,java.lang.Object> env, boolean cached)
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
public java.lang.Object execute(java.lang.String expression, java.util.Map<java.lang.String,java.lang.Object> env)
Execute a text expression without caching- Parameters:
expression
-env
-- Returns:
-
invalidateCache
public void invalidateCache(java.lang.String expression)
Invalidate expression cache- Parameters:
expression
-
-
invalidateCacheByKey
public void invalidateCacheByKey(java.lang.String cacheKey)
Invalidate expression cache by cacheKey- Parameters:
cacheKey
-
-
execute
public java.lang.Object execute(java.lang.String expression)
Execute a text expression without caching and env map.- Parameters:
expression
-- Returns:
-
ensureFeatureEnabled
public void ensureFeatureEnabled(Feature feature)
-
compileStringSegments
public AviatorEvaluatorInstance.StringSegments compileStringSegments(java.lang.String lexeme)
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(java.lang.String lexeme, java.lang.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
public java.lang.Class<?> checkIfClassIsAllowed(boolean checkIfAllow, java.lang.Class<?> clazz)
check if class is in Options.ALLOWED_CLASS_SET- Parameters:
checkIfAllow
- check or notclazz
- the class for check- Returns:
- the class for check
-
-