Class BeanPath

java.lang.Object
cloud.opencode.base.core.bean.BeanPath

public final class BeanPath extends Object
Bean Path Utility Class - Nested property path access Bean 路径访问工具 - 嵌套属性路径访问

Supports nested property access like user.address.city and array/List index access like items[0].name.

支持嵌套属性访问(如 user.address.city)和数组/List 索引访问(如 items[0].name)。

Features | 主要功能:

  • Nested property access (user.address.city) - 嵌套属性访问
  • Array/List index access (items[0]) - 数组/List 索引访问
  • Map key access (map[key]) - Map 键访问
  • Path validation and existence check - 路径验证和存在性检查
  • Auto-create intermediate objects - 自动创建中间对象

Usage Examples | 使用示例:

// Get nested value - 获取嵌套值
String city = BeanPath.get(user, "address.city", String.class);

// Get array element - 获取数组元素
Item item = BeanPath.get(order, "items[0]", Item.class);

// Set with auto-create - 自动创建设置
BeanPath.setWithCreate(user, "address.city", "Beijing");

// Check path exists - 检查路径存在
boolean exists = BeanPath.exists(user, "address.city");

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
  • Null-safe: Yes (returns null on null path) - 空值安全: 是
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • get

      public static Object get(Object bean, String path)
      Gets a property value by path | 通过路径获取属性值
      Parameters:
      bean - the target bean | 目标 Bean
      path - the property path | 属性路径
      Returns:
      the value at path, or null | 路径上的值,或 null
    • get

      public static <T> T get(Object bean, String path, Class<T> targetType)
      Gets a property value by path with type conversion | 通过路径获取属性值(带类型转换)
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      bean - the target bean | 目标 Bean
      path - the property path | 属性路径
      targetType - the target class | 目标类
      Returns:
      the converted value | 转换后的值
    • getOptional

      public static <T> Optional<T> getOptional(Object bean, String path, Class<T> targetType)
      Safely gets the value as Optional | 安全获取(返回 Optional)
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      bean - the target bean | 目标 Bean
      path - the property path | 属性路径
      targetType - the target class | 目标类
      Returns:
      an Optional containing the value | 包含值的 Optional
    • set

      public static void set(Object bean, String path, Object value)
      Sets a property value by path | 通过路径设置属性值
      Parameters:
      bean - the target bean | 目标 Bean
      path - the property path | 属性路径
      value - the value to set | 要设置的值
    • setWithCreate

      public static void setWithCreate(Object bean, String path, Object value)
      Sets a property value by path, auto-creating intermediate objects | 通过路径设置属性值(自动创建中间对象)
      Parameters:
      bean - the target bean | 目标 Bean
      path - the property path | 属性路径
      value - the value to set | 要设置的值
    • exists

      public static boolean exists(Object bean, String path)
      Checks if the path exists | 检查路径是否存在
      Parameters:
      bean - the target bean | 目标 Bean
      path - the property path | 属性路径
      Returns:
      true if the path exists | 路径存在返回 true
    • isNull

      public static boolean isNull(Object bean, String path)
      Checks if the value at path is null | 检查路径值是否为 null
      Parameters:
      bean - the target bean | 目标 Bean
      path - the property path | 属性路径
      Returns:
      true if value is null | 值为 null 返回 true
    • isNotNull

      public static boolean isNotNull(Object bean, String path)
      Checks if the value at path is not null | 检查路径值是否非 null
      Parameters:
      bean - the target bean | 目标 Bean
      path - the property path | 属性路径
      Returns:
      true if value is not null | 值非 null 返回 true
    • parsePath

      public static List<BeanPath.PathSegment> parsePath(String path)
      Parses a path string into segments | 解析路径为段列表
      Parameters:
      path - the property path | 属性路径
      Returns:
      the list of path segments | 路径段列表
    • getParentPath

      public static String getParentPath(String path)
      Gets the parent path | 获取路径的父路径
      Parameters:
      path - the property path | 属性路径
      Returns:
      the parent path | 父路径
    • getLastSegment

      public static String getLastSegment(String path)
      Gets the last segment of the path | 获取路径的最后一段
      Parameters:
      path - the property path | 属性路径
      Returns:
      the last segment | 最后一段
    • joinPath

      public static String joinPath(String... segments)
      Concatenates path segments | 拼接路径
      Parameters:
      segments - the path segments | 路径段
      Returns:
      the joined path | 拼接后的路径