Class Env


  • @Internal
    public class Env
    extends java.lang.Object
    Environment used during checking of expressions. Provides name resolution and error reporting.

    Note: the environment is not thread-safe. Create multiple envs from scratch for working with different threads.

    CEL Library Internals. Do Not Use. CEL-Java users should leverage the Fluent APIs instead. See CelCompilerFactory.

    • Field Detail

      • ERROR_IDENT_DECL

        public static final CelIdentDecl ERROR_IDENT_DECL
        An ident declaration to represent an error.
      • ERROR_FUNCTION_DECL

        public static final CelFunctionDecl ERROR_FUNCTION_DECL
        A function declaration to represent an error.
    • Method Detail

      • unconfigured

        @Deprecated
        public static Env unconfigured​(Errors errors,
                                       ExprFeatures... exprFeatures)
        Deprecated.
        use unconfigured with CelOptions instead.
        Creates an unconfigured Env value without the standard CEL types, functions, and operators with a reference to the feature flags enabled in the environment.
      • unconfigured

        @Deprecated
        public static Env unconfigured​(Errors errors,
                                       TypeProvider typeProvider,
                                       ExprFeatures... exprFeatures)
        Deprecated.
        use unconfigured with CelOptions instead.
        Creates an unconfigured Env value without the standard CEL types, functions, and operators using a custom typeProvider.
      • unconfigured

        @Deprecated
        public static Env unconfigured​(Errors errors,
                                       TypeProvider typeProvider,
                                       com.google.common.collect.ImmutableSet<ExprFeatures> exprFeatures)
        Deprecated.
        use unconfigured with CelOptions instead.
        Creates an unconfigured Env value without the standard CEL types, functions, and operators using a custom typeProvider. The set of enabled exprFeatures is also provided.
      • unconfigured

        public static Env unconfigured​(Errors errors,
                                       CelOptions celOptions)
        Creates an unconfigured Env value without the standard CEL types, functions, and operators with a reference to the configured celOptions.
      • unconfigured

        public static Env unconfigured​(Errors errors,
                                       TypeProvider typeProvider,
                                       CelOptions celOptions)
        Creates an unconfigured Env value without the standard CEL types, functions, and operators using a custom typeProvider. The CelOptions are provided as well.
      • standard

        @Deprecated
        public static Env standard​(Errors errors,
                                   ExprFeatures... exprFeatures)
        Deprecated.
        use standard with CelOptions instead.
        Creates an Env value configured with the standard types, functions, and operators with a reference to the set of exprFeatures enabled in the environment.

        Note: standard declarations are configured in an isolated scope, and may be shadowed by subsequent declarations.

      • standard

        @Deprecated
        public static Env standard​(Errors errors,
                                   TypeProvider typeProvider,
                                   ExprFeatures... exprFeatures)
        Deprecated.
        use standard with CelOptions instead.
        Creates an Env value configured with the standard types, functions, and operators, configured with a custom typeProvider.

        Note: standard declarations are configured in an isolated scope, and may be shadowed by subsequent declarations with the same signature.

      • standard

        @Deprecated
        public static Env standard​(Errors errors,
                                   TypeProvider typeProvider,
                                   com.google.common.collect.ImmutableSet<ExprFeatures> exprFeatures)
        Deprecated.
        use standard with CelOptions instead.
        Creates an Env value configured with the standard types, functions, and operators, configured with a custom typeProvider and a reference to the set of exprFeatures enabled in the environment.

        Note: standard declarations are configured in an isolated scope, and may be shadowed by subsequent declarations with the same signature.

      • standard

        public static Env standard​(Errors errors,
                                   CelOptions celOptions)
        Creates an Env value configured with the standard types, functions, and operators and a reference to the configured celOptions.

        Note: standard declarations are configured in an isolated scope, and may be shadowed by subsequent declarations with the same signature.

      • standard

        public static Env standard​(Errors errors,
                                   TypeProvider typeProvider,
                                   CelOptions celOptions)
        Creates an Env value configured with the standard types, functions, and operators, configured with a custom typeProvider and a reference to the celOptions to use within the environment.

        Note: standard declarations are configured in an isolated scope, and may be shadowed by subsequent declarations with the same signature.

      • getErrorContext

        public Errors getErrorContext()
        Returns the current Errors object.
      • getTypeProvider

        public TypeProvider getTypeProvider()
        Returns the TypeProvider.
      • enterScope

        public void enterScope()
        Enters a new scope. All new declarations added to the environment exist only in this scope, and will shadow declarations of the same name in outer scopes. This includes overloads in outer scope (overloads from different scopes are not merged).
      • exitScope

        public void exitScope()
        Exits a previously opened scope, forgetting all declarations created in this scope.
      • scopeDepth

        public int scopeDepth()
        Return the current scope depth for the environment.
      • getDeclGroup

        public Env.DeclGroup getDeclGroup()
        Returns the top-most declaration scope.
      • getDeclGroup

        public Env.DeclGroup getDeclGroup​(int scopeDepth)
        Returns the DeclGroup at the given scopeDepth, where depth of 0 represents root scope.
      • resetTypeAndRefMaps

        public void resetTypeAndRefMaps()
        Reset type and ref maps. This must be called before type checking an expression.
      • getRefMap

        public java.util.Map<java.lang.Long,​CelReference> getRefMap()
        Returns the reference map.
      • getTypeMap

        public java.util.Map<java.lang.Long,​CelType> getTypeMap()
        Returns the type map.
      • getType

        @Deprecated
        public Type getType​(Expr expr)
        Deprecated.
        Use getType(CelExpr) instead.
        Returns the type associated with an expression by expression id. It's an error to call this method if the type is not present.
      • getType

        public CelType getType​(CelExpr expr)
        Returns the type associated with an expression by expression id. It's an error to call this method if the type is not present.
      • setType

        @CanIgnoreReturnValue
        public CelExpr setType​(CelExpr expr,
                               CelType type)
        Sets the type associated with an expression by id. It's an error if the type is already set and is different than the provided one. Returns the expression parameter.
      • setRef

        public void setRef​(CelExpr expr,
                           CelReference reference)
        Sets the reference associated with an expression. It's an error if the reference is already set and is different.
      • add

        @CanIgnoreReturnValue
        @Deprecated
        public Env add​(Decl decl)
        Deprecated.
        Migrate to the CEL-Java fluent APIs and leverage the publicly available native types (e.g: CelCompilerFactory accepts CelFunctionDecl and CelVarDecl).
        Adds a declaration to the environment, based on the Decl proto. Will report errors if the declaration overlaps with an existing one, or clashes with a macro.
      • add

        @CanIgnoreReturnValue
        public Env add​(CelIdentDecl celIdentDecl)
      • add

        @CanIgnoreReturnValue
        @Deprecated
        public Env add​(java.lang.String name,
                       Type type)
        Deprecated.
        Migrate to the CEL-Java fluent APIs and leverage the publicly available native types (e.g: CelCompilerFactory accepts CelFunctionDecl and CelVarDecl).
        Adds simple name declaration to the environment for a non-function.
      • tryLookupCelFunction

        public @Nullable CelFunctionDecl tryLookupCelFunction​(java.lang.String container,
                                                              java.lang.String name)
        Try to lookup a function with the given name within a container.

        For protos, the container may be a package or message name. The code tries to resolve the name first in the container, then within the container parent, and so on until the root package is reached. If container starts with ., the resolution is in the root container only.

        Returns null if the function cannot be found.

      • tryLookupCelIdent

        public @Nullable CelIdentDecl tryLookupCelIdent​(java.lang.String container,
                                                        java.lang.String name)
        Try to lookup an identifier with the given name within a container.

        For protos, the container may be a package or message name. The code tries to resolve the name first in the container, then within the container parent, and so on until the root package is reached. If container starts with ., the resolution is in the root container only.

        Returns null if the function cannot be found.

      • reportError

        public void reportError​(int position,
                                java.lang.String message,
                                java.lang.Object... args)
        Reports an error.