Package com.googlecode.aviator
Class AviatorEvaluator
- java.lang.Object
-
- com.googlecode.aviator.AviatorEvaluator
-
public final class AviatorEvaluator extends java.lang.ObjectAvaitor Expression evaluator
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classAviatorEvaluator.StaticHolder
-
Field Summary
Fields Modifier and Type Field Description static intBYTECODE_VERDeprecated.static intCOMPILEOptimized for compile speedstatic intEVALOptimized for execute speed,this is the default optionstatic java.util.Map<java.lang.String,java.lang.Object>FUNC_MAPDeprecated.please use instance's field.static java.util.Map<OperatorType,AviatorFunction>OPS_MAPDeprecated.please use instance's field.static java.lang.StringVERSIONAviator version
-
Constructor Summary
Constructors Modifier Constructor Description privateAviatorEvaluator()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidaddFunction(AviatorFunction function)Add an aviator function,it's not thread-safe.static voidaddFunctionLoader(FunctionLoader loader)Adds a function loader.static java.util.List<java.lang.String>addInstanceFunctions(java.lang.String namespace, java.lang.Class<?> clazz)static voidaddOpFunction(OperatorType opType, AviatorFunction function)Add an operator aviator function,it's not thread-safe.static java.util.List<java.lang.String>addStaticFunctions(java.lang.String namespace, java.lang.Class<?> clazz)static voidclearExpressionCache()Clear all cached compiled expressionstatic Expressioncompile(java.lang.String expression)Compile a text expression to Expression Object without cachingstatic Expressioncompile(java.lang.String expression, boolean cached)Compile a text expression to Expression objectstatic Expressioncompile(java.lang.String cacheKey, java.lang.String expression, boolean cached)Compile a text expression to Expression object and cache it with the specified cache keystatic booleancontainsFunction(java.lang.String name)Check if the function exists in the global evaluator instance.static voiddefineFunction(java.lang.String name, java.lang.String expression)Define a function by name and expression.static voiddefineFunction(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.static java.lang.Objectexec(java.lang.String expression, java.lang.Object... values)Deprecated.static java.lang.Objectexecute(java.lang.String expression)Execute a text expression without caching and env map.static java.lang.Objectexecute(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 environmentstatic java.lang.Objectexecute(java.lang.String expression, java.util.Map<java.lang.String,java.lang.Object> env)Execute a text expression without cachingstatic java.lang.Objectexecute(java.lang.String expression, java.util.Map<java.lang.String,java.lang.Object> env, boolean cached)Execute a text expression with environmentstatic AviatorClassLoadergetAviatorClassLoader()Returns classloaderstatic AviatorClassLoadergetAviatorClassLoader(boolean cached)Returns classloaderstatic ExpressiongetCachedExpression(java.lang.String expression)Returns a compiled expression in cachestatic AviatorFunctiongetFunction(java.lang.String name)Retrieve an aviator function by name,throw exception if not found or null.It's not thread-safe.static FunctionMissinggetFunctionMissing()static AviatorEvaluatorInstancegetInstance()Get the default evaluator instancestatic java.math.MathContextgetMathContext()Deprecated.Please usegetOption(Options)static AviatorFunctiongetOpFunction(OperatorType opType)Retrieve an operator aviator function by op type, return null if not found.It's not thread-safe.static <T> TgetOption(Options opt)Deprecated.static Options.ValuegetOptionValue(Options opt)Returns the current evaluator option value union, returns null if missing.static java.io.OutputStreamgetTraceOutputStream()Get current trace output stream,default is System.outstatic java.util.List<java.lang.String>importFunctions(java.lang.Class<?> clazz)static voidinvalidateCache(java.lang.String expression)Invalidate expression cachestatic java.util.Map<java.lang.String,java.lang.Object>newEnv(java.lang.Object... args)A helper method to generate a env object.static AviatorEvaluatorInstancenewInstance()Create a aviator script engine instance.static AviatorEvaluatorInstancenewInstance(EvalMode evalMode)Create a aviator script engine instance with eval modestatic AviatorFunctionremoveFunction(AviatorFunction function)Remove a aviator functionstatic AviatorFunctionremoveFunction(java.lang.String name)Remove an aviator function by name,it's not thread-safe.static voidremoveFunctionLoader(FunctionLoader loader)Removes a function loader.static AviatorFunctionremoveOpFunction(OperatorType opType)Remove an operator aviator function by op type, it's not thread-safe.static voidsetAviatorClassLoader(AviatorClassLoader aviatorClassLoader)Deprecated.static voidsetBYTECODE_VER(int nversion)static voidsetFunctionMissing(FunctionMissing functionMissing)static voidsetMathContext(java.math.MathContext mathContext)Deprecated.please usesetOption(Options, Object)static voidsetOptimize(int value)Deprecated.please usesetOption(Options, Object)static voidsetOption(Options opt, java.lang.Object val)Adds a evaluator optionstatic voidsetTraceOutputStream(java.io.OutputStream traceOutputStream)Set trace output streamstatic voidvalidate(java.lang.String script)
-
-
-
Field Detail
-
COMPILE
public static final int COMPILE
Optimized for compile speed- See Also:
- Constant Field Values
-
EVAL
public static final int EVAL
Optimized for execute speed,this is the default option- See Also:
- Constant Field Values
-
VERSION
public static final java.lang.String VERSION
Aviator version
-
FUNC_MAP
@Deprecated public static final java.util.Map<java.lang.String,java.lang.Object> FUNC_MAP
Deprecated.please use instance's field.The global evaluator's functions map.- See Also:
AviatorEvaluatorInstance.getFuncMap()
-
OPS_MAP
@Deprecated public static final java.util.Map<OperatorType,AviatorFunction> OPS_MAP
Deprecated.please use instance's field.The global evaluator's operators map.- See Also:
AviatorEvaluatorInstance.getOpsMap()
-
BYTECODE_VER
@Deprecated public static int BYTECODE_VER
Deprecated.The global evaluator's byte code version.
-
-
Method Detail
-
newInstance
public static AviatorEvaluatorInstance newInstance(EvalMode evalMode)
Create a aviator script engine instance with eval mode- Returns:
- the script engine
- Since:
- 5.3
-
newInstance
public static AviatorEvaluatorInstance newInstance()
Create a aviator script engine instance.- Returns:
- the script engine
-
getInstance
public static AviatorEvaluatorInstance getInstance()
Get the default evaluator instance- Returns:
- Since:
- 4.0.0
-
newEnv
public static java.util.Map<java.lang.String,java.lang.Object> newEnv(java.lang.Object... args)
A helper method to generate a env object. The arguments should be a sequence of pair. - Parameters:
args-- Returns:
- A new env instance contains arguments.
- Since:
- 4.2.9
-
addFunctionLoader
public static void addFunctionLoader(FunctionLoader loader)
Adds a function loader.- Parameters:
loader-- Since:
- 4.0.0
- See Also:
FunctionLoader
-
removeFunctionLoader
public static void removeFunctionLoader(FunctionLoader loader)
Removes a function loader.- Parameters:
loader-- Since:
- 4.0.0
- See Also:
FunctionLoader
-
setFunctionMissing
public static void setFunctionMissing(FunctionMissing functionMissing)
- Parameters:
functionMissing-- See Also:
AviatorEvaluatorInstance.setFunctionMissing(FunctionMissing)
-
getFunctionMissing
public static FunctionMissing getFunctionMissing()
- Parameters:
functionMissing-- See Also:
AviatorEvaluatorInstance#getFunctionMissing(FunctionMissing)
-
setOption
public static void setOption(Options opt, java.lang.Object val)
Adds a evaluator option- Parameters:
opt-val-- Since:
- 2.3.4
- See Also:
Options
-
getOption
@Deprecated public static <T> T getOption(Options opt)
Deprecated.Returns the current evaluator option value, returns null if missing. usegetOptionValue(Options)instead.- Parameters:
opt-- Returns:
-
getOptionValue
public static 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.
-
getTraceOutputStream
public static java.io.OutputStream getTraceOutputStream()
Get current trace output stream,default is System.out- Returns:
-
getMathContext
@Deprecated public static java.math.MathContext getMathContext()
Deprecated.Please usegetOption(Options)Returns current math context for decimal.- Returns:
- Since:
- 2.3.0
-
setMathContext
@Deprecated public static void setMathContext(java.math.MathContext mathContext)
Deprecated.please usesetOption(Options, Object)Set math context for decimal.- Parameters:
mathContext-- Since:
- 2.3.0
-
setTraceOutputStream
public static void setTraceOutputStream(java.io.OutputStream traceOutputStream)
Set trace output stream- Parameters:
traceOutputStream-
-
setOptimize
@Deprecated public static void setOptimize(int value)
Deprecated.please usesetOption(Options, Object)set optimize level,default AviatorEvaluator.EVAL
-
setBYTECODE_VER
public static void setBYTECODE_VER(int nversion)
-
clearExpressionCache
public static void clearExpressionCache()
Clear all cached compiled expression
-
getAviatorClassLoader
public static AviatorClassLoader getAviatorClassLoader()
Returns classloader- Returns:
-
getAviatorClassLoader
public static AviatorClassLoader getAviatorClassLoader(boolean cached)
Returns classloader- Returns:
-
addInstanceFunctions
public static java.util.List<java.lang.String> addInstanceFunctions(java.lang.String namespace, java.lang.Class<?> clazz) throws java.lang.IllegalAccessException, java.lang.NoSuchMethodException- Throws:
java.lang.IllegalAccessExceptionjava.lang.NoSuchMethodException- See Also:
AviatorEvaluatorInstance.addInstanceFunctions(String, Class)
-
addStaticFunctions
public static java.util.List<java.lang.String> addStaticFunctions(java.lang.String namespace, java.lang.Class<?> clazz) throws java.lang.IllegalAccessException, java.lang.NoSuchMethodException- Throws:
java.lang.IllegalAccessExceptionjava.lang.NoSuchMethodException- See Also:
AviatorEvaluatorInstance.addStaticFunctions(String, Class)
-
importFunctions
public static java.util.List<java.lang.String> importFunctions(java.lang.Class<?> clazz) throws java.lang.IllegalAccessException, java.lang.NoSuchMethodException- Throws:
java.lang.IllegalAccessExceptionjava.lang.NoSuchMethodException- See Also:
AviatorEvaluatorInstance.importFunctions(Class)
-
addFunction
public static void addFunction(AviatorFunction function)
Add an aviator function,it's not thread-safe.- Parameters:
function-
-
defineFunction
public static 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 static 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 static AviatorFunction removeFunction(java.lang.String name)
Remove an aviator function by name,it's not thread-safe.- Parameters:
name-- Returns:
-
getFunction
public static AviatorFunction getFunction(java.lang.String name)
Retrieve an aviator function by name,throw exception if not found or null.It's not thread-safe.- Parameters:
name-- Returns:
-
addOpFunction
public static void addOpFunction(OperatorType opType, AviatorFunction function)
Add an operator aviator function,it's not thread-safe.- Parameters:
function-
-
getOpFunction
public static 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 static 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 static boolean containsFunction(java.lang.String name)
Check if the function exists in the global evaluator instance.- Parameters:
name-- Returns:
-
removeFunction
public static AviatorFunction removeFunction(AviatorFunction function)
Remove a aviator function- Parameters:
function-- Returns:
-
setAviatorClassLoader
@Deprecated public static void setAviatorClassLoader(AviatorClassLoader aviatorClassLoader)
Deprecated.Configure user defined classloader- Parameters:
aviatorClassLoader-
-
getCachedExpression
public static Expression getCachedExpression(java.lang.String expression)
Returns a compiled expression in cache- Parameters:
expression-- Returns:
-
compile
public static Expression compile(java.lang.String cacheKey, java.lang.String expression, boolean cached)
Compile a text expression to Expression object and cache it with the specified cache key- Parameters:
cacheKey- cache keyexpression- text expressioncached- Whether to cache the compiled result,make true to cache it.- Returns:
-
compile
public static 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 static Expression compile(java.lang.String expression)
Compile a text expression to Expression Object without caching- Parameters:
expression-- Returns:
-
validate
public static void validate(java.lang.String script)
- Parameters:
script-- See Also:
AviatorEvaluatorInstance.validate(String)
-
exec
@Deprecated public static 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 static 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 static 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 static 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 static void invalidateCache(java.lang.String expression)
Invalidate expression cache- Parameters:
expression-
-
execute
public static java.lang.Object execute(java.lang.String expression)
Execute a text expression without caching and env map.- Parameters:
expression-- Returns:
-
-