Class ObjectPropertyUtils

java.lang.Object
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 Details

    • 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.
    • 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

      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:
    • 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:
    • 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:
    • 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
    • getWritablePropertyNames

      public static Set<String> getWritablePropertyNames(Object bean)
      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

      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:
    • getPropertyValueAsText

      public static String getPropertyValueAsText(Object bean, String path)
      Looks up a property value, then convert to text using a registered property editor.
      Parameters:
      bean - bean instance to look up a property value for
      path - 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

      public static Class<?> getPrimitiveType(Class<?> type)
      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

      public static PropertyEditor getPropertyEditor(Object bean, String path)
      Gets a property editor given a specific bean and property path.
      Parameters:
      bean - The bean instance.
      path - The property path.
      Returns:
      property editor
    • getPropertyEditor

      public static PropertyEditor getPropertyEditor(Class<?> propertyType)
      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

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

      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
      See Also:
    • getPathTail

      public static String getPathTail(String path)
      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

      public static String removePathTail(String path)
      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

      public static String getCanonicalPath(String path)
      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.