Class BeanUtils
Since 1.10, bean properties can use Optional getters. Their
values will be unwrapped when they are read, where an empty
Optional means the same as null, and the type of such a
property is the Optional's value type. Since the standard
JavaBeans introspector drops a plain-typed setter that sits next to an
Optional getter, RIFE2 pairs them up again so that the property
simply behaves like a regular read-write property. A setter that declares
an Optional parameter will receive its values wrapped.
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnum used to specify which accessors to consider when retrieving a bean's properties. -
Method Summary
Modifier and TypeMethodDescriptionstatic intcountProperties(Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix) Counts the number of properties that match the given criteria for a specified bean class.static intcountProperties(BeanUtils.Accessors accessors, Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix) Counts the number of properties that match the given criteria for a specified bean class, using the specified Accessors enum value.static StringformatPropertyValue(Object propertyValue, ConstrainedProperty constrainedProperty) Formats a property value based on the given format from a constrained property.static BeanInfogetBeanInfo(Class<?> beanClass) Gets the BeanInfo for the specified beanClass.getPropertyNames(Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix) Gets the names of the properties of the specified beanClass that have both read and write accessors.getPropertyNames(BeanUtils.Accessors accessors, Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix) Gets the names of the properties of the specified beanClass that have the specified accessors.static ClassgetPropertyType(Class beanClass, String name) Returns the class of a property of a bean, given its name.getPropertyTypes(Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix) Returns a map of property names and their corresponding types for the given bean class, based on the included/excluded properties and prefix.getPropertyTypes(BeanUtils.Accessors accessors, Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix) Returns a map of property names and their corresponding types for the given bean class, based on the included/excluded properties and prefix, using the specified accessors.static ObjectgetPropertyValue(Object bean, String name) Returns the value of the property with the given name from the specified bean.getPropertyValues(Object bean, String[] includedProperties, String[] excludedProperties, String prefix) Returns the values of the properties of a bean as a map, given an optional prefix and inclusive/exclusive property filters.getPropertyValues(BeanUtils.Accessors accessors, Object bean, String[] includedProperties, String[] excludedProperties, String prefix) Returns the values of the properties of a bean as a map, given an optional prefix and inclusive/exclusive property filters and a custom set of accessors.static Map<String,PropertyDescriptor> getUppercasedBeanProperties(Class beanClass) Retrieves a map of all the properties of a bean and their descriptors.Parses the textual representation of the date using a custom format, or by relying on the standard date formats.static voidprocessProperties(Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix, BeanPropertyProcessor processor) Processes the properties of a given bean class with provided accessors, including and excluding properties, using the given prefix and calling the provided BeanPropertyProcessor with each valid property.static voidprocessProperties(BeanUtils.Accessors accessors, Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix, BeanPropertyProcessor processor) Processes the properties of a given bean class with the provided accessors, including and excluding properties, using the given prefix and calling the provided BeanPropertyProcessor with each valid property.static voidprocessPropertyValues(Object bean, String[] includedProperties, String[] excludedProperties, String prefix, BeanPropertyValueProcessor processor) Processes the property values of a bean based on the provided parameters.static voidprocessPropertyValues(BeanUtils.Accessors accessors, Object bean, String[] includedProperties, String[] excludedProperties, String prefix, BeanPropertyValueProcessor processor) Processes the property values of a bean based on the provided parameters, using the specified Accessors enum value.static voidsetPropertyValue(Object bean, String name, Object value) Sets the value of the property with the given name in the specified bean.static voidsetUppercasedBeanProperty(String propertyName, String[] propertyValues, String propertyNamePrefix, Map<String, PropertyDescriptor> beanProperties, Object beanInstance, Object emptyBean) Set the value of a bean property from an array of strings.static voidsetUppercasedBeanProperty(String propertyName, UploadedFile propertyFile, String propertyNamePrefix, Map<String, PropertyDescriptor> beanProperties, Object beanInstance) Set the value of a bean property from an uploaded file.
-
Method Details
-
getBeanInfo
Gets the BeanInfo for the specified beanClass.The property descriptors describe
Optionalproperties directly, which means thatPropertyDescriptor.getPropertyType()will return theOptional's value type and thatPropertyDescriptor.getWriteMethod()will return the plain-typed setter that the standard introspector dropped. This also applies to properties that only have anOptionalsetter.- Parameters:
beanClass- The Class to get the BeanInfo for.- Returns:
- The BeanInfo for the specified beanClass.
- Throws:
BeanUtilsException- If an error occurs while introspecting the bean.- Since:
- 1.0
-
getPropertyNames
public static Set<String> getPropertyNames(Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix) throws BeanUtilsException Gets the names of the properties of the specified beanClass that have both read and write accessors.- Parameters:
beanClass- The Class to get the property names for.includedProperties- The properties that should be included.excludedProperties- The properties that should be excluded.prefix- The prefix to apply to the property name.- Returns:
- A set containing the names of the properties.
- Throws:
BeanUtilsException- If an error occurs while introspecting the bean.- Since:
- 1.0
-
getPropertyNames
public static Set<String> getPropertyNames(BeanUtils.Accessors accessors, Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix) throws BeanUtilsException Gets the names of the properties of the specified beanClass that have the specified accessors.- Parameters:
accessors- The Accessors to use when retrieving the property names.beanClass- The Class to get the property names for.includedProperties- The properties that should be included.excludedProperties- The properties that should be excluded.prefix- The prefix to apply to the property name.- Returns:
- A set containing the names of the properties.
- Throws:
BeanUtilsException- If an error occurs while introspecting the bean.- Since:
- 1.0
-
processProperties
public static void processProperties(Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix, BeanPropertyProcessor processor) throws BeanUtilsException Processes the properties of a given bean class with provided accessors, including and excluding properties, using the given prefix and calling the provided BeanPropertyProcessor with each valid property.- Parameters:
beanClass- The class of the bean to process.includedProperties- An array of property names to include, or null if all properties should be included.excludedProperties- An array of property names to exclude, or null if no properties should be excluded.prefix- A string to prepend to each property name when processing.processor- The BeanPropertyProcessor to call for each valid property.- Throws:
BeanUtilsException- If an error occurs while processing the properties, such as an invalid property name or an error invoking a property method.- Since:
- 1.0
-
processProperties
public static void processProperties(BeanUtils.Accessors accessors, Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix, BeanPropertyProcessor processor) throws BeanUtilsException Processes the properties of a given bean class with the provided accessors, including and excluding properties, using the given prefix and calling the provided BeanPropertyProcessor with each valid property.- Parameters:
accessors- The Accessors to use when processing properties (either GETTERS_SETTERS or FIELDS).beanClass- The class of the bean to process.includedProperties- An array of property names to include, or null if all properties should be included.excludedProperties- An array of property names to exclude, or null if no properties should be excluded.prefix- A string to prepend to each property name when processing.processor- The BeanPropertyProcessor to call for each valid property.- Throws:
BeanUtilsException- If an error occurs while processing the properties, such as an invalid property name or an error invoking a property method.- Since:
- 1.0
-
processPropertyValues
public static void processPropertyValues(Object bean, String[] includedProperties, String[] excludedProperties, String prefix, BeanPropertyValueProcessor processor) throws BeanUtilsException Processes the property values of a bean based on the provided parameters.- Parameters:
bean- the bean whose properties will be processedincludedProperties- an array of property names to include in the processingexcludedProperties- an array of property names to exclude from the processingprefix- a prefix to add to all processed property namesprocessor- a processor function that will handle each property value- Throws:
BeanUtilsException- if an error occurs during property processing- Since:
- 1.0
-
processPropertyValues
public static void processPropertyValues(BeanUtils.Accessors accessors, Object bean, String[] includedProperties, String[] excludedProperties, String prefix, BeanPropertyValueProcessor processor) throws BeanUtilsException Processes the property values of a bean based on the provided parameters, using the specified Accessors enum value.- Parameters:
accessors- the Accessors enum value to use for getting property valuesbean- the bean whose properties will be processedincludedProperties- an array of property names to include in the processingexcludedProperties- an array of property names to exclude from the processingprefix- a prefix to add to all processed property namesprocessor- a processor function that will handle each property value- Throws:
BeanUtilsException- if an error occurs during property processing- Since:
- 1.0
-
countProperties
public static int countProperties(Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix) throws BeanUtilsException Counts the number of properties that match the given criteria for a specified bean class.- Parameters:
beanClass- the bean class to examineincludedProperties- an array of property names to include in the countexcludedProperties- an array of property names to exclude from the countprefix- a prefix to add to all property names being counted- Returns:
- the number of properties that match the given criteria
- Throws:
BeanUtilsException- if an error occurs during property counting- Since:
- 1.0
-
countProperties
public static int countProperties(BeanUtils.Accessors accessors, Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix) throws BeanUtilsException Counts the number of properties that match the given criteria for a specified bean class, using the specified Accessors enum value.- Parameters:
accessors- the Accessors enum value to use for getting property valuesbeanClass- the bean class to examineincludedProperties- an array of property names to include in the countexcludedProperties- an array of property names to exclude from the countprefix- a prefix to add to all property names being counted- Returns:
- the number of properties that match the given criteria
- Throws:
BeanUtilsException- if an error occurs during property counting- Since:
- 1.0
-
getPropertyValue
Returns the value of the property with the given name from the specified bean. Throws an exception if the property does not exist or is not readable.The value of an
Optionalgetter will be unwrapped, so that an emptyOptionalresults innull.- Parameters:
bean- The bean instance to retrieve the property value from.name- The name of the property to retrieve.- Returns:
- The value of the property.
- Throws:
BeanUtilsException- If the bean is null, is a class and not an instance, or if the property does not exist or is not readable.- Since:
- 1.0
-
setPropertyValue
public static void setPropertyValue(Object bean, String name, Object value) throws BeanUtilsException Sets the value of the property with the given name in the specified bean. Throws an exception if the property does not exist or is not writable.When the property only has an
Optionalgetter, the plain-typed setter that RIFE2 pairs with it will be used, while a setter that declares anOptionalparameter will receive the value wrapped.- Parameters:
bean- The bean instance to set the property value on.name- The name of the property to set.value- The value to set the property to.- Throws:
BeanUtilsException- If the bean is null, is a class and not an instance, or if the property does not exist or is not writable.- Since:
- 1.0
-
getPropertyType
Returns the class of a property of a bean, given its name.The type of an
Optionalproperty is the type of the value that theOptionalholds, and notOptionalitself.- Parameters:
beanClass- the class of the bean to search for the propertyname- the name of the property to retrieve the type- Returns:
- the class of the property
- Throws:
BeanUtilsException- if the bean doesn't contain the specified propertyIllegalArgumentException- if the bean class or the name are null or empty- Since:
- 1.0
-
getPropertyValues
public static Map<String,Object> getPropertyValues(Object bean, String[] includedProperties, String[] excludedProperties, String prefix) throws BeanUtilsException Returns the values of the properties of a bean as a map, given an optional prefix and inclusive/exclusive property filters.- Parameters:
bean- the bean to get the properties fromincludedProperties- an array of the property names to include in the result mapexcludedProperties- an array of the property names to exclude from the result mapprefix- an optional prefix to add to the property names in the result map- Returns:
- a map containing the property values of the bean
- Throws:
BeanUtilsException- if there was an error while retrieving the property values- Since:
- 1.0
-
getPropertyValues
public static Map<String,Object> getPropertyValues(BeanUtils.Accessors accessors, Object bean, String[] includedProperties, String[] excludedProperties, String prefix) throws BeanUtilsException Returns the values of the properties of a bean as a map, given an optional prefix and inclusive/exclusive property filters and a custom set of accessors.- Parameters:
accessors- the Accessors object to use to retrieve the property valuesbean- the bean to get the properties fromincludedProperties- an array of the property names to include in the result mapexcludedProperties- an array of the property names to exclude from the result mapprefix- an optional prefix to add to the property names in the result map- Returns:
- a map containing the property values of the bean
- Throws:
BeanUtilsException- if there was an error while retrieving the property values- Since:
- 1.0
-
formatPropertyValue
public static String formatPropertyValue(Object propertyValue, ConstrainedProperty constrainedProperty) Formats a property value based on the given format from a constrained property.A
nullvalue simply results innullwithout the format being used. Note that the values ofOptionalproperties have already been unwrapped when RIFE2 reads them.- Parameters:
propertyValue- the value of the property to formatconstrainedProperty- the constrained property that contains formatting info- Returns:
- the formatted value of the property; or
nullwhen the value isnull - Since:
- 1.0
-
getPropertyTypes
public static Map<String,Class> getPropertyTypes(Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix) throws BeanUtilsException Returns a map of property names and their corresponding types for the given bean class, based on the included/excluded properties and prefix.- Parameters:
beanClass- the class for which to retrieve property typesincludedProperties- the list of property names to includeexcludedProperties- the list of property names to excludeprefix- the prefix to use when filtering properties- Returns:
- a map of property names and their corresponding types for the given bean class
- Throws:
BeanUtilsException- if an error occurs during property retrieval- Since:
- 1.0
-
getPropertyTypes
public static Map<String,Class> getPropertyTypes(BeanUtils.Accessors accessors, Class beanClass, String[] includedProperties, String[] excludedProperties, String prefix) throws BeanUtilsException Returns a map of property names and their corresponding types for the given bean class, based on the included/excluded properties and prefix, using the specified accessors.- Parameters:
accessors- the accessor type to use for property retrievalbeanClass- the class for which to retrieve property typesincludedProperties- the list of property names to includeexcludedProperties- the list of property names to excludeprefix- the prefix to use when filtering properties- Returns:
- a map of property names and their corresponding types for the given bean class
- Throws:
BeanUtilsException- if an error occurs during property retrieval- Since:
- 1.0
-
getUppercasedBeanProperties
public static Map<String,PropertyDescriptor> getUppercasedBeanProperties(Class beanClass) throws BeanUtilsException Retrieves a map of all the properties of a bean and their descriptors.The property names will be uppercased and an exception will be thrown if two properties are equals case-insensitively.
- Parameters:
beanClass- the class of the bean- Returns:
- the map of the bean properties
- Throws:
BeanUtilsException- when an error occurred while obtaining the bean properties- Since:
- 1.0
- See Also:
-
parseDate
Parses the textual representation of the date using a custom format, or by relying on the standard date formats.- Parameters:
date- the textual representation of the dateformat- the custom format that should be used for parsing the string representation of the date; ornullif the default formats should be used- Returns:
- the parsed date
- Throws:
ParseException- if an error occurred when the date was parsed- Since:
- 1.0
-
setUppercasedBeanProperty
public static void setUppercasedBeanProperty(String propertyName, String[] propertyValues, String propertyNamePrefix, Map<String, PropertyDescriptor> beanProperties, Object beanInstance, Object emptyBean) throws BeanUtilsExceptionSet the value of a bean property from an array of strings.The values will be converted to the type of the property, which for an
Optionalproperty is the type of the value that it holds. When the property only has anOptionalgetter, the plain-typed setter that RIFE2 pairs with it will be used, while a setter that declares anOptionalparameter will receive the converted value wrapped. When an empty value comes from the empty bean and the setter takes a primitive parameter, the property will be left untouched since a primitive can't hold an absent value.- Parameters:
propertyName- the name of the propertypropertyValues- the values that will be set, can benullpropertyNamePrefix- the prefix that the propertyName parameter should have, can benullbeanProperties- the map of the uppercased bean property names and their descriptorsbeanInstance- the bean instance whose property should be updatedemptyBean- this bean instance will be used to set the value of the property in case the propertyValues parameter is empty or null, can benull- Throws:
BeanUtilsException- when an error occurred while setting the bean property- Since:
- 1.0
- See Also:
-
setUppercasedBeanProperty
public static void setUppercasedBeanProperty(String propertyName, UploadedFile propertyFile, String propertyNamePrefix, Map<String, PropertyDescriptor> beanProperties, Object beanInstance) throws BeanUtilsExceptionSet the value of a bean property from an uploaded file.- Parameters:
propertyName- the name of the propertypropertyFile- the file that will be set, can benullpropertyNamePrefix- the prefix that the propertyName parameter should have, can benullbeanProperties- the map of the uppercased bean property names and their descriptorsbeanInstance- the bean instance whose property should be updated- Throws:
BeanUtilsException- when an error occurred while setting the bean property- Since:
- 1.0
- See Also:
-