Class BeanContext
java.lang.Object
cloud.opencode.base.expression.context.BeanContext
- All Implemented Interfaces:
EvaluationContext
Bean-based Evaluation Context
基于 Bean 的求值上下文
A context that wraps a Java bean as the root object. Properties of the bean can be accessed directly in expressions.
一个以 Java Bean 作为根对象的上下文。Bean 的属性可以在表达式中直接访问。
Usage | 用法
User user = new User("John", 30);
BeanContext ctx = new BeanContext(user);
Object name = OpenExpression.eval("name", ctx); // "John"
Object age = OpenExpression.eval("age", ctx); // 30
Features | 主要功能:
- Wraps a Java bean as root object for property access - 将Java Bean包装为根对象用于属性访问
- Built-in BeanPropertyAccessor for getter/field access - 内置BeanPropertyAccessor用于getter/字段访问
- Variable management with ConcurrentHashMap - 使用ConcurrentHashMap的变量管理
- Builder pattern for construction - 构建器模式用于构造
Security | 安全性:
- Thread-safe: Yes, ConcurrentHashMap for variables - 线程安全: 是,变量使用ConcurrentHashMap
- Null-safe: Yes, null name ignored on setVariable - 空值安全: 是,setVariable中忽略null名称
- Only public methods and fields accessible - 仅可访问公共方法和字段
Usage Examples | 使用示例:
User user = new User("John", 30);
BeanContext ctx = new BeanContext(user);
Object name = OpenExpression.eval("name", ctx); // "John"
Object age = OpenExpression.eval("age", ctx); // 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 BeanContext BeanContext 构建器 -
Constructor Summary
ConstructorsConstructorDescriptionBeanContext(Object rootObject) Create bean context with root object.BeanContext(Object rootObject, FunctionRegistry functionRegistry, Sandbox sandbox) Create bean context with full customization 使用完整自定义创建 Bean 上下文 -
Method Summary
Modifier and TypeMethodDescriptionstatic BeanContext.Builderbuilder()Create a builder for BeanContext 创建 BeanContext 的构建器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 BeanContextCreate from bean 从 Bean 创建voidsetRootObject(Object root) Set the root object 设置根对象voidsetVariable(String name, Object value) Set variable value 设置变量值
-
Constructor Details
-
BeanContext
Create bean context with root object. V1.0.4 sec round-5 P0: now installsDefaultSandbox.standard()by default — seeMapContext(java.util.Map)for full rationale (round-4 only fixedStandardContext, leavingMapContext/BeanContextas null-sandbox holes). 使用根对象创建 Bean 上下文。V1.0.4 sec round-5 P0:默认装入DefaultSandbox.standard(), 完整原因见MapContext(java.util.Map)。- Parameters:
rootObject- the root bean | 根 Bean
-
BeanContext
Create bean context with full customization 使用完整自定义创建 Bean 上下文- Parameters:
rootObject- the root bean | 根 BeanfunctionRegistry- 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 | 子上下文
-
of
Create from bean 从 Bean 创建- Parameters:
bean- the bean object | Bean 对象- Returns:
- the context | 上下文
-
builder
Create a builder for BeanContext 创建 BeanContext 的构建器- Returns:
- the builder | 构建器
-