net.sf.navel.beans
Class BeanManipulator

java.lang.Object
  extended by net.sf.navel.beans.BeanManipulator

public class BeanManipulator
extends java.lang.Object

This class allows for generically manipulating the values encapsulated in JavaBeans and Navel beans. Values can be extracted, via Introspection, into a Map and populated, via Introspection, back into any given JavaBean instance from a Map. Navel and JavaBeans are treated equally with no special handling of unset properties or entries in the Map that do not match any available properties. For more strict treatment of just Navel beans, use the ProxyManipulator.

Author:
cmdln

Field Summary
static java.lang.Class<?> VARIANT_METHODS_ONLY
          Returned from typeOf(Class, String) when the property name is for an indexed property that provides no access to the array itself.
 
Method Summary
static java.util.Map<java.lang.String,java.lang.Object> describe(java.lang.Object bean)
          Overload that assumes true for suppressExceptions.
static java.util.Map<java.lang.String,java.lang.Object> describe(java.lang.Object bean, boolean suppressExceptions)
          Extracts the values from the bean argument into a Map keyed by property names.
static boolean isPropertyOf(java.lang.Class<?> beanType, java.lang.String propertyName)
          Support method to help in dealing with introspection, reflection of JavaBeans.
static boolean isPropertyOf(java.lang.Object bean, java.lang.String propertyName)
          Support method to help in dealing with introspection, reflection of JavaBeans.
static java.util.Map<java.lang.String,java.lang.Object> populate(java.lang.Object bean, java.util.Map<java.lang.String,java.lang.Object> values)
          Overload that suppresses exceptions.
static java.util.Map<java.lang.String,java.lang.Object> populate(java.lang.Object bean, java.util.Map<java.lang.String,java.lang.Object> values, boolean suppressExceptions)
          Populates the values from the Map argument into the bean argument.
static boolean putValue(java.lang.Object bean, java.lang.String property, java.lang.Object value)
          Put a single value into the target bean, assumes that exceptions should be suppressed.
static boolean putValue(java.lang.Object bean, java.lang.String property, java.lang.Object value, boolean suppressException)
          Put a single value into the target bean.
static java.lang.Object resolveValue(java.lang.String propertyName, java.util.Map<java.lang.String,java.lang.Object> values)
          Overload that suppresses exceptions.
static java.lang.Object resolveValue(java.lang.String propertyName, java.util.Map<java.lang.String,java.lang.Object> values, boolean suppressExceptions)
          Utility method to recurse into a description map explicitly for a nested property name and dig out the ultimate value.
static java.lang.Class<?> typeOf(java.lang.Class<?> beanType, java.lang.String propertyName)
          Parse and traverse the provided property name and try to determine its type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VARIANT_METHODS_ONLY

public static final java.lang.Class<?> VARIANT_METHODS_ONLY
Returned from typeOf(Class, String) when the property name is for an indexed property that provides no access to the array itself.

Method Detail

describe

public static java.util.Map<java.lang.String,java.lang.Object> describe(java.lang.Object bean)
Overload that assumes true for suppressExceptions.

Parameters:
bean - JavaBean to extract from.
flattenNested - If any of the properties are themselves Navel beans, should we flatten their properties into the key set of the containing bean?
Returns:
Map of extracted values, never null but may be empty.

describe

public static java.util.Map<java.lang.String,java.lang.Object> describe(java.lang.Object bean,
                                                                        boolean suppressExceptions)
Extracts the values from the bean argument into a Map keyed by property names. If using PropertyBeanHandler, you can safely skip this method, using getValues() instead.

Parameters:
bean - JavaBean to extract from.
suppressExceptions - Should exceptions by re-thrown as PropertyAccessException instances or logged only as warnings?
Returns:
Map of extracted values, never null but may be empty.

populate

public static java.util.Map<java.lang.String,java.lang.Object> populate(java.lang.Object bean,
                                                                        java.util.Map<java.lang.String,java.lang.Object> values)
Overload that suppresses exceptions.

Parameters:
bean - JavaBean instance to populate.
values - Values to populate into the bean.

populate

public static java.util.Map<java.lang.String,java.lang.Object> populate(java.lang.Object bean,
                                                                        java.util.Map<java.lang.String,java.lang.Object> values,
                                                                        boolean suppressExceptions)
Populates the values from the Map argument into the bean argument. If a property in the Map argument isn't supported in the bean, it is logged but functionally ignored. This method supports nested properties using "dot notation" in the Map argument, meaning "foo.bar" causes a read of the foo property and a write on the resulting object's bar property with the value in the Map for the "foo.bar" key.

Parameters:
bean - JavaBean instance to populate.
values - Values to populate into the bean.
suppressExceptions - Should exceptions by re-thrown as PropertyAccessException instances or logged only as warnings?

putValue

public static boolean putValue(java.lang.Object bean,
                               java.lang.String property,
                               java.lang.Object value)
Put a single value into the target bean, assumes that exceptions should be suppressed.

Parameters:
bean - Target into which the value will be put, if the property name is applicable.
property - Single property expression to set the specified value.
value - Value to set if the property expression is valid for the target.
Returns:
True if the property is value and the put succeeds.

putValue

public static boolean putValue(java.lang.Object bean,
                               java.lang.String property,
                               java.lang.Object value,
                               boolean suppressException)
Put a single value into the target bean.

Parameters:
bean - Target into which the value will be put, if the property name is applicable.
property - Single property expression to set the specified value.
value - Value to set if the property expression is valid for the target.
suppressException - If true, no error will be thrown if the property expression is invalid.
Returns:
True if the property is value and the put succeeds.

resolveValue

public static java.lang.Object resolveValue(java.lang.String propertyName,
                                            java.util.Map<java.lang.String,java.lang.Object> values)
Overload that suppresses exceptions.

Parameters:
propertyName - Property name, may use dot notation.
values - Map of possible values, initially the immediate properties of some bean, but with each recursive call, it will represent a new layer of bean properties.
Returns:
Null or the resolved value.

resolveValue

public static java.lang.Object resolveValue(java.lang.String propertyName,
                                            java.util.Map<java.lang.String,java.lang.Object> values,
                                            boolean suppressExceptions)
Utility method to recurse into a description map explicitly for a nested property name and dig out the ultimate value.

Parameters:
propertyName - Property name, may use dot notation.
values - Map of possible values, initially the immediate properties of some bean, but with each recursive call, it will represent a new layer of bean properties.
suppressExceptions - Should exceptions by re-thrown as PropertyAccessException instances or logged only as warnings?
Returns:
Null or the resolved value.

isPropertyOf

public static boolean isPropertyOf(java.lang.Object bean,
                                   java.lang.String propertyName)
Support method to help in dealing with introspection, reflection of JavaBeans.

Parameters:
bean - Target bean of any type.
propertyName - Name of a property to check via introspection.
Returns:
Whether the named property belongs to the bean class.

isPropertyOf

public static boolean isPropertyOf(java.lang.Class<?> beanType,
                                   java.lang.String propertyName)
Support method to help in dealing with introspection, reflection of JavaBeans.

Parameters:
beanType - Target bean type, be careful not to pass the proxy type from a dynamic proxy backing a bean.
propertyName - Name of a property to check via introspection.
Returns:
Whether the named property belongs to the bean class.

typeOf

public static java.lang.Class<?> typeOf(java.lang.Class<?> beanType,
                                        java.lang.String propertyName)
Parse and traverse the provided property name and try to determine its type.

Parameters:
beanType - Target bean type, be careful not to pass the proxy type from a dynamic proxy backing a bean.
propertyName - Name of a property to check via introspection.
Returns:
The type if it can be found or null if not.