org.kuali.rice.krad.uif.util
Class ObjectPropertyUtils

java.lang.Object
  extended by org.kuali.rice.krad.uif.util.ObjectPropertyUtils

public final class ObjectPropertyUtils
extends Object

Utility methods to get/set property values and working with objects.

Author:
Kuali Rice Team (rice.collab@kuali.org)

Method Summary
static void copyPropertiesToObject(Map<String,String> properties, Object object)
          Copy properties from a string map to an object.
static Type findGenericType(Class<?> sourceClass, Class<?> targetClass)
          Locate the generic type declaration for a given target class in the generic type hierarchy of the source class.
static List<Field> getAllFields(List<Field> fields, Class<?> type, Class<?> stopAt)
          Returns an List of Field objects reflecting all the fields declared by the class or interface represented by this Class object.
static Type getComponentType(Type type)
          Get the best known component type for a generic type.
static PropertyDescriptor getPropertyDescriptor(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 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> T
getPropertyValue(Object object, String propertyPath)
          Look up a property value.
static Set<String> getReadablePropertyNames(Class<?> beanClass)
          Gets the names of all readable properties for the bean class.
static Set<String> getReadablePropertyNamesByAnnotationType(Class<?> beanClass, Class<? extends Annotation> annotationType)
          Gets the property names by annotation type, based on the read methods.
static Set<String> getReadablePropertyNamesByAnnotationType(Object bean, Class<? extends Annotation> annotationType)
          Gets the property names by annotation type, based on the read methods.
static Set<String> getReadablePropertyNamesByCollectionType(Class<?> beanClass, Class<?> collectionType)
          Gets the property names by collection type, based on the read methods.
static Set<String> getReadablePropertyNamesByCollectionType(Object bean, Class<?> collectionType)
          Gets the property names by collection type, based on the read methods.
static Set<String> getReadablePropertyNamesByType(Class<?> beanClass, Class<?> propertyType)
          Gets the property names by property type, based on the read methods.
static Set<String> getReadablePropertyNamesByType(Object bean, Class<?> propertyType)
          Gets the property names by property type, based on the read methods.
static Method getReadMethod(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.
static Method getWriteMethod(Class<?> beanClass, String propertyName)
          Get the read method for a specific property on a bean class.
static void initializeProperty(Object object, String propertyPath)
          Initialize a property value.
static boolean isReadableProperty(Object object, String propertyPath)
          Determine if a property is readable.
static boolean isWritableProperty(Object object, String propertyPath)
          Determine if a property is writable.
static void setPropertyValue(Object object, String propertyPath, Object propertyValue)
          Modify a property value.
static void setPropertyValue(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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getPropertyDescriptors

public static Map<String,PropertyDescriptor> getPropertyDescriptors(Class<?> beanClass)
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

public static PropertyDescriptor getPropertyDescriptor(Class<?> beanClass,
                                                       String propertyName)
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.

getReadablePropertyNames

public static Set<String> getReadablePropertyNames(Class<?> beanClass)
Gets the names of all readable properties for the bean class.

Parameters:
beanClass - The bean class.
Returns:
set of property names

getReadMethod

public static Method getReadMethod(Class<?> beanClass,
                                   String propertyName)
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

public static Method getWriteMethod(Class<?> beanClass,
                                    String propertyName)
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

public static void copyPropertiesToObject(Map<String,String> properties,
                                          Object object)
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:
ObjectPathExpressionParser

getPropertyType

public static Class<?> getPropertyType(Class<?> beanClass,
                                       String propertyPath)
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:
ObjectPathExpressionParser

getPropertyType

public static Class<?> getPropertyType(Object object,
                                       String propertyPath)
Get the type of a bean property.

Parameters:
object - The bean instance. Use getPropertyType(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:
ObjectPathExpressionParser

getReadablePropertyNamesByType

public static Set<String> getReadablePropertyNamesByType(Object bean,
                                                         Class<?> propertyType)
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

public static Set<String> getReadablePropertyNamesByType(Class<?> beanClass,
                                                         Class<?> propertyType)
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

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

public static <T> T getPropertyValue(Object object,
                                     String propertyPath)
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:
ObjectPathExpressionParser

initializeProperty

public static void initializeProperty(Object object,
                                      String propertyPath)
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:
getPropertyType(Object, String), setPropertyValue(Object, String, Object), ObjectPathExpressionParser

setPropertyValue

public static void setPropertyValue(Object object,
                                    String propertyPath,
                                    Object propertyValue)
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:
ObjectPathExpressionParser

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:
ObjectPathExpressionParser

isReadableProperty

public static boolean isReadableProperty(Object object,
                                         String propertyPath)
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

public static boolean isWritableProperty(Object object,
                                         String propertyPath)
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

public static List<Field> getAllFields(List<Field> fields,
                                       Class<?> type,
                                       Class<?> stopAt)
Returns an List of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, and includes inherited fields.

Parameters:
fields - A list of Field objects 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

public static Type getComponentType(Type type)
Get the best known component type for a generic type.

When the type is not parameterized or has no explicitly defined parameters, Object is 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

public static Class<?> getUpperBound(Type valueType)
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, Object is returned.

Parameters:
valueType - The generic collection or map type.
Returns:
component or value type, resolved from the generic type

findGenericType

public static Type findGenericType(Class<?> sourceClass,
                                   Class<?> targetClass)
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

public static String[] splitPropertyPath(String path)
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


Copyright © 2005–2014 The Kuali Foundation. All rights reserved.