Class Env
- java.lang.Object
-
- dev.cel.checker.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classEnv.DeclGroupObject for managing a group of declarations within a scope.static classEnv.FunctionBuilderDeprecated.UseCelFunctionDecl.newBuilder()instead.static classEnv.IdentBuilderDeprecated.UseCelVarDecl#newBuilder()instead.
-
Field Summary
Fields Modifier and Type Field Description static CelFunctionDeclERROR_FUNCTION_DECLA function declaration to represent an error.static CelIdentDeclERROR_IDENT_DECLAn ident declaration to represent an error.static intROOT_SCOPEThe top-most scope in the environment, for use withgetDeclGroup(int).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Envadd(CelIdentDecl celIdentDecl)Envadd(CelFunctionDecl celFunctionDecl)Envadd(Decl decl)Deprecated.Migrate to the CEL-Java fluent APIs and leverage the publicly available native types (e.g:CelCompilerFactoryacceptsCelFunctionDeclandCelVarDecl).Envadd(java.lang.String name, Type type)Deprecated.Migrate to the CEL-Java fluent APIs and leverage the publicly available native types (e.g:CelCompilerFactoryacceptsCelFunctionDeclandCelVarDecl).voidenterScope()Enters a new scope.voidexitScope()Exits a previously opened scope, forgetting all declarations created in this scope.Env.DeclGroupgetDeclGroup()Returns the top-most declaration scope.Env.DeclGroupgetDeclGroup(int scopeDepth)Returns theDeclGroupat the givenscopeDepth, where depth of0represents root scope.ErrorsgetErrorContext()Returns the current Errors object.java.util.Map<java.lang.Long,CelReference>getRefMap()Returns the reference map.CelTypegetType(CelExpr expr)Returns the type associated with an expression by expression id.TypegetType(Expr expr)Deprecated.UsegetType(CelExpr)instead.java.util.Map<java.lang.Long,CelType>getTypeMap()Returns the type map.TypeProvidergetTypeProvider()Returns theTypeProvider.CelFunctionDecllookupFunction(int position, java.lang.String inContainer, java.lang.String name)Lookup a name liketryLookupCelFunction(java.lang.String, java.lang.String)but report an error if the name is not found and return theERROR_FUNCTION_DECL.CelIdentDecllookupIdent(int position, java.lang.String inContainer, java.lang.String name)Lookup a name liketryLookupCelIdent(java.lang.String, java.lang.String), but report an error if the name is not found and return theERROR_IDENT_DECL.voidreportError(int position, java.lang.String message, java.lang.Object... args)Reports an error.voidresetTypeAndRefMaps()Reset type and ref maps.intscopeDepth()Return the current scope depth for the environment.voidsetRef(CelExpr expr, CelReference reference)Sets the reference associated with an expression.CelExprsetType(CelExpr expr, CelType type)Sets the type associated with an expression by id.static Envstandard(Errors errors, TypeProvider typeProvider, com.google.common.collect.ImmutableSet<ExprFeatures> exprFeatures)Deprecated.usestandardwithCelOptionsinstead.static Envstandard(Errors errors, TypeProvider typeProvider, CelOptions celOptions)Creates anEnvvalue configured with the standard types, functions, and operators, configured with a customtypeProviderand a reference to thecelOptionsto use within the environment.static Envstandard(Errors errors, TypeProvider typeProvider, ExprFeatures... exprFeatures)Deprecated.usestandardwithCelOptionsinstead.static Envstandard(Errors errors, CelOptions celOptions)Creates anEnvvalue configured with the standard types, functions, and operators and a reference to the configuredcelOptions.static Envstandard(Errors errors, ExprFeatures... exprFeatures)Deprecated.usestandardwithCelOptionsinstead.@Nullable CelFunctionDecltryLookupCelFunction(java.lang.String container, java.lang.String name)Try to lookup a function with the givennamewithin acontainer.@Nullable CelIdentDecltryLookupCelIdent(java.lang.String container, java.lang.String name)Try to lookup an identifier with the givennamewithin acontainer.@Nullable DecltryLookupFunction(java.lang.String container, java.lang.String name)Deprecated.@Nullable DecltryLookupIdent(java.lang.String container, java.lang.String name)Deprecated.static Envunconfigured(Errors errors, TypeProvider typeProvider, com.google.common.collect.ImmutableSet<ExprFeatures> exprFeatures)Deprecated.useunconfiguredwithCelOptionsinstead.static Envunconfigured(Errors errors, TypeProvider typeProvider, CelOptions celOptions)Creates an unconfiguredEnvvalue without the standard CEL types, functions, and operators using a customtypeProvider.static Envunconfigured(Errors errors, TypeProvider typeProvider, ExprFeatures... exprFeatures)Deprecated.useunconfiguredwithCelOptionsinstead.static Envunconfigured(Errors errors, CelOptions celOptions)Creates an unconfiguredEnvvalue without the standard CEL types, functions, and operators with a reference to the configuredcelOptions.static Envunconfigured(Errors errors, ExprFeatures... exprFeatures)Deprecated.useunconfiguredwithCelOptionsinstead.
-
-
-
Field Detail
-
ROOT_SCOPE
public static final int ROOT_SCOPE
The top-most scope in the environment, for use withgetDeclGroup(int).- See Also:
- Constant Field Values
-
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.useunconfiguredwithCelOptionsinstead.Creates an unconfiguredEnvvalue 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.useunconfiguredwithCelOptionsinstead.Creates an unconfiguredEnvvalue without the standard CEL types, functions, and operators using a customtypeProvider.
-
unconfigured
@Deprecated public static Env unconfigured(Errors errors, TypeProvider typeProvider, com.google.common.collect.ImmutableSet<ExprFeatures> exprFeatures)
Deprecated.useunconfiguredwithCelOptionsinstead.Creates an unconfiguredEnvvalue without the standard CEL types, functions, and operators using a customtypeProvider. The set of enabledexprFeaturesis also provided.
-
unconfigured
public static Env unconfigured(Errors errors, CelOptions celOptions)
Creates an unconfiguredEnvvalue without the standard CEL types, functions, and operators with a reference to the configuredcelOptions.
-
unconfigured
public static Env unconfigured(Errors errors, TypeProvider typeProvider, CelOptions celOptions)
Creates an unconfiguredEnvvalue without the standard CEL types, functions, and operators using a customtypeProvider. TheCelOptionsare provided as well.
-
standard
@Deprecated public static Env standard(Errors errors, ExprFeatures... exprFeatures)
Deprecated.usestandardwithCelOptionsinstead.Creates anEnvvalue configured with the standard types, functions, and operators with a reference to the set ofexprFeaturesenabled 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.usestandardwithCelOptionsinstead.Creates anEnvvalue configured with the standard types, functions, and operators, configured with a customtypeProvider.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.usestandardwithCelOptionsinstead.Creates anEnvvalue configured with the standard types, functions, and operators, configured with a customtypeProviderand a reference to the set ofexprFeaturesenabled 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 anEnvvalue configured with the standard types, functions, and operators and a reference to the configuredcelOptions.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 anEnvvalue configured with the standard types, functions, and operators, configured with a customtypeProviderand a reference to thecelOptionsto 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 theTypeProvider.
-
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 theDeclGroupat the givenscopeDepth, where depth of0represents 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.UsegetType(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:CelCompilerFactoryacceptsCelFunctionDeclandCelVarDecl).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(CelFunctionDecl celFunctionDecl)
-
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:CelCompilerFactoryacceptsCelFunctionDeclandCelVarDecl).Adds simple name declaration to the environment for a non-function.
-
tryLookupFunction
@Deprecated public @Nullable Decl tryLookupFunction(java.lang.String container, java.lang.String name)
Deprecated.
-
tryLookupCelFunction
public @Nullable CelFunctionDecl tryLookupCelFunction(java.lang.String container, java.lang.String name)
Try to lookup a function with the givennamewithin acontainer.For protos, the
containermay be a package or message name. The code tries to resolve thenamefirst in the container, then within the container parent, and so on until the root package is reached. Ifcontainerstarts with., the resolution is in the root container only.Returns
nullif the function cannot be found.
-
tryLookupIdent
@Deprecated public @Nullable Decl tryLookupIdent(java.lang.String container, java.lang.String name)
Deprecated.
-
tryLookupCelIdent
public @Nullable CelIdentDecl tryLookupCelIdent(java.lang.String container, java.lang.String name)
Try to lookup an identifier with the givennamewithin acontainer.For protos, the
containermay be a package or message name. The code tries to resolve thenamefirst in the container, then within the container parent, and so on until the root package is reached. Ifcontainerstarts with., the resolution is in the root container only.Returns
nullif the function cannot be found.
-
lookupIdent
public CelIdentDecl lookupIdent(int position, java.lang.String inContainer, java.lang.String name)
Lookup a name liketryLookupCelIdent(java.lang.String, java.lang.String), but report an error if the name is not found and return theERROR_IDENT_DECL.
-
lookupFunction
public CelFunctionDecl lookupFunction(int position, java.lang.String inContainer, java.lang.String name)
Lookup a name liketryLookupCelFunction(java.lang.String, java.lang.String)but report an error if the name is not found and return theERROR_FUNCTION_DECL.
-
reportError
public void reportError(int position, java.lang.String message, java.lang.Object... args)Reports an error.
-
-