Class OpenBean

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

public final class OpenBean extends Object
Bean Facade Entry Class Bean门面入口类

Provides common bean operations API.

提供常用bean操作API。

Features | 主要功能:

  • Property discovery - 属性发现
  • Property access - 属性访问
  • Bean copying - Bean复制
  • Bean to Map conversion - Bean转Map

Usage Examples | 使用示例:

// Get property descriptors
Map<String, PropertyDescriptor> props = OpenBean.getPropertyDescriptors(User.class);

// Copy properties
OpenBean.copyProperties(source, target);

// Bean to Map
Map<String, Object> map = OpenBean.toMap(user);

Security | 安全性:

  • Thread-safe: Yes (uses ConcurrentHashMap for caching) - 线程安全: 是(使用ConcurrentHashMap缓存)
  • Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getPropertyDescriptors

      public static Map<String, PropertyDescriptor> getPropertyDescriptors(Class<?> clazz)
      Gets all property descriptors for a class 获取类的所有属性描述符
      Parameters:
      clazz - the class | 类
      Returns:
      map of property name to descriptor | 属性名到描述符的映射
    • getPropertyDescriptor

      public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName)
      Gets a property descriptor by name 按名称获取属性描述符
      Parameters:
      clazz - the class | 类
      propertyName - the property name | 属性名
      Returns:
      the descriptor or null | 描述符或null
    • getPropertyNames

      public static Set<String> getPropertyNames(Class<?> clazz)
      Gets all property names 获取所有属性名
      Parameters:
      clazz - the class | 类
      Returns:
      set of property names | 属性名集合
    • getReadablePropertyNames

      public static Set<String> getReadablePropertyNames(Class<?> clazz)
      Gets readable property names 获取可读属性名
      Parameters:
      clazz - the class | 类
      Returns:
      set of readable property names | 可读属性名集合
    • getWritablePropertyNames

      public static Set<String> getWritablePropertyNames(Class<?> clazz)
      Gets writable property names 获取可写属性名
      Parameters:
      clazz - the class | 类
      Returns:
      set of writable property names | 可写属性名集合
    • getProperty

      public static Object getProperty(Object bean, String propertyName)
      Gets a property value 获取属性值
      Parameters:
      bean - the bean | bean
      propertyName - the property name | 属性名
      Returns:
      the value | 值
    • getProperty

      public static <T> T getProperty(Object bean, String propertyName, Class<T> type)
      Gets a property value with type 获取属性值(带类型)
      Type Parameters:
      T - the value type | 值类型
      Parameters:
      bean - the bean | bean
      propertyName - the property name | 属性名
      type - the expected type | 期望类型
      Returns:
      the value | 值
    • setProperty

      public static void setProperty(Object bean, String propertyName, Object value)
      Sets a property value 设置属性值
      Parameters:
      bean - the bean | bean
      propertyName - the property name | 属性名
      value - the value | 值
    • setPropertyIfWritable

      public static boolean setPropertyIfWritable(Object bean, String propertyName, Object value)
      Sets a property value if writable 如果可写则设置属性值
      Parameters:
      bean - the bean | bean
      propertyName - the property name | 属性名
      value - the value | 值
      Returns:
      true if set | 如果已设置返回true
    • copyProperties

      public static void copyProperties(Object source, Object target)
      Copies properties from source to target 从源复制属性到目标
      Parameters:
      source - the source bean | 源bean
      target - the target bean | 目标bean
    • copyProperties

      public static void copyProperties(Object source, Object target, String... excludeProperties)
      Copies properties excluding specified ones 复制属性(排除指定属性)
      Parameters:
      source - the source bean | 源bean
      target - the target bean | 目标bean
      excludeProperties - properties to exclude | 要排除的属性
    • copyProperties

      public static <T> T copyProperties(Object source, Class<T> targetClass)
      Copies and creates a new instance 复制并创建新实例
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      source - the source bean | 源bean
      targetClass - the target class | 目标类
      Returns:
      the new instance | 新实例
    • toMap

      public static Map<String,Object> toMap(Object bean)
      Converts a bean to a Map 将bean转换为Map
      Parameters:
      bean - the bean | bean
      Returns:
      the map | 映射
    • asBeanMap

      public static <T> BeanMap<T> asBeanMap(T bean)
      Creates a BeanMap view 创建BeanMap视图
      Type Parameters:
      T - the bean type | bean类型
      Parameters:
      bean - the bean | bean
      Returns:
      the BeanMap | BeanMap
    • populate

      public static void populate(Object bean, Map<String,?> map)
      Populates a bean from a Map 从Map填充bean
      Parameters:
      bean - the bean | bean
      map - the source map | 源映射
    • fromMap

      public static <T> T fromMap(Map<String,?> map, Class<T> clazz)
      Creates a bean from a Map 从Map创建bean
      Type Parameters:
      T - the bean type | bean类型
      Parameters:
      map - the source map | 源映射
      clazz - the bean class | bean类
      Returns:
      the bean | bean
    • createCopier

      public static <S,T> BeanCopier<S,T> createCopier(Class<S> sourceClass, Class<T> targetClass)
      Creates a BeanCopier 创建BeanCopier
      Type Parameters:
      S - the source type | 源类型
      T - the target type | 目标类型
      Parameters:
      sourceClass - the source class | 源类
      targetClass - the target class | 目标类
      Returns:
      the BeanCopier | BeanCopier
    • hasProperty

      public static boolean hasProperty(Class<?> clazz, String propertyName)
      Checks if bean has a property 检查bean是否有属性
      Parameters:
      clazz - the class | 类
      propertyName - the property name | 属性名
      Returns:
      true if has property | 如果有属性返回true
    • clearCache

      public static void clearCache(Class<?> clazz)
      Clears the descriptor cache for a class 清除类的描述符缓存
      Parameters:
      clazz - the class | 类
    • clearAllCache

      public static void clearAllCache()
      Clears all descriptor cache 清除所有描述符缓存