Class AviatorEvaluatorInstance


  • public final class AviatorEvaluatorInstance
    extends java.lang.Object
    A aviator evaluator instance
    Since:
    4.0.0
    • Field Detail

      • traceOutputStream

        private java.io.OutputStream traceOutputStream
      • bytecodeVersion

        private int bytecodeVersion
        Generated java class version,default 1.7
      • 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
      • 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 type
        token - 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 path
        cached - 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 file
        cached - 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.
        For more information on security, please refer to the documentation
        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 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:
        java.lang.IllegalAccessException
        java.lang.NoSuchMethodException
        Since:
        4.2.3
        See Also:
        Ignore, Function
      • 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
        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:
        java.lang.IllegalAccessException
        java.lang.NoSuchMethodException
        Since:
        4.2.2
        See Also:
        Ignore, Function
      • 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 by Import 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:
      • 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 -
      • 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 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 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
      • 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​(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 name
        expression - 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 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​(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,
                                           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:
      • 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 expression
        cached - 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 expression
        cached - 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)
      • 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 use execute(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 caching
        expression - text expression
        env - Binding variable environment
        cached - 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 expression
        env - Binding variable environment
        cached - 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 not
        clazz - the class for check
        Returns:
        the class for check