Class MapContext
java.lang.Object
cloud.opencode.base.expression.context.MapContext
- All Implemented Interfaces:
EvaluationContext
Map-based Evaluation Context
基于 Map 的求值上下文
A lightweight context that wraps a Map for simple variable-based expressions. The map values are treated as variables.
一个轻量级上下文,包装 Map 用于简单的基于变量的表达式。Map 值被视为变量。
Usage | 用法
Map<String, Object> vars = Map.of("name", "John", "age", 30);
MapContext ctx = new MapContext(vars);
Object result = OpenExpression.eval("name + ' is ' + age", ctx);
Features | 主要功能:
- Lightweight context backed by HashMap - 基于HashMap的轻量级上下文
- Map values treated as variables - Map值被视为变量
- Builder pattern for construction - 构建器模式用于构造
Security | 安全性:
- Thread-safe: No, uses HashMap internally - 线程安全: 否,内部使用HashMap
- Null-safe: Yes, null name ignored on setVariable - 空值安全: 是,setVariable中忽略null名称
Usage Examples | 使用示例:
MapContext ctx = new MapContext(Map.of("name", "John", "age", 30));
Object result = OpenExpression.eval("name + ' is ' + age", ctx); // "John is 30"
- Since:
- JDK 25, opencode-base-expression V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for MapContext MapContext 构建器 -
Constructor Summary
ConstructorsConstructorDescriptionCreate empty map context.MapContext(Map<String, Object> variables) Create map context with initial variables.MapContext(Map<String, Object> variables, FunctionRegistry functionRegistry, Sandbox sandbox) Create map context with full customization 使用完整自定义创建 Map 上下文 -
Method Summary
Modifier and TypeMethodDescriptionstatic MapContext.Builderbuilder()Create a builder for MapContext 创建 MapContext 的构建器Create child context 创建子上下文Get function registry 获取函数注册表Get property accessors 获取属性访问器Get the root object 获取根对象Get security sandbox 获取安全沙箱Get type converter 获取类型转换器getVariable(String name) Get variable value 获取变量值Get all variables 获取所有变量booleanhasVariable(String name) Check if variable exists 检查变量是否存在static MapContextCreate from map 从 Map 创建voidsetRootObject(Object root) Set the root object 设置根对象voidsetVariable(String name, Object value) Set variable value 设置变量值
-
Constructor Details
-
MapContext
public MapContext()Create empty map context. V1.0.4 sec round-5 P0: now installsDefaultSandbox.standard()by default — seeMapContext(java.util.Map, cloud.opencode.base.expression.function.FunctionRegistry, cloud.opencode.base.expression.sandbox.Sandbox)for rationale. 创建空 Map 上下文。V1.0.4 sec round-5 P0:默认装入DefaultSandbox.standard()。 -
MapContext
Create map context with initial variables. V1.0.4 sec round-5 P0: now installsDefaultSandbox.standard()by default — round-4 only fixedStandardContext, leavingMapContext/BeanContextas null-sandbox holes that the round-4MethodCallNodedeny-on-null change converted to immediateSandboxExceptionon every method invocation. Aligning the default closes both the legitimate-use breakage and the pre-deny-on-null RCE risk. 使用初始变量创建 Map 上下文。V1.0.4 sec round-5 P0:默认装入DefaultSandbox.standard()—— round-4 仅 修了StandardContext,MapContext/BeanContext仍是 null-sandbox 漏点; round-4 的MethodCallNodedeny-on-null 把该路径变成每次方法调用立即SandboxException。对齐默认值同时修复"合法使用被破坏"与 deny-on-null 之前的 RCE 风险。- Parameters:
variables- the variable map | 变量映射
-
MapContext
Create map context with full customization 使用完整自定义创建 Map 上下文- Parameters:
variables- the variable map | 变量映射functionRegistry- the function registry | 函数注册表sandbox- the security sandbox | 安全沙箱
-
-
Method Details
-
getRootObject
Description copied from interface:EvaluationContextGet the root object 获取根对象- Specified by:
getRootObjectin interfaceEvaluationContext- Returns:
- the root object | 根对象
-
setRootObject
Description copied from interface:EvaluationContextSet the root object 设置根对象- Specified by:
setRootObjectin interfaceEvaluationContext- Parameters:
root- the root object | 根对象
-
getVariable
Description copied from interface:EvaluationContextGet variable value 获取变量值- Specified by:
getVariablein interfaceEvaluationContext- Parameters:
name- the variable name | 变量名- Returns:
- the variable value | 变量值
-
setVariable
Description copied from interface:EvaluationContextSet variable value 设置变量值- Specified by:
setVariablein interfaceEvaluationContext- Parameters:
name- the variable name | 变量名value- the variable value | 变量值
-
hasVariable
Description copied from interface:EvaluationContextCheck if variable exists 检查变量是否存在- Specified by:
hasVariablein interfaceEvaluationContext- Parameters:
name- the variable name | 变量名- Returns:
- true if exists | 如果存在返回true
-
getVariables
Description copied from interface:EvaluationContextGet all variables 获取所有变量- Specified by:
getVariablesin interfaceEvaluationContext- Returns:
- the variable map | 变量映射
-
getFunctionRegistry
Description copied from interface:EvaluationContextGet function registry 获取函数注册表- Specified by:
getFunctionRegistryin interfaceEvaluationContext- Returns:
- the function registry | 函数注册表
-
getPropertyAccessors
Description copied from interface:EvaluationContextGet property accessors 获取属性访问器- Specified by:
getPropertyAccessorsin interfaceEvaluationContext- Returns:
- the property accessor list | 属性访问器列表
-
getTypeConverter
Description copied from interface:EvaluationContextGet type converter 获取类型转换器- Specified by:
getTypeConverterin interfaceEvaluationContext- Returns:
- the type converter | 类型转换器
-
getSandbox
Description copied from interface:EvaluationContextGet security sandbox 获取安全沙箱- Specified by:
getSandboxin interfaceEvaluationContext- Returns:
- the sandbox, or null if not configured | 沙箱,如果未配置则返回null
-
createChild
Description copied from interface:EvaluationContextCreate child context 创建子上下文Creates a child context that inherits from this context but can have its own variable bindings. Used for collection operations.
创建继承自此上下文但可以有自己变量绑定的子上下文。用于集合操作。
- Specified by:
createChildin interfaceEvaluationContext- Returns:
- the child context | 子上下文
-
builder
Create a builder for MapContext 创建 MapContext 的构建器- Returns:
- the builder | 构建器
-
of
Create from map 从 Map 创建- Parameters:
map- the variable map | 变量映射- Returns:
- the context | 上下文
-