Class BeanUtil

  • Direct Known Subclasses:
    ActionHarness

    public class BeanUtil
    extends Object
    Utility methods for processing Java Beans in a consistent manner across all Hitachi Vantara projects. This is not an attempt to duplicate the behavior of commons-beanutils, rather, a central spot for common operations on beans so we can ensure that same bean property binding functionality and logic anytime we need to work with Java Beans.

    This utility is especially important in dealing with Hitachi Vantara Action beans IActions. See ActionHarness for an IAction-specific flavor of this utility.

    Author:
    aphillips
    See Also:
    ActionHarness
    • Constructor Detail

      • BeanUtil

        public BeanUtil​(Object targetBean)
        Setup a new bean util for operating on the given bean
        Parameters:
        targetBean - the bean on which to operate
    • Method Detail

      • isReadable

        public boolean isReadable​(String propertyName)
      • isWriteable

        public boolean isWriteable​(String propertyName)
        Returns true if a bean property can be written to (i.e. there is an accessible and appropriately typed setter method on the bean). Note: this method will check for both scalar (normal) and indexed properties before returning false.
        Parameters:
        propertyName - the name of the bean property to check for write-ability
        Returns:
        true if the bean property can be written to
      • setValue

        public void setValue​(String propertyName,
                             Object value,
                             ValueSetErrorCallback callback)
                      throws Exception
        Set a bean property with a given value, allowing the caller to respond to various error states that may be encountered during the attempt to set the value on the bean.
        Parameters:
        propertyName - the bean property to set
        value - the value to set on the bean. If value is an instance of ValueGenerator, then the value will be derived by calling ValueGenerator.getValue(String)
        callback - a structure that alerts the caller of any error states and enables the caller to fail, log, proceed, etc
        Throws:
        Exception - if there was a problem setting the value on the bean
        See Also:
        ValueGenerator, ValueSetErrorCallback
      • setValue

        public void setValue​(String propertyName,
                             Object value,
                             PropertyNameFormatter... formatters)
                      throws Exception
        Set a bean property with a given value, allowing the caller to respond to various error states that may be encountered during the attempt to set the value on the bean. This method also allows the caller to specify formatters that will modify the property name to match what the bean expects as the true name of the property. This can be helpful when you are trying to map parameters from a source that follows a convention that is not Java Bean spec compliant.
        Parameters:
        propertyName - the bean property to set
        value - the value to set on the bean. If value is an instance of ValueGenerator, then the value will be derived by calling ValueGenerator.getValue(String). Note: if value is null, we consciously bypass the set operation altogether since it leads to indeterminate behavior, i.e. it may fail or succeed.
        callback - a structure that alerts the caller of any error states and enables the caller to fail, log, proceed, etc
        formatters - a list of objects that can be used to modify the given property name prior to performing any operations on the bean itself. This new formatted property name will be used to identify the bean property. bean lookup and value setting
        Throws:
        Exception - when something goes wrong (controlled by the callback object)
        See Also:
        ValueGenerator, ValueSetErrorCallback, PropertyNameFormatter
      • setValue

        public void setValue​(String propertyName,
                             Object value,
                             ValueSetErrorCallback callback,
                             PropertyNameFormatter... formatters)
                      throws Exception
        Set a bean property with a given value, allowing the caller to respond to various error states that may be encountered during the attempt to set the value on the bean. This method also allows the caller to specify formatters that will modify the property name to match what the bean expects as the true name of the property. This can be helpful when you are trying to map parameters from a source that follows a convention that is not Java Bean spec compliant.
        Parameters:
        propertyName - the bean property to set
        value - the value to set on the bean. If value is an instance of ValueGenerator, then the value will be derived by calling ValueGenerator.getValue(String). Note: if value is null, we consciously bypass the set operation altogether since it leads to indeterminate behavior, i.e. it may fail or succeed.
        callback - a structure that alerts the caller of any error states and enables the caller to fail, log, proceed, etc
        formatters - a list of objects that can be used to modify the given property name prior to performing any operations on the bean itself. This new formatted property name will be used to identify the bean property. bean lookup and value setting
        Throws:
        Exception - when something goes wrong (controlled by the callback object)
        See Also:
        ValueGenerator, ValueSetErrorCallback, PropertyNameFormatter
      • setValues

        public void setValues​(Map<String,​Object> propValueMap)
                       throws Exception
        Sets a number of bean properties based on given property-value map, where the key of the map is the bean property and the value is the value to which to set that property.
        Parameters:
        propValueMap - a map whose keys are property names and whose values are to be set on the associated property of the bean
        Throws:
        Exception - if there was a problem setting the value on the bean