jodd.bean
Class BeanUtil

java.lang.Object
  extended byjodd.bean.BeanUtil

public class BeanUtil
extends java.lang.Object

Miscellaneous and useful bean utilities.


Constructor Summary
BeanUtil()
           
 
Method Summary
static void copyProperties(java.lang.Object source, java.lang.Object destination)
          Copies properties of one bean to another.
static void forEachProperty(java.lang.Object bean, PropertyCallback fepcb)
          Iterrates all read properties and executes callback method on it
static java.util.Map getAllProperties(java.lang.Object bean)
          Returns all bean properties (getXxx()) as a Map for easier and faster manipulation.
static java.lang.Object getProperty(java.lang.Object bean, java.lang.String name)
          Gets the value from the bean paramter.
static void load(java.lang.Object bean, java.lang.Object obj)
          Populates bean from given object by using a loader for given objects type.
static void setAllProperties(java.lang.Object bean, java.util.Map map)
          Sets all bean properties that are defined in the map for faster work.
static boolean setProperty(java.lang.Object bean, java.lang.String name, java.lang.Object value)
          Sets JavaBean property.
static java.lang.String toString(java.lang.Object o)
          Creates formated string that contains names of all readable properties (getXXX() methods) and their values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanUtil

public BeanUtil()
Method Detail

setProperty

public static boolean setProperty(java.lang.Object bean,
                                  java.lang.String name,
                                  java.lang.Object value)
Sets JavaBean property. It finds setXxx() method (i.e. property) and inspects type of its (one and only) parameter. A converter object is called according to this type, value (as Object) is sent to Converter and the result is used for invoking setXxx() method. If a Converter can not be found, object is sended as it is.

Note: there must *not* be more than one setXxx() with the same name. This is ok, since Java doesn't allow more than one getXxx() with the same name.

This method is about much faster then Commons.

Method doesn't throw any exception.

Parameters:
bean - java bean
name - name of the property (not method, i.e. without "set" prefix)
value - new value for the property
Returns:
true if setting succeed, false otherwise

setAllProperties

public static void setAllProperties(java.lang.Object bean,
                                    java.util.Map map)
Sets all bean properties that are defined in the map for faster work.

Parameters:
bean - java bean which properties has to be set
map - map to read from
See Also:
setProperty(java.lang.Object, java.lang.String, java.lang.Object), getAllProperties(java.lang.Object)

getProperty

public static java.lang.Object getProperty(java.lang.Object bean,
                                           java.lang.String name)
Gets the value from the bean paramter. If parameter doesn't exists in the bean, null is returned.

Method doesn't throw any exception.

Parameters:
bean - java bean
name - property name
Returns:
null if parameter doesn't exist, otherwise parameters value.

getAllProperties

public static java.util.Map getAllProperties(java.lang.Object bean)
Returns all bean properties (getXxx()) as a Map for easier and faster manipulation. Property names (without 'get') are keys and property values are map values.

Parameters:
bean - java bean which properties to read
Returns:
Map that contains all bean properties values
See Also:
getProperty(java.lang.Object, java.lang.String), setAllProperties(java.lang.Object, java.util.Map)

copyProperties

public static void copyProperties(java.lang.Object source,
                                  java.lang.Object destination)
Copies properties of one bean to another. It itterates all getXXX methods, reads values and populates another bean with setXXX.

Parameters:
source - source bean, one to read properties from
destination - destination bean, to write properties to

forEachProperty

public static void forEachProperty(java.lang.Object bean,
                                   PropertyCallback fepcb)
Iterrates all read properties and executes callback method on it

Parameters:
bean - bean that will be modified
fepcb - modifier that will be called back for properties modifications

load

public static void load(java.lang.Object bean,
                        java.lang.Object obj)
Populates bean from given object by using a loader for given objects type. Only read-write properties can be loaded.

Parameters:
bean - java bean
obj - object

toString

public static java.lang.String toString(java.lang.Object o)
Creates formated string that contains names of all readable properties (getXXX() methods) and their values. For debugging purposes only.

Parameters:
o - java bean
Returns:
formated String that contains info about all beans properties.


Jodd v0.24.5 Javadoc