Class StandardContext
- All Implemented Interfaces:
EvaluationContext
The default implementation of EvaluationContext with full feature support.
具有完整功能支持的EvaluationContext默认实现。
Features | 主要功能:
- Variable management with ConcurrentHashMap - 使用ConcurrentHashMap的变量管理
- Built-in Map and Bean property accessors - 内置Map和Bean属性访问器
- Parent context chaining for scoped evaluation - 父上下文链接用于作用域求值
- Configurable sandbox, type converter, and function registry - 可配置沙箱、类型转换器和函数注册表
- Builder pattern for construction - 构建器模式用于构造
Usage Examples | 使用示例:
StandardContext ctx = StandardContext.builder()
.rootObject(myBean)
.sandbox(DefaultSandbox.standard())
.build();
ctx.setVariable("discount", 0.1);
Object result = OpenExpression.eval("price * (1 - discount)", ctx);
Security | 安全性:
- Thread-safe: Yes, ConcurrentHashMap for variables - 线程安全: 是,变量使用ConcurrentHashMap
- Null-safe: Yes, null name/variable handled gracefully - 空值安全: 是,null名称/变量优雅处理
- 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 StandardContext StandardContext构建器 -
Constructor Summary
ConstructorsConstructorDescriptionCreate standard context with default settings.StandardContext(Object rootObject) Create standard context with root object.StandardContext(Object rootObject, FunctionRegistry functionRegistry, List<PropertyAccessor> propertyAccessors, TypeConverter typeConverter, Sandbox sandbox, EvaluationContext parent) Create standard context with full customization 使用完整自定义创建标准上下文 -
Method Summary
Modifier and TypeMethodDescriptionaddPropertyAccessor(PropertyAccessor accessor) Add a property accessor 添加属性访问器static StandardContext.Builderbuilder()Create a builder for StandardContext 创建StandardContext的构建器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 检查变量是否存在voidsetRootObject(Object root) Set the root object 设置根对象voidsetVariable(String name, Object value) Set variable value 设置变量值
-
Constructor Details
-
StandardContext
public StandardContext()Create standard context with default settings. 使用默认设置创建标准上下文。V1.0.4 sec round-4 P0: this constructor now installs
DefaultSandbox.standard()as the default sandbox. Pre-fix the default wasnull, which combined with a null-tolerantMethodCallNode.validateMethodAccesslet attacker-controlled expressions likectx.getClass().forName('java.lang.Runtime').getMethod('exec', String.class) .invoke(null, 'id')reach Runtime.exec — RCE. TheDefaultSandbox.standard()preset blocks theRuntime/ProcessBuilder/System/Thread/ClassLoaderclasses plus reflection methods, closing that chain. Callers that need a different policy can use the 6-arg constructor orDefaultSandbox.restrictive(); callers that genuinely need NO sandbox (test code only!) can passnullexplicitly. V1.0.4 sec round-4 P0:本构造器现在装入DefaultSandbox.standard()作默认 sandbox。 修复前默认为null,配合容忍 null 的MethodCallNode.validateMethodAccess让攻击者表达式如ctx.getClass().forName('java.lang.Runtime').getMethod('exec', String.class) .invoke(null, 'id')可达 Runtime.exec —— RCE。DefaultSandbox.standard()阻断Runtime/ProcessBuilder/System/Thread/ClassLoader类与反射方法,关闭此链。需要其他策略请用 6 参构造器或DefaultSandbox.restrictive(); 确实需要无 sandbox 的(仅测试代码!)可显式传null。 -
StandardContext
Create standard context with root object. Same default sandbox asStandardContext(). 使用根对象创建标准上下文。默认 sandbox 与StandardContext()一致。- Parameters:
rootObject- the root object | 根对象
-
StandardContext
public StandardContext(Object rootObject, FunctionRegistry functionRegistry, List<PropertyAccessor> propertyAccessors, TypeConverter typeConverter, Sandbox sandbox, EvaluationContext parent) Create standard context with full customization 使用完整自定义创建标准上下文- Parameters:
rootObject- the root object | 根对象functionRegistry- the function registry | 函数注册表propertyAccessors- the property accessors | 属性访问器typeConverter- the type converter | 类型转换器sandbox- the security sandbox | 安全沙箱parent- the parent context | 父上下文
-
-
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 | 子上下文
-
addPropertyAccessor
Add a property accessor 添加属性访问器- Parameters:
accessor- the accessor | 访问器- Returns:
- this context for chaining | 用于链式调用的上下文
-
builder
Create a builder for StandardContext 创建StandardContext的构建器- Returns:
- the builder | 构建器
-