Class ObjectPropertyUtils
- Author:
- Kuali Rice Team (rice.collab@kuali.org)
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidcopyPropertiesToObject(Map<String, String> properties, Object object) Copy properties from a string map to an object.static TypefindGenericType(Class<?> sourceClass, Class<?> targetClass) Locate the generic type declaration for a given target class in the generic type hierarchy of the source class.getAllFields(List<Field> fields, Class<?> type, Class<?> stopAt) Returns an List ofFieldobjects reflecting all the fields declared by the class or interface represented by thisClassobject.static StringgetCanonicalPath(String path) Removes any collection references from a property path, making it more useful for referring to metadata related to the property.static TypegetComponentType(Type type) Get the best known component type for a generic type.static StringgetPathTail(String path) Returns the tail of a given property path (if nested, the nested path).static Class<?> getPrimitiveType(Class<?> type) Convert to a primitive type if available.static PropertyDescriptorgetPropertyDescriptor(Class<?> beanClass, String propertyName) Get a property descriptor from a class by property name.static Map<String, PropertyDescriptor> getPropertyDescriptors(Class<?> beanClass) Get a mapping of property descriptors by property name for a bean class.static PropertyEditorgetPropertyEditor(Class<?> propertyType) Get a property editor given a property type.static PropertyEditorgetPropertyEditor(Object bean, String path) Gets a property editor given a specific bean and property path.static org.springframework.beans.PropertyEditorRegistryGets the property editor registry configured for the active request.static Class<?> getPropertyType(Class<?> beanClass, String propertyPath) Get the type of a bean property.static Class<?> getPropertyType(Object object, String propertyPath) Get the type of a bean property.static <T> TgetPropertyValue(Object object, String propertyPath) Look up a property value.static StringgetPropertyValueAsText(Object bean, String path) Looks up a property value, then convert to text using a registered property editor.getReadablePropertyNames(Class<?> beanClass) Gets the names of all readable properties for the bean class.getReadablePropertyNamesByAnnotationType(Class<?> beanClass, Class<? extends Annotation> annotationType) Gets the property names by annotation type, based on the read methods.getReadablePropertyNamesByAnnotationType(Object bean, Class<? extends Annotation> annotationType) Gets the property names by annotation type, based on the read methods.getReadablePropertyNamesByCollectionType(Class<?> beanClass, Class<?> collectionType) Gets the property names by collection type, based on the read methods.getReadablePropertyNamesByCollectionType(Object bean, Class<?> collectionType) Gets the property names by collection type, based on the read methods.getReadablePropertyNamesByType(Class<?> beanClass, Class<?> propertyType) Gets the property names by property type, based on the read methods.getReadablePropertyNamesByType(Object bean, Class<?> propertyType) Gets the property names by property type, based on the read methods.static MethodgetReadMethod(Class<?> beanClass, String propertyName) Get the read method for a specific property on a bean class.static Class<?> getUpperBound(Type valueType) Get the upper bound of a generic type.Gets the property names for the given object that are writablestatic MethodgetWriteMethod(Class<?> beanClass, String propertyName) Get the read method for a specific property on a bean class.static voidinitializeProperty(Object object, String propertyPath) Initialize a property value.static booleanisReadableProperty(Object object, String propertyPath) Determine if a property is readable.static booleanisWritableProperty(Object object, String propertyPath) Determine if a property is writable.static voidregisterPropertyEditors(org.springframework.beans.PropertyEditorRegistry registry) Registers a default set of property editors for use with KRAD in a given property editor registry.static StringremovePathTail(String path) Removes the tail of the path from the return path.static voidsetPropertyValue(Object object, String propertyPath, Object propertyValue) Modify a property value.static voidsetPropertyValue(Object object, String propertyPath, Object propertyValue, boolean ignoreUnknown) Modify a property value.static String[]splitPropertyPath(String path) Splits the given property path into a string of property names that make up the path.
-
Method Details
-
getPropertyDescriptors
Get a mapping of property descriptors by property name for a bean class.- Parameters:
beanClass- The bean class.- Returns:
- A mapping of all property descriptors for the bean class, by property name.
-
getPropertyDescriptor
Get a property descriptor from a class by property name.- Parameters:
beanClass- The bean class.propertyName- The bean property name.- Returns:
- The property descriptor named on the bean class.
-
registerPropertyEditors
public static void registerPropertyEditors(org.springframework.beans.PropertyEditorRegistry registry) Registers a default set of property editors for use with KRAD in a given property editor registry.- Parameters:
registry- property editor registry
-
getReadablePropertyNames
Gets the names of all readable properties for the bean class.- Parameters:
beanClass- The bean class.- Returns:
- set of property names
-
getReadMethod
Get the read method for a specific property on a bean class.- Parameters:
beanClass- The bean class.propertyName- The property name.- Returns:
- The read method for the property.
-
getWriteMethod
Get the read method for a specific property on a bean class.- Parameters:
beanClass- The bean class.propertyName- The property name.- Returns:
- The read method for the property.
-
copyPropertiesToObject
Copy properties from a string map to an object.- Parameters:
properties- The string map. The keys of this map must be valid property path expressions in the context of the target object. The values are the string representations of the target bean properties.object- The target object, to copy the property values to.- See Also:
-
getPropertyType
Get the type of a bean property.Note that this method does not instantiate the bean class before performing introspection, so will not dynamic initialization behavior into account. When dynamic initialization is needed to accurate inspect the inferred property type, use
getPropertyType(Object, String)instead of this method. This method is, however, intended for use on the implementation class; to avoid instantiation simply to infer the property type, consider overriding the return type on the property read method.- Parameters:
beanClass- The bean class.propertyPath- A valid property path expression in the context of the bean class.- Returns:
- The property type referred to by the provided bean class and property path.
- See Also:
-
getPropertyType
Get the type of a bean property.- Parameters:
object- The bean instance. UsegetPropertyType(Class, String)to look up property types when an instance is not available.propertyPath- A valid property path expression in the context of the bean.- Returns:
- The property type referred to by the provided bean and property path.
- See Also:
-
getReadablePropertyNamesByType
Gets the property names by property type, based on the read methods.- Parameters:
bean- The bean.propertyType- The return type of the read method on the property.- Returns:
- list of property names
-
getReadablePropertyNamesByType
Gets the property names by property type, based on the read methods.- Parameters:
beanClass- The bean class.propertyType- The return type of the read method on the property.- Returns:
- list of property names
-
getReadablePropertyNamesByAnnotationType
public static Set<String> getReadablePropertyNamesByAnnotationType(Object bean, Class<? extends Annotation> annotationType) Gets the property names by annotation type, based on the read methods.- Parameters:
bean- The bean.annotationType- The type of an annotation on the return type.- Returns:
- list of property names
-
getReadablePropertyNamesByAnnotationType
public static Set<String> getReadablePropertyNamesByAnnotationType(Class<?> beanClass, Class<? extends Annotation> annotationType) Gets the property names by annotation type, based on the read methods.- Parameters:
beanClass- The bean class.annotationType- The type of an annotation on the return type.- Returns:
- list of property names
-
getReadablePropertyNamesByCollectionType
public static Set<String> getReadablePropertyNamesByCollectionType(Object bean, Class<?> collectionType) Gets the property names by collection type, based on the read methods.- Parameters:
bean- The bean.collectionType- The type of elements in a collection or array.- Returns:
- list of property names
-
getWritablePropertyNames
Gets the property names for the given object that are writable- Parameters:
bean- object to get writable property names for- Returns:
- set of property names
-
getReadablePropertyNamesByCollectionType
public static Set<String> getReadablePropertyNamesByCollectionType(Class<?> beanClass, Class<?> collectionType) Gets the property names by collection type, based on the read methods.- Parameters:
beanClass- The bean class.collectionType- The type of elements in a collection or array.- Returns:
- list of property names
-
getPropertyValue
Look up a property value.- Type Parameters:
T- property type- Parameters:
object- The bean instance to look up a property value for.propertyPath- A valid property path expression in the context of the bean.- Returns:
- The value of the property referred to by the provided bean and property path.
- See Also:
-
getPropertyValueAsText
Looks up a property value, then convert to text using a registered property editor.- Parameters:
bean- bean instance to look up a property value forpath- property path relative to the bean- Returns:
- The property value, converted to text using a registered property editor.
-
getPropertyEditorRegistry
public static org.springframework.beans.PropertyEditorRegistry getPropertyEditorRegistry()Gets the property editor registry configured for the active request. -
getPrimitiveType
Convert to a primitive type if available.- Parameters:
type- The type to convert.- Returns:
- A primitive type, if available, that corresponds to the type.
-
getPropertyEditor
Gets a property editor given a specific bean and property path.- Parameters:
bean- The bean instance.path- The property path.- Returns:
- property editor
-
getPropertyEditor
Get a property editor given a property type.- Parameters:
propertyType- The property type to look up an editor for.path- The property path, if applicable.- Returns:
- property editor
-
initializeProperty
Initialize a property value.Upon returning from this method, the property referred to by the provided bean and property path will have been initialized with a default instance of the indicated property type.
- Parameters:
object- The bean instance to initialize a property value for.propertyPath- A valid property path expression in the context of the bean.- See Also:
-
setPropertyValue
Modify a property value.Upon returning from this method, the property referred to by the provided bean and property path will have been populated with property value provided. If the propertyValue does not match the type of the indicated property, then type conversion will be attempted using
PropertyEditorManager.- Parameters:
object- The bean instance to initialize a property value for.propertyPath- A valid property path expression in the context of the bean.propertyValue- The value to populate value in the property referred to by the provided bean and property path.- Throws:
RuntimeException- If the property path is not valid in the context of the bean provided.- See Also:
-
setPropertyValue
public static void setPropertyValue(Object object, String propertyPath, Object propertyValue, boolean ignoreUnknown) Modify a property value.Upon returning from this method, the property referred to by the provided bean and property path will have been populated with property value provided. If the propertyValue does not match the type of the indicated property, then type conversion will be attempted using
PropertyEditorManager.- Parameters:
object- The bean instance to initialize a property value for.propertyPath- A property path expression in the context of the bean.propertyValue- The value to populate value in the property referred to by the provided bean and property path.ignoreUnknown- True if invalid property values should be ignored, false to throw a RuntimeException if the property reference is invalid.- See Also:
-
isReadableProperty
Determine if a property is readable.- Parameters:
object- The bean instance to initialize a property value for.propertyPath- A property path expression in the context of the bean.- Returns:
- True if the path expression resolves to a valid readable property reference in the context of the bean provided.
-
isWritableProperty
Determine if a property is writable.- Parameters:
object- The bean instance to initialize a property value for.propertyPath- A property path expression in the context of the bean.- Returns:
- True if the path expression resolves to a valid writable property reference in the context of the bean provided.
-
getAllFields
Returns an List ofFieldobjects reflecting all the fields declared by the class or interface represented by thisClassobject. This includes public, protected, default (package) access, and private fields, and includes inherited fields.- Parameters:
fields- A list ofFieldobjects which gets returned.type- Type of class or interface for which fields are returned.stopAt- The Superclass upto which the inherited fields are to be included- Returns:
- List of all fields
-
getComponentType
Get the best known component type for a generic type.When the type is not parameterized or has no explicitly defined parameters,
Objectis returned.When the type has multiple parameters, the right-most parameter is considered the component type. This facilitates identifying the value type of a Map.
- Parameters:
type- The generic collection or map type.- Returns:
- component or value type, resolved from the generic type
-
getUpperBound
Get the upper bound of a generic type.When the type is a class, the class is returned.
When the type is a wildcard, and the upper bound is a class, the upper bound of the wildcard is returned.
If the type has not been explicitly defined at compile time,
Objectis returned.- Parameters:
valueType- The generic collection or map type.- Returns:
- component or value type, resolved from the generic type
-
findGenericType
Locate the generic type declaration for a given target class in the generic type hierarchy of the source class.- Parameters:
sourceClass- The class representing the generic type hierarchy to scan.targetClass- The class representing the generic type declaration to locate within the source class' hierarchy.- Returns:
- The generic type representing the target class within the source class' generic hierarchy.
-
splitPropertyPath
Splits the given property path into a string of property names that make up the path.- Parameters:
path- property path to split- Returns:
- string array of names, starting from the top parent
- See Also:
-
getPathTail
Returns the tail of a given property path (if nested, the nested path).For example, if path is "nested1.foo", this will return "foo". If path is just "foo", "foo" will be returned.
- Parameters:
path- path to return tail for- Returns:
- String tail of path
-
removePathTail
Removes the tail of the path from the return path.For example, if path is "nested1.foo", this will return "nested1". If path is just "foo", "" will be returned.
- Parameters:
path- path to remove tail from- Returns:
- String path with tail removed (may be empty string)
-
getCanonicalPath
Removes any collection references from a property path, making it more useful for referring to metadata related to the property.- Parameters:
path- A property path expression.- Returns:
- The path, with collection references removed.
-