Class BeanUtil
Bean/object utilities.
- Since:
- 2.0.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Tclone(T bean) Clones the given object by creating a new instance of the same type and performing shallow copies of the given object property values over the corresponding target properties of the new object.static <T> voidcopyProperties(T target, T source) Performs shallow copies of source object property values over the corresponding target properties.static <T> voidcopyPropertiesOverNulls(T target, T source) Performs shallow copies of source object property values over the corresponding target properties whose values arenull.static <T> Stringdiff(T bean1, T bean2) Checks for differences between two beans and returns different property values as a formatted string.static <T> List<T> Finds a list of objects matching the given type by traversing the supplied object graph (i.e., itself and its child objects, recursively).getChildren(Object bean) Gets child property beans of the supplied objects (nested beans).static List<FluentPropertyDescriptor> getPropertyDescriptors(Class<?> beanClass) Gets a list of fluent property descriptors from the supplied bean class.static List<FluentPropertyDescriptor> getPropertyDescriptors(Object bean) Gets a list of fluent property descriptors from the supplied bean.static Class<?> getPropertyGenericType(Class<?> beanClass, String propertyName) Gets the generic type of a class property.static Class<?> getPropertyType(Object bean, String propertyName) Gets the type of specified object property based on its property descriptor.static MethodgetReadMethod(Class<?> beanClass, String propertyName) Gets the read method (getter) for a bean property.static MethodgetReadMethod(Object bean, String propertyName) Gets the read method (getter) for a bean property.static <T> TgetValue(Object bean, PropertyDescriptor propertyDescriptor) Gets the value of a bean property based on aPropertyDescriptor.static <T> TGets the value of a bean property.static MethodgetWriteMethod(Class<?> beanClass, String propertyName) Gets the write method (setter) for a bean property.static MethodgetWriteMethod(Class<?> beanClass, String propertyName, Class<?> propertyType) Gets the write method (setter) for a bean property.static MethodgetWriteMethod(Object bean, String propertyName) Gets the write method (setter) for a bean property.static booleanhasChildren(Object bean) Gets whether the supplied object has any children (nested beans).static booleanisGettable(Object bean, String propertyName) Deprecated.static booleanisReadable(Class<?> beanClass, String propertyName) Gets whether a given class property has a read method for it (getter).static booleanisReadable(Object bean, String propertyName) Gets whether a given bean property has a read method for it (getter).static booleanisSettable(Object bean, String propertyName) Deprecated.UseisWritable(Object, String)instead.static booleanisWritable(Class<?> beanClass, String propertyName) Gets whether a given bean property has a write method for it (setter).static booleanisWritable(Object bean, String propertyName) Gets whether a given bean property has a write method for it (setter).static voidSets the value of a bean property only if it is writable.Converts a bean to a map where the keys are the object property names and values are the object property values.static PropertiestoProperties(Object bean, String... ignoredProperties) Converts a bean to aPropertiesinstance where the keys are the object property names and values are the object property values, converted to string, with support for arrays and collections.static booleanVisits objects by traversing the supplied object graph (i.e., itself and its child objects, recursively) for as long as the predicate returnstrue.static <T> booleanVisits objects matching the given type by traversing the supplied object graph (i.e., itself and its child objects, recursively) for as long as the predicate returnstrue.static voidVisits all objects by traversing the supplied object graph (i.e., itself and its child objects, recursively).static <T> voidVisits all objects matching the given type by traversing the supplied object graph (i.e., itself and its child objects, recursively).static voidvisitAllProperties(Object bean, BiConsumer<Object, PropertyDescriptor> visitor) Visits all properties of the supplied object by traversing its object graph (i.e., the supplied object properties and its child object properties, recursively).static <T> voidvisitAllProperties(Object bean, BiConsumer<T, PropertyDescriptor> visitor, Class<T> type) Visits all properties of objects matching the given type starting with the supplied object, traversing its object graph (i.e., the supplied object properties and its child object properties, recursively).static booleanvisitProperties(Object bean, BiPredicate<Object, PropertyDescriptor> visitor) Visits all properties of the supplied object by traversing its object graph (i.e., the supplied object properties and its child object properties, recursively) for as long as the predicate returnstrue.static <T> booleanvisitProperties(Object bean, BiPredicate<Object, PropertyDescriptor> visitor, Class<T> type) Visits all properties of objects matching the given type starting with the supplied object, traversing its object graph (i.e., the supplied object properties and its child object properties, recursively) for as long as the predicate returnstrue.
-
Method Details
-
getPropertyDescriptors
Gets a list of fluent property descriptors from the supplied bean. Only beans and properties meeting these conditions will have their property descriptors extracted:
- Bean class must have a public, no-argument constructor
- Property field must be private
- Property must have at least one public read or write method.
If there is an error in retrieving a properties, it will be silently skipped.
Contrary to regular Java beans, the supplied class does not have to implement
Serializable.Since 3.0.0, properties descriptors with read-only or write-only methods are supported and method name variations will be used to detect read and write methods.
- Parameters:
bean- the bean to get property descriptors from- Returns:
- list of property descriptors or an empty list if the supplied
bean does not qualify or is
null. - See Also:
-
getPropertyDescriptors
Gets a list of fluent property descriptors from the supplied bean class. Only classes and properties meeting these conditions will have their property descriptors extracted:
- Bean class must have a public, no-argument constructor
- Property field must be private
- Property must have at least one public read or write method.
If there is an error in retrieving a properties, it will be silently skipped.
Contrary to regular Java beans, the supplied class does not have to implement
Serializable.Supports method name variations for accessors.
- Parameters:
beanClass- the bean class to get property descriptors from- Returns:
- list of property descriptors or an empty list if the supplied
bean does not qualify or is
null. - Since:
- 3.0.0
- See Also:
-
copyPropertiesOverNulls
public static <T> void copyPropertiesOverNulls(T target, T source) Performs shallow copies of source object property values over the corresponding target properties whose values arenull. Supports method name variations for accessors.- Type Parameters:
T- the type of source and target objects- Parameters:
target- the target objectsource- the source object- See Also:
-
copyProperties
public static <T> void copyProperties(T target, T source) Performs shallow copies of source object property values over the corresponding target properties. Supports method name variations for accessors.- Type Parameters:
T- the type of source and target objects- Parameters:
target- the target objectsource- the source object- See Also:
-
clone
public static <T> T clone(T bean) Clones the given object by creating a new instance of the same type and performing shallow copies of the given object property values over the corresponding target properties of the new object. The object type must have an empty constructor. Supports method name variations for accessors.- Type Parameters:
T- the type of the object being cloned- Parameters:
bean- the object being cloned- Returns:
- the cloned object
- Throws:
BeanException- error cloning the bean- See Also:
-
diff
Checks for differences between two beans and returns different property values as a formatted string.- Type Parameters:
T- the type of the objects being checked for differences- Parameters:
bean1- the first objectbean2- the second object- Returns:
- a formatted string highlighting any differences
-
getChildren
Gets child property beans of the supplied objects (nested beans). For a bean object to be returned as a child, it needs to be accessible via a read method (getter).- Parameters:
bean- the object to obtain child beans from- Returns:
- list of child beans
-
hasChildren
Gets whether the supplied object has any children (nested beans). That is, it has at least one property with a read method.- Parameters:
bean- the bean to test for children- Returns:
trueif the object has child beans
-
getPropertyType
Gets the type of specified object property based on its property descriptor. Supports method name variations for accessors.- Parameters:
bean- the objectpropertyName- the property name- Returns:
- object property type or
nullif not a bean property (e.g., has no read or write method) - Throws:
BeanException- on error obtaining the property type- See Also:
-
getPropertyGenericType
Gets the generic type of a class property. Useful with collections or others classes holding a single single parameterized type.- Parameters:
beanClass- the object classpropertyName- the property name- Returns:
- object property generic type
- Throws:
BeanException- on error obtaining the property generic type
-
getReadMethod
Gets the read method (getter) for a bean property. Supports method name variations for accessors.- Parameters:
bean- bean objectpropertyName- property name- Returns:
- read method, or
ObjectUtils.Nullif property is write-only - Since:
- 3.0.0
- See Also:
-
getReadMethod
Gets the read method (getter) for a bean property. Supports method name variations for accessors.- Parameters:
beanClass- object classpropertyName- property name- Returns:
- read method, or
ObjectUtils.Nullif property is write-only - Since:
- 3.0.0
- See Also:
-
getWriteMethod
Gets the write method (setter) for a bean property. Supports method name variations for accessors.- Parameters:
bean- objectpropertyName- property name- Returns:
- write method, or
nullif property is read-only - Since:
- 3.0.0
- See Also:
-
getWriteMethod
Gets the write method (setter) for a bean property. Supports method name variations for accessors.- Parameters:
beanClass- object classpropertyName- property name- Returns:
- write method, or
ObjectUtils.Nullif property is read-only - Since:
- 3.0.0
- See Also:
-
getWriteMethod
Gets the write method (setter) for a bean property. Supports method name variations for accessors.- Parameters:
beanClass- object classpropertyName- property namepropertyType- property type, ornullif to be detected- Returns:
- write method, or
ObjectUtils.Nullif property is read-only - Since:
- 3.0.0
- See Also:
-
getValue
Gets the value of a bean property. Returnsnullif the property has no read method. Supports method name variations for accessors.- Type Parameters:
T- return value type- Parameters:
bean- the objectpropertyName- the property name- Returns:
- object property value or
nullif no read method - Throws:
BeanException- on error obtaining the property value- See Also:
-
getValue
Gets the value of a bean property based on aPropertyDescriptor. This is a convenience method equivalent to invoking the method obtained withPropertyDescriptor.getReadMethod()when not read-only.- Type Parameters:
T- return value type- Parameters:
bean- the objectpropertyDescriptor- the property descriptor- Returns:
- object property value
- Throws:
BeanException- on error obtaining the property value
-
isReadable
Gets whether a given bean property has a read method for it (getter). Supports method name variations for accessors.- Parameters:
bean- the objectpropertyName- the property name- Returns:
trueif the object property as a getter method- Throws:
BeanException- on error obtaining property information- Since:
- 3.0.0, replaces now
deprecated
isGettable(Object, String) - See Also:
-
isReadable
Gets whether a given class property has a read method for it (getter). Supports method name variations for accessors.- Parameters:
beanClass- a bean classpropertyName- the property name- Returns:
trueif the object property as a getter method- Throws:
BeanException- on error obtaining property information- Since:
- 3.0.0
- See Also:
-
isGettable
Deprecated.UseisReadable(Object, String)insteadGets whether a given bean property has a getter method for it.- Parameters:
bean- the objectpropertyName- the property name- Returns:
trueif the object property as a getter method- Throws:
BeanException- on error obtaining property information
-
setValue
public static void setValue(@NonNull @NonNull Object bean, @NonNull @NonNull String propertyName, Object value) Sets the value of a bean property only if it is writable. Otherwise does nothing. Supports method name variations for accessors.- Parameters:
bean- the objectpropertyName- the property namevalue- object property value- Throws:
BeanException- on error obtaining the property value- See Also:
-
isSettable
Deprecated.UseisWritable(Object, String)instead.Gets whether a given bean property has a write method for it (setter). Supports method name variations for accessors.- Parameters:
bean- the objectpropertyName- the property name- Returns:
trueif the object property as a setter method- Throws:
BeanException- on error obtaining property information- See Also:
-
isWritable
Gets whether a given bean property has a write method for it (setter). Supports method name variations for accessors.- Parameters:
bean- the objectpropertyName- the property name- Returns:
trueif the object property as a write method- Throws:
BeanException- on error obtaining property information- Since:
- 3.0.0
- See Also:
-
isWritable
Gets whether a given bean property has a write method for it (setter). Supports method name variations for accessors.- Parameters:
beanClass- bean classpropertyName- property name- Returns:
trueif the object property as a write method- Throws:
BeanException- on error obtaining property information- Since:
- 3.0.0
- See Also:
-
toMap
Converts a bean to a map where the keys are the object property names and values are the object property values.- Parameters:
bean- the object to convert to a map.- Returns:
- the converted a map
- Throws:
BeanException- on error converting bean to map
-
toProperties
Converts a bean to aPropertiesinstance where the keys are the object property names and values are the object property values, converted to string, with support for arrays and collections.- Parameters:
bean- the object to convert to properties.ignoredProperties- properties to skip (not converted)- Returns:
- the converted properties
- Throws:
BeanException- on error converting bean to properties
-
find
Finds a list of objects matching the given type by traversing the supplied object graph (i.e., itself and its child objects, recursively).- Type Parameters:
T- the type of objects to find- Parameters:
bean- the bean on which to find matchestype- class instance of the type to find (must not benull)- Returns:
- a list of objects matching the supplied type
-
visitAll
Visits all objects by traversing the supplied object graph (i.e., itself and its child objects, recursively).- Parameters:
bean- the bean to visitvisitor- a consumer invoked for all objects visited
-
visitAll
Visits all objects matching the given type by traversing the supplied object graph (i.e., itself and its child objects, recursively).- Type Parameters:
T- the type of objects to visit- Parameters:
bean- the bean to visitvisitor- a consumer invoked for all objects visitedtype- class instance of the type to find
-
visit
Visits objects by traversing the supplied object graph (i.e., itself and its child objects, recursively) for as long as the predicate returnstrue. The visiting stops the moment afalsevalue is returned.- Parameters:
bean- the bean to visitvisitor- a predicate invoked for all objects visited- Returns:
trueif the visit was complete (all predicate invocations returnedtrue).
-
visit
Visits objects matching the given type by traversing the supplied object graph (i.e., itself and its child objects, recursively) for as long as the predicate returnstrue. The visiting stops the moment afalsevalue is returned.- Type Parameters:
T- the type of objects to visit- Parameters:
bean- the bean to visitvisitor- a predicate invoked for all objects visitedtype- class instance of the type to find- Returns:
trueif the visit was complete (all predicate invocations returnedtrue).
-
visitAllProperties
Visits all properties of the supplied object by traversing its object graph (i.e., the supplied object properties and its child object properties, recursively). For each properties, the supplied visitor takes both the property descriptor and the object instance on which the property was found.- Parameters:
bean- the bean on which visit propertiesvisitor- a consumer invoked for all properties visited
-
visitAllProperties
public static <T> void visitAllProperties(Object bean, BiConsumer<T, PropertyDescriptor> visitor, Class<T> type) Visits all properties of objects matching the given type starting with the supplied object, traversing its object graph (i.e., the supplied object properties and its child object properties, recursively). For each properties, the supplied visitor takes both the property descriptor and the object instance on which the property was found.- Type Parameters:
T- the type of objects to visit- Parameters:
bean- the bean on which visit propertiesvisitor- a consumer invoked for all properties visitedtype- class instance of the type to find
-
visitProperties
Visits all properties of the supplied object by traversing its object graph (i.e., the supplied object properties and its child object properties, recursively) for as long as the predicate returnstrue. The visiting stops the moment afalsevalue is returned. For each properties, the supplied visitor takes both the property descriptor and the object instance on which the property was found.- Parameters:
bean- the bean to visitvisitor- a predicate invoked for all properties visited- Returns:
trueif the visit was complete (all predicate invocations returnedtrue).
-
visitProperties
public static <T> boolean visitProperties(Object bean, BiPredicate<Object, PropertyDescriptor> visitor, Class<T> type) Visits all properties of objects matching the given type starting with the supplied object, traversing its object graph (i.e., the supplied object properties and its child object properties, recursively) for as long as the predicate returnstrue. The visiting stops the moment afalsevalue is returned. For each properties, the supplied visitor takes both the property descriptor and the object instance on which the property was found.- Type Parameters:
T- the type of objects to visit- Parameters:
bean- the bean to visitvisitor- a predicate invoked for all properties visitedtype- class instance of the type to find- Returns:
trueif the visit was complete (all predicate invocations returnedtrue).
-
isReadable(Object, String)instead