Package org.pentaho.platform.util.beans
Class BeanUtil
- java.lang.Object
-
- 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 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
IAction
s. SeeActionHarness
for an IAction-specific flavor of this utility.- Author:
- aphillips
- See Also:
ActionHarness
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BeanUtil.EagerFailingCallback
static class
BeanUtil.FeedbackValueGenerator
-
Field Summary
Fields Modifier and Type Field Description protected Object
bean
protected ValueSetErrorCallback
defaultCallback
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Class<?>
getPropertyType(String propertyName)
Object
getValue(String propertyName)
boolean
isReadable(String propertyName)
boolean
isWriteable(String propertyName)
Returnstrue
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, 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
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, PropertyNameFormatter... formatters)
void
setValues(Map<String,Object> propValueMap, ValueSetErrorCallback callback)
-
-
-
Field Detail
-
bean
protected Object bean
-
defaultCallback
protected ValueSetErrorCallback defaultCallback
-
-
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
-
getPropertyType
public Class<?> getPropertyType(String propertyName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
-
isWriteable
public boolean isWriteable(String propertyName)
Returnstrue
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 returningfalse
.- 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 setvalue
- the value to set on the bean. If value is an instance ofValueGenerator
, then the value will be derived by callingValueGenerator.getValue(String)
- Throws:
Exception
- if there was a problem setting the value on the bean- See Also:
ValueGenerator
-
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 setvalue
- the value to set on the bean. If value is an instance ofValueGenerator
, then the value will be derived by callingValueGenerator.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 setvalue
- the value to set on the bean. If value is an instance ofValueGenerator
, then the value will be derived by callingValueGenerator.getValue(String)
. Note: if value isnull
, 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, etcformatters
- 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 setvalue
- the value to set on the bean. If value is an instance ofValueGenerator
, then the value will be derived by callingValueGenerator.getValue(String)
. Note: if value isnull
, 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, etcformatters
- 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
-
setValues
public void setValues(Map<String,Object> propValueMap, ValueSetErrorCallback callback) throws Exception
- Throws:
Exception
-
-