org.pentaho.platform.util.beans
Class BeanUtil

java.lang.Object
  extended by org.pentaho.platform.util.beans.BeanUtil
Direct Known Subclasses:
ActionHarness

public class BeanUtil
extends Object

Utility methods for processing Java Beans in a consistent manner across all Pentaho 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 Pentaho Action beans IActions. See ActionHarness for an IAction-specific flavor of this utility.

Author:
aphillips
See Also:
ActionHarness

Nested Class Summary
static class BeanUtil.EagerFailingCallback
           
static class BeanUtil.FeedbackValueGenerator
           
 
Constructor Summary
BeanUtil(Object targetBean)
          Setup a new bean util for operating on the given bean
 
Method Summary
 Class<?> getPropertyType(String propertyName)
           
 Object getValue(String propertyName)
           
 boolean isReadable(String propertyName)
           
 boolean isWriteable(String propertyName)
          Returns true if a bean property can be written to (i.e.
 void setDefaultCallback(ValueSetErrorCallback defaultCallback)
           
 void setValue(String propertyName, Object value)
          Set a bean property with a given value.
 void setValue(String propertyName, Object value, ValueSetErrorCallback callback)
          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.
 void setValue(String propertyName, Object value, ValueSetErrorCallback callback, PropertyNameFormatter... formatters)
          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.
 void setValues(Map<String,Object> propValueMap)
          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.
 void setValues(Map<String,Object> propValueMap, ValueSetErrorCallback callback)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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

setDefaultCallback

public void setDefaultCallback(ValueSetErrorCallback defaultCallback)

isReadable

public boolean isReadable(String propertyName)

getValue

public Object getValue(String propertyName)
                throws IllegalAccessException,
                       InvocationTargetException,
                       NoSuchMethodException
Throws:
IllegalAccessException
InvocationTargetException
NoSuchMethodException

getPropertyType

public Class<?> getPropertyType(String propertyName)
                         throws IllegalAccessException,
                                InvocationTargetException,
                                NoSuchMethodException
Throws:
IllegalAccessException
InvocationTargetException
NoSuchMethodException

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)
              throws Exception
Set a bean property with a given value.

Parameters:
propertyName - the bean property to set
value - the value to set on the bean. If value is an instance of BeanUtil.FeedbackValueGenerator, then the value will be derived by calling getValue(String)
Throws:
Exception - if there was a problem setting the value on the bean
See Also:
BeanUtil.FeedbackValueGenerator

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 BeanUtil.FeedbackValueGenerator, then the value will be derived by calling 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:
BeanUtil.FeedbackValueGenerator, ValueSetErrorCallback

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 BeanUtil.FeedbackValueGenerator, then the value will be derived by calling 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:
BeanUtil.FeedbackValueGenerator, 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

setValues

public void setValues(Map<String,Object> propValueMap,
                      ValueSetErrorCallback callback)
               throws Exception
Throws:
Exception