Interface Expression

All Known Implementing Classes:
CompiledExpression

public interface Expression
Expression Interface 表达式接口

Represents a parsed expression that can be evaluated.

表示可以求值的已解析表达式。

Features | 主要功能:

  • Evaluate expressions with or without context - 支持有无上下文的表达式求值
  • Type-safe evaluation with automatic conversion - 类型安全求值与自动转换
  • Root object binding for property access - 根对象绑定用于属性访问
  • Optional writable expressions - 可选的可写表达式

Usage Examples | 使用示例:

Expression expr = OpenExpression.parse("1 + 2 * 3");
Object result = expr.getValue();  // 7

// With type conversion
int typed = expr.getValue(Integer.class);  // 7

// With context
StandardContext ctx = new StandardContext();
ctx.setVariable("x", 10);
Expression expr2 = OpenExpression.parse("x + 5");
Object result2 = expr2.getValue(ctx);  // 15

Security | 安全性:

  • Thread-safe: Depends on implementation - 线程安全: 取决于实现
  • Null-safe: Yes, null context creates default - 空值安全: 是,null上下文创建默认值
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Get the expression string 获取表达式字符串
    Evaluate the expression 求值表达式
    Evaluate the expression with context 使用上下文求值表达式
    <T> T
    getValue(EvaluationContext context, Class<T> targetType)
    Evaluate the expression with context and convert to type 使用上下文求值表达式并转换为指定类型
    <T> T
    getValue(Class<T> targetType)
    Evaluate the expression and convert to type 求值表达式并转换为指定类型
    getValue(Object rootObject)
    Evaluate the expression with root object 使用根对象求值表达式
    <T> T
    getValue(Object rootObject, Class<T> targetType)
    Evaluate the expression with root object and convert to type 使用根对象求值表达式并转换为指定类型
    Get the value type 获取值类型
    Get the value type with context 使用上下文获取值类型
    default boolean
    Check if expression is writable 检查表达式是否可写
    default void
    Set the value of the expression 设置表达式的值
  • Method Details

    • getExpressionString

      String getExpressionString()
      Get the expression string 获取表达式字符串
      Returns:
      the expression string | 表达式字符串
    • getValue

      Object getValue()
      Evaluate the expression 求值表达式
      Returns:
      the result | 结果
    • getValue

      Object getValue(EvaluationContext context)
      Evaluate the expression with context 使用上下文求值表达式
      Parameters:
      context - the evaluation context | 求值上下文
      Returns:
      the result | 结果
    • getValue

      <T> T getValue(Class<T> targetType)
      Evaluate the expression and convert to type 求值表达式并转换为指定类型
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      targetType - the target type | 目标类型
      Returns:
      the typed result | 类型化结果
    • getValue

      <T> T getValue(EvaluationContext context, Class<T> targetType)
      Evaluate the expression with context and convert to type 使用上下文求值表达式并转换为指定类型
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      context - the evaluation context | 求值上下文
      targetType - the target type | 目标类型
      Returns:
      the typed result | 类型化结果
    • getValue

      Object getValue(Object rootObject)
      Evaluate the expression with root object 使用根对象求值表达式
      Parameters:
      rootObject - the root object | 根对象
      Returns:
      the result | 结果
    • getValue

      <T> T getValue(Object rootObject, Class<T> targetType)
      Evaluate the expression with root object and convert to type 使用根对象求值表达式并转换为指定类型
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      rootObject - the root object | 根对象
      targetType - the target type | 目标类型
      Returns:
      the typed result | 类型化结果
    • getValueType

      Class<?> getValueType()
      Get the value type 获取值类型
      Returns:
      the value type | 值类型
    • getValueType

      Class<?> getValueType(EvaluationContext context)
      Get the value type with context 使用上下文获取值类型
      Parameters:
      context - the evaluation context | 求值上下文
      Returns:
      the value type | 值类型
    • isWritable

      default boolean isWritable()
      Check if expression is writable 检查表达式是否可写
      Returns:
      true if writable | 如果可写返回true
    • setValue

      default void setValue(EvaluationContext context, Object value)
      Set the value of the expression 设置表达式的值
      Parameters:
      context - the evaluation context | 求值上下文
      value - the value to set | 要设置的值