Class OpenBean

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

public final class OpenBean extends Object
Bean Utility Class - JavaBean property operations and conversions Bean 工具类 - JavaBean 属性操作和转换

Provides comprehensive bean operations including copy, conversion, and comparison.

提供完整的 Bean 操作,包括复制、转换和比较。

Features | 主要功能:

  • Property copy (with mapping, converter, ignore) - 属性复制(支持映射、转换器、忽略)
  • Bean/Map conversion (camelCase/underline) - Bean/Map 转换(驼峰/下划线)
  • Property get/set with type conversion - 属性读写(带类型转换)
  • Bean comparison and diff - Bean 比较和差异
  • Record support (to/from) - Record 支持

Usage Examples | 使用示例:

// Copy properties - 复制属性
OpenBean.copyProperties(source, target);
User copy = OpenBean.copyToNew(source, User.class);

// Bean to/from Map - Bean 与 Map 转换
Map<String, Object> map = OpenBean.toMap(user);
User user = OpenBean.toBean(map, User.class);

// Property access - 属性访问
String name = OpenBean.getProperty(user, "name", String.class);
OpenBean.setProperty(user, "name", "Leon");

Security | 安全性:

  • Thread-safe: Yes (ConcurrentHashMap cache) - 线程安全: 是 (ConcurrentHashMap 缓存)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • copyProperties

      public static void copyProperties(Object source, Object target)
      Copies properties with matching names and types 复制属性(同名同类型属性)
    • copyProperties

      public static void copyProperties(Object source, Object target, String... ignoreProperties)
      Copies properties with ignored properties 复制属性(带忽略属性)
    • copyProperties

      public static void copyProperties(Object source, Object target, Map<String,String> propertyMapping)
      Copies properties with property name mapping 复制属性(带属性映射)
    • copyProperties

      public static void copyProperties(Object source, Object target, PropertyConverter converter)
      Copies properties with a converter 复制属性(带转换器)
    • copyToNew

      public static <T> T copyToNew(Object source, Class<T> targetClass)
      Copies to a new object 复制到新对象
    • copyToNew

      public static <T> T copyToNew(Object source, Class<T> targetClass, String... ignoreProperties)
      Copies to a new object with ignored properties 复制到新对象(带忽略属性)
    • deepCopyProperties

      public static void deepCopyProperties(Object source, Object target)
      Deep copies properties including nested objects 深度复制属性(包括嵌套对象)
    • toMap

      public static Map<String,Object> toMap(Object bean)
      Converts a Bean to a Map Bean 转 Map
    • toMap

      public static Map<String,Object> toMap(Object bean, String... ignoreProperties)
      Converts a Bean to a Map with ignored properties Bean 转 Map(带忽略属性)
    • toMapNonNull

      public static Map<String,Object> toMapNonNull(Object bean)
      Converts a Bean to a Map with only non-null properties Bean 转 Map(仅包含非空属性)
    • toUnderlineKeyMap

      public static Map<String,Object> toUnderlineKeyMap(Object bean)
      Converts a Bean to a Map with underline keys Bean 转 Map(驼峰转下划线 key)
    • toBean

      public static <T> T toBean(Map<String,?> map, Class<T> beanClass)
      Converts a Map to a Bean Map 转 Bean
    • toBean

      public static <T> T toBean(Map<String,?> map, Class<T> beanClass, Map<String,String> propertyMapping)
      Converts a Map to a Bean with property name mapping Map 转 Bean(带属性映射)
    • toBeanFromUnderlineKey

      public static <T> T toBeanFromUnderlineKey(Map<String,?> map, Class<T> beanClass)
      Converts a Map with underline keys to a Bean Map 转 Bean(下划线 key 转驼峰)
    • getProperty

      public static Object getProperty(Object bean, String propertyName)
      Gets a property value 获取属性值
    • getProperty

      public static <T> T getProperty(Object bean, String propertyName, Class<T> targetType)
      Gets a property value with type conversion 获取属性值(带类型转换)
    • getPropertyOptional

      public static <T> Optional<T> getPropertyOptional(Object bean, String propertyName, Class<T> targetType)
      Safely gets a property value returning an Optional 安全获取属性值(返回 Optional)
    • setProperty

      public static void setProperty(Object bean, String propertyName, Object value)
      Sets a property value 设置属性值
    • setPropertyWithConvert

      public static void setPropertyWithConvert(Object bean, String propertyName, Object value)
      Sets a property value with type conversion 设置属性值(带类型转换)
    • setProperties

      public static void setProperties(Object bean, Map<String,?> properties)
      Sets multiple properties in batch 批量设置属性
    • getPropertyDescriptors

      public static List<PropertyDescriptor> getPropertyDescriptors(Class<?> beanClass)
      Gets all property descriptors 获取所有属性描述符
    • getPropertyDescriptor

      public static Optional<PropertyDescriptor> getPropertyDescriptor(Class<?> beanClass, String propertyName)
      Gets the specified property descriptor 获取指定属性描述符
    • getPropertyNames

      public static List<String> getPropertyNames(Class<?> beanClass)
      Gets all property names 获取所有属性名
    • getReadablePropertyNames

      public static List<String> getReadablePropertyNames(Class<?> beanClass)
      Gets all readable property names 获取所有可读属性名
    • getWritablePropertyNames

      public static List<String> getWritablePropertyNames(Class<?> beanClass)
      Gets all writable property names 获取所有可写属性名
    • hasProperty

      public static boolean hasProperty(Class<?> beanClass, String propertyName)
      Checks whether a property exists 检查属性是否存在
    • getPropertyType

      public static Class<?> getPropertyType(Class<?> beanClass, String propertyName)
      Gets the property type 获取属性类型
    • equals

      public static boolean equals(Object bean1, Object bean2)
      Compares two Beans for equality on all properties 比较两个 Bean 是否相等(所有属性)
    • equals

      public static boolean equals(Object bean1, Object bean2, String... properties)
      Compares two Beans for equality on specified properties 比较两个 Bean 是否相等(指定属性)
    • diff

      public static Map<String,Object[]> diff(Object bean1, Object bean2)
      Gets the differing properties between two Beans 获取两个 Bean 的差异属性
    • diff

      public static Map<String,Object[]> diff(Object bean1, Object bean2, String... properties)
      Gets the differing properties between two Beans for specified properties 获取两个 Bean 的差异属性(指定属性)
    • isEmpty

      public static boolean isEmpty(Object bean)
      Checks whether the Bean is empty (all properties are null or primitive defaults) 检查是否为空 Bean(所有属性为 null 或基本类型默认值)
    • hasNonNullProperty

      public static boolean hasNonNullProperty(Object bean)
      Checks whether there is any non-null property 检查是否有任意非空属性
    • getNonNullPropertyNames

      public static List<String> getNonNullPropertyNames(Object bean)
      Gets all non-null property names 获取所有非空属性名
    • fromRecord

      public static <T> T fromRecord(Record record, Class<T> beanClass)
      Converts a Record to a Bean Record 转 Bean
    • toRecord

      public static <T extends Record> T toRecord(Object bean, Class<T> recordClass)
      Converts a Bean to a Record Bean 转 Record