Class OpenBean
java.lang.Object
cloud.opencode.base.core.bean.OpenBean
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 Summary
Modifier and TypeMethodDescriptionstatic voidcopyProperties(Object source, Object target) Copies properties with matching names and types 复制属性(同名同类型属性)static voidcopyProperties(Object source, Object target, PropertyConverter converter) Copies properties with a converter 复制属性(带转换器)static voidcopyProperties(Object source, Object target, String... ignoreProperties) Copies properties with ignored properties 复制属性(带忽略属性)static voidCopies properties with property name mapping 复制属性(带属性映射)static <T> TCopies to a new object 复制到新对象static <T> TCopies to a new object with ignored properties 复制到新对象(带忽略属性)static voiddeepCopyProperties(Object source, Object target) Deep copies properties including nested objects.Gets the differing properties between two Beans 获取两个 Bean 的差异属性Gets the differing properties between two Beans for specified properties 获取两个 Bean 的差异属性(指定属性)static booleanCompares two Beans for equality on all properties 比较两个 Bean 是否相等(所有属性)static booleanCompares two Beans for equality on specified properties 比较两个 Bean 是否相等(指定属性)static <T> TfromRecord(Record record, Class<T> beanClass) Converts a Record to a Bean Record 转 BeanGets all non-null property names 获取所有非空属性名static ObjectgetProperty(Object bean, String propertyName) Gets a property value 获取属性值static <T> TgetProperty(Object bean, String propertyName, Class<T> targetType) Gets a property value with type conversion 获取属性值(带类型转换)static Optional<PropertyDescriptor> getPropertyDescriptor(Class<?> beanClass, String propertyName) Gets the specified property descriptor 获取指定属性描述符static List<PropertyDescriptor> getPropertyDescriptors(Class<?> beanClass) Gets all property descriptors 获取所有属性描述符getPropertyNames(Class<?> beanClass) Gets all property names 获取所有属性名static <T> Optional<T> getPropertyOptional(Object bean, String propertyName, Class<T> targetType) Safely gets a property value returning an Optional 安全获取属性值(返回 Optional)static Class<?> getPropertyType(Class<?> beanClass, String propertyName) Gets the property type 获取属性类型getReadablePropertyNames(Class<?> beanClass) Gets all readable property names 获取所有可读属性名getWritablePropertyNames(Class<?> beanClass) Gets all writable property names 获取所有可写属性名static booleanhasNonNullProperty(Object bean) Checks whether there is any non-null property 检查是否有任意非空属性static booleanhasProperty(Class<?> beanClass, String propertyName) Checks whether a property exists 检查属性是否存在static booleanChecks whether the Bean is empty (all properties are null or primitive defaults) 检查是否为空 Bean(所有属性为 null 或基本类型默认值)static voidsetProperties(Object bean, Map<String, ?> properties) Sets multiple properties in batch 批量设置属性static voidsetProperty(Object bean, String propertyName, Object value) Sets a property value 设置属性值static voidsetPropertyWithConvert(Object bean, String propertyName, Object value) Sets a property value with type conversion 设置属性值(带类型转换)static <T> TConverts a Map to a Bean Map 转 Beanstatic <T> TConverts a Map to a Bean with property name mapping Map 转 Bean(带属性映射)static <T> TtoBeanFromUnderlineKey(Map<String, ?> map, Class<T> beanClass) Converts a Map with underline keys to a Bean Map 转 Bean(下划线 key 转驼峰)Converts a Bean to a Map Bean 转 MapConverts a Bean to a Map with ignored properties Bean 转 Map(带忽略属性)toMapNonNull(Object bean) Converts a Bean to a Map with only non-null properties Bean 转 Map(仅包含非空属性)static <T extends Record>
TConverts a Bean to a Record Bean 转 RecordtoUnderlineKeyMap(Object bean) Converts a Bean to a Map with underline keys Bean 转 Map(驼峰转下划线 key)
-
Method Details
-
copyProperties
-
copyProperties
-
copyProperties
-
copyProperties
Copies properties with a converter 复制属性(带转换器) -
copyToNew
-
copyToNew
-
deepCopyProperties
Deep copies properties including nested objects. Uses recursive deep copy for arrays, collections, maps, and nested beans. Handles circular references via IdentityHashMap tracking.Note: If a nested bean cannot be instantiated (e.g., abstract class, no default constructor), the original reference is used as a fallback. Modifying such nested objects in the copy will affect the original.
深度复制属性(包括嵌套对象)。 对数组、集合、Map 和嵌套 Bean 进行递归深拷贝。 通过 IdentityHashMap 处理循环引用。注意:如果嵌套 Bean 无法实例化(如抽象类、无默认构造函数),将回退使用原始引用。修改拷贝中此类嵌套对象会影响原对象。
- Parameters:
source- the source object - 源对象target- the target object - 目标对象
-
toMap
-
toMap
-
toMapNonNull
-
toUnderlineKeyMap
-
toBean
-
toBean
-
toBeanFromUnderlineKey
-
getProperty
-
getProperty
-
getPropertyOptional
-
setProperty
-
setPropertyWithConvert
-
setProperties
-
getPropertyDescriptors
Gets all property descriptors 获取所有属性描述符 -
getPropertyDescriptor
public static Optional<PropertyDescriptor> getPropertyDescriptor(Class<?> beanClass, String propertyName) Gets the specified property descriptor 获取指定属性描述符 -
getPropertyNames
-
getReadablePropertyNames
-
getWritablePropertyNames
-
hasProperty
-
getPropertyType
-
equals
-
equals
-
diff
-
diff
-
isEmpty
Checks whether the Bean is empty (all properties are null or primitive defaults) 检查是否为空 Bean(所有属性为 null 或基本类型默认值) -
hasNonNullProperty
Checks whether there is any non-null property 检查是否有任意非空属性 -
getNonNullPropertyNames
-
fromRecord
-
toRecord
-