Class AviatorEvaluatorInstance

java.lang.Object
com.googlecode.aviator.AviatorEvaluatorInstance

public final class AviatorEvaluatorInstance extends Object
A aviator evaluator instance
Since:
4.0.0
  • Field Details

  • Constructor Details

    • AviatorEvaluatorInstance

      AviatorEvaluatorInstance(EvalMode evalMode)
      Create a aviator evaluator instance.
  • Method Details

    • aliasOperator

      public void aliasOperator(OperatorType type, 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 type
      token - the alias token
      Since:
      5.3.1
    • getOperatorAliasToken

      public String getOperatorAliasToken(OperatorType type)
    • newObjectInputStream

      public ObjectInputStream newObjectInputStream(InputStream in) throws IOException
      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

      public ObjectOutputStream newObjectOutputStream(OutputStream out) throws IOException
      Create an ObjectOutputStream to serialize an expression.
      Returns:
      Throws:
      IOException
      Since:
      5.3.4
    • addFunctionLoader

      public void addFunctionLoader(FunctionLoader loader)
      Adds a function loader
      Parameters:
      loader -
      Since:
      4.0.0
      See Also:
    • 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(String path, boolean cached) throws IOException
      Compile a script file into expression.
      Parameters:
      path - the script file path
      cached - 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

      public Expression compileScript(String cacheKey, File file, boolean cached) throws IOException
      Compile a script into expression.
      Parameters:
      cacheKey - caching key when cached is true.
      file - the script file
      cached - whether to cache the expression instance by cacheKey.
      Returns:
      the compiled expression instance.
      Throws:
      IOException
      Since:
      5.0.0
    • tryFindScriptFile

      public File tryFindScriptFile(String path) throws IOException
      Throws:
      IOException
    • setAviatorClassLoader

      public void setAviatorClassLoader(AviatorClassLoader aviatorClassLoader)
      Set a custom aviator class loader
      Parameters:
      aviatorClassLoader -
      Since:
      5.0.0
    • tryFindFileFromClassLoader

      private File tryFindFileFromClassLoader(String path, ClassLoader contextLoader)
    • loadScript

      public Map<String,Object> loadScript(String path) throws IOException
      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

      private Map<String,Object> loadScript0(String abPath) throws IOException
      Throws:
      IOException
    • executeModule

      private Map<String,Object> executeModule(Expression exp, String abPath)
    • requireScript

      public Map<String,Object> requireScript(String path) throws IOException
      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.
      For more information on security, please refer to the documentation
      Since:
      5.4.3
    • addModule

      public Env addModule(Class<?> moduleClazz) throws NoSuchMethodException, 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:
      NoSuchMethodException
      IllegalAccessException
      Since:
      5.0.0
    • removeModule

      public void removeModule(String ns)
      Remove a module by namespace name.
      Parameters:
      ns -
      Since:
      5.1.4
    • loadModule

      private Env loadModule(Class<?> moduleClazz) throws IllegalAccessException, NoSuchMethodException
      Throws:
      IllegalAccessException
      NoSuchMethodException
    • getModuleCache

      public Map<String,Object> getModuleCache()
    • compileScript

      public Expression compileScript(String path) throws IOException
      Compile a script file into expression, it doesn't cache the compiled result.
      Parameters:
      file - the script file path
      Returns:
      Throws:
      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 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 have Ignore annotation, all these functions will keep the same name as method name, but prefixed with namespace, the function name can be renamed by Function 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).
      Parameters:
      namespace - the functions namespace
      clazz - the class
      Returns:
      the added function list.
      Throws:
      IllegalAccessException
      NoSuchMethodException
      Since:
      4.2.3
      See Also:
    • addMethodFunctions

      private List<String> addMethodFunctions(String namespace, boolean isStatic, Class<?> clazz) throws IllegalAccessException, NoSuchMethodException
      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 have Ignore annotation, all these functions will keep the same name as method name, but prefixed with namespace, the function name can be renamed by Function annotation.
      Parameters:
      namespace - the functions namespace
      clazz - the class
      Returns:
      the added function list.
      Throws:
      IllegalAccessException
      NoSuchMethodException
      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 by Import annotation.
      Parameters:
      clazz - the class
      Returns:
      the added function list.
      Throws:
      NoSuchMethodException
      IllegalAccessException
      Since:
      4.2.2
      See Also:
    • removeFunctionLoader

      public void removeFunctionLoader(FunctionLoader loader)
      Remove a function loader
      Parameters:
      loader -
      Since:
      4.0.0
    • setOption

      public void setOption(Options opt, Object val)
      Adds a evaluator option
      Parameters:
      opt -
      val -
      Since:
      2.3.4
      See Also:
    • enableFeature

      public void enableFeature(Feature feature)
      Enable a script engine feature.
      Parameters:
      feature -
      Since:
      5.0.0
      See Also:
    • getFeatures

      public 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:
    • 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:
    • getOptions

      public Map<Options,Object> getOptions()
      Get the evaluator instance options
      Returns:
    • getFuncMap

      public Map<String,Object> getFuncMap()
      Returns the functions map
      Returns:
    • getOpsMap

      public Map<OperatorType,AviatorFunction> getOpsMap()
      Returns the operators map.
      Returns:
    • getTraceOutputStream

      public OutputStream getTraceOutputStream()
      Get current trace output stream,default is System.out
      Returns:
    • setTraceOutputStream

      public void setTraceOutputStream(OutputStream traceOutputStream)
      Set trace output stream
      Parameters:
      traceOutputStream -
    • initAviatorClassLoader

      private AviatorClassLoader initAviatorClassLoader()
    • loadModule

      private void loadModule()
    • aliasFunction

      public void aliasFunction(String name, String aliasName)
      Set alias name for function specified by name
      Parameters:
      name - the origin function name
      aliasName - 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 Map<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 invoke compile(String), compileScript(String), execute(String) and execute(String, Map). Default is false.
      Parameters:
      cachedExpressionByDefault -
      Since:
      5.2.2
    • loadFeatureFunctions

      private void loadFeatureFunctions()
    • useLRUExpressionCache

      public AviatorEvaluatorInstance useLRUExpressionCache(int capacity)
      Use LRUMap 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(String name, AviatorFunction function)
      Adds a function with the name
      Parameters:
      name -
      function -
    • defineFunction

      public void defineFunction(String name, String expression)
      Define a function by name and expression.
      Parameters:
      name - the function name
      expression - the expression to be executed and it's result must be a function.
      Since:
      4.0.0
    • defineFunction

      public void defineFunction(String name, String expression, Map<String,Object> env)
      Define a function by name and expression with the execution env.
      Parameters:
      name - the function name
      expression - 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(String name)
      Remove an aviator function by name,it's not thread-safe.
      Parameters:
      name -
      Returns:
    • getFunction

      public AviatorFunction getFunction(String name)
      Parameters:
      name -
      Returns:
      See Also:
    • getFunction

      public AviatorFunction getFunction(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(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(String expression)
      Returns a compiled expression in cache
      Parameters:
      expression -
      Returns:
    • getCachedExpressionByKey

      public Expression getCachedExpressionByKey(String cacheKey)
      Returns a compiled expression in cache by cacheKey.
      Parameters:
      cacheKey -
      Returns:
    • isExpressionCached

      public boolean isExpressionCached(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(String expression, boolean cached)
      Compile a text expression to Expression object
      Parameters:
      expression - text expression
      cached - Whether to cache the compiled result,make true to cache it.
      Returns:
    • compile

      public Expression compile(String cacheKey, String expression, boolean cached)
      Compile a text expression to Expression object
      Parameters:
      cacheKey - unique key for caching.
      expression - text expression
      cached - Whether to cache the compiled result,make true to cache it.
      Returns:
    • compile

      private Expression compile(String cacheKey, String expression, String sourceFile, boolean cached)
    • newCompileTask

      private FutureTask<Expression> newCompileTask(String expression, String sourceFile, boolean cached)
    • getCompiledExpression

      private Expression getCompiledExpression(String cacheKey, FutureTask<Expression> task)
    • innerCompile

      private Expression innerCompile(String expression, String sourceFile, boolean cached)
    • getEvalMode

      private EvalMode getEvalMode()
    • getOptimizeLevel

      private int getOptimizeLevel()
    • newCodeGenerator

      public CodeGenerator newCodeGenerator(String sourceFile, boolean cached)
    • newEvalCodeGenerator

      public EvalCodeGenerator newEvalCodeGenerator(AviatorClassLoader classLoader, String sourceFile)
    • newCodeGenerator

      public CodeGenerator newCodeGenerator(AviatorClassLoader classLoader, String sourceFile)
    • compile

      public Expression compile(String expression)
      Compile a text expression to Expression Object without caching
      Parameters:
      expression -
      Returns:
    • validate

      public void validate(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 Object exec(String expression, 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 use execute(String, Map) instead.
      Parameters:
      expression -
      values -
      Returns:
    • execute

      public Object execute(String cacheKey, String expression, Map<String,Object> env, boolean cached)
      Execute a text expression with environment
      Parameters:
      cacheKey - unique key for caching
      expression - text expression
      env - Binding variable environment
      cached - Whether to cache the compiled result,make true to cache it.
    • execute

      public Object execute(String expression, Map<String,Object> env, boolean cached)
      Execute a text expression with environment
      Parameters:
      expression - text expression
      env - Binding variable environment
      cached - Whether to cache the compiled result,make true to cache it.
    • execute

      public Object execute(String expression, Map<String,Object> env)
      Execute a text expression without caching
      Parameters:
      expression -
      env -
      Returns:
    • invalidateCache

      public void invalidateCache(String expression)
      Invalidate expression cache
      Parameters:
      expression -
    • invalidateCacheByKey

      public void invalidateCacheByKey(String cacheKey)
      Invalidate expression cache by cacheKey
      Parameters:
      cacheKey -
    • execute

      public Object execute(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(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(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

      public Class<?> checkIfClassIsAllowed(boolean checkIfAllow, Class<?> clazz)
      check if class is in Options.ALLOWED_CLASS_SET
      Parameters:
      checkIfAllow - check or not
      clazz - the class for check
      Returns:
      the class for check