org.omnaest.utils.beans
Class BeanUtils

java.lang.Object
  extended by org.omnaest.utils.beans.BeanUtils

public class BeanUtils
extends Object

Helper class for Java beans.

Author:
Omnaest

Nested Class Summary
static interface BeanUtils.BeanConverter<FROM,TO>
          Converter interface which offers a BeanUtils.BeanConverter.convert(Object) method which has to convert one bean type to another.
 
Constructor Summary
BeanUtils()
           
 
Method Summary
static BeanMethodInformation beanMethodInformation(Method method)
          Returns a BeanMethodInformation object determined for the given Method.
static Set<BeanMethodInformation> beanMethodInformationSet(Class<?> clazz)
          Returns a set of BeanMethodInformation instances for all methods of a given Class.
static
<B> BeanPropertyAccessor<B>
beanPropertyAccessor(Class<B> beanClass, Field field)
          Returns a BeanPropertyAccessor object determined for the given Field.
static
<B> BeanPropertyAccessor<B>
beanPropertyAccessor(Class<B> beanClass, Method method)
          Returns a BeanPropertyAccessor object determined for the given Method.
static
<B> BeanPropertyAccessor<B>
beanPropertyAccessor(Class<B> beanClass, String propertyName)
          Returns a BeanPropertyAccessor object determined for the given property name
static
<B> BeanPropertyAccessors<B>
beanPropertyAccessors(Class<B> beanClass)
          Returns the BeanPropertyAccessors for a given Java Bean type
static
<B> Set<BeanPropertyAccessor<B>>
beanPropertyAccessorSet(Class<B> beanClass)
          Returns a set of BeanPropertyAccessor instances for all Java Bean properties of the given Class
static
<B> B
cloneBean(B bean)
          Clones the given JavaBean by using the standard contructor to create a new instance and then to populate all properties using copyPropertyValues(Object, Object) to the new created instance.
static
<B> B
cloneBeanUsingInstanceOfMap(B bean)
          Clones the given Java bean using a Map to store all properties and providing a proxy which accesses this underlying Map.
static
<B> B
cloneBeanUsingInstanceOfMap(B bean, boolean underlyingMapAware)
          Clones the given Java bean using a Map to store all properties and providing a proxy which accesses this underlying Map.
static
<B> B
cloneBeanUsingNestedfMap(B bean)
          Clones the given Java bean using BeanToNestedMapConverter.clone(Object)
static
<B> B
cloneBeanUsingNestedfMap(B bean, Map<Class<?>,Class<?>> sourceTypeTodestinationTypeMap)
          Clones the given Java bean using a BeanToNestedMapConverter.clone(Object).
static
<FROM,TO> TO
convert(FROM sourceBean, BeanUtils.BeanConverter<FROM,TO> beanConverter)
          Converts a given bean into another using a BeanUtils.BeanConverter.
static
<S,D> void
copyPropertyValues(S beanSource, D beanDestination)
          Copies the property values from one Java Bean to another.
static
<S,D> void
copyPropertyValues(S beanSource, D beanDestination, Collection<String> propertyNameCollection)
          Copies the property values from one Java Bean to another but only for the given property names.
static
<S,D> void
copyPropertyValues(S beanSource, D beanDestination, String... propertyNames)
          Copies the property values for the given property names from one Java Bean to another.
static Map<String,BeanMethodInformation> methodNameToBeanMethodInformationMap(Class<?> type)
          Determines a Map with the referenced Method.getName()s as keys and the respective BeanMethodInformation name.
static Map<String,BeanMethodInformation> methodNameToBeanMethodInformationMap(Method... methods)
          Determines a Map with all Method.getName()s and the respective BeanMethodInformation instances.
static int numberOfProperties(Class<?> beanClass)
          Returns the number of available Java Bean properties for the given Java Bean type.
static Set<String> propertyNameSetForMethodAccess(Class<?> clazz)
          Determines the property names of a given bean class which are addressed by getter or setter.
static String[] propertyNamesForMethodAccess(Class<?> type)
          Determines the property names of a given bean class which are addressed by getter or setter.
static Map<String,Set<BeanMethodInformation>> propertyNameToBeanMethodInformationMap(Class<?> clazz)
          Determines a Map with the referenced field names as keys and a Set of BeanMethodInformation for every field name.
static Map<String,Set<BeanMethodInformation>> propertyNameToBeanMethodInformationMap(Method... methods)
          Determines a map with the referenced field names as keys and a Set of BeanMethodInformation for every field name.
static
<B> Map<String,BeanPropertyAccessor<B>>
propertyNameToBeanPropertyAccessorMap(Class<B> beanClass)
          Returns a map of property names and BeanPropertyAccessor instances for all Java Bean properties of the given Class.
static
<B> Map<String,BeanPropertyAccessor<B>>
propertyNameToBeanPropertyAccessorMap(Class<B> beanClass, Field... fields)
          Returns a map of field names and BeanPropertyAccessor instances for all Java Bean properties related to at least one of the given Fields.
static
<B> Map<String,BeanPropertyAccessor<B>>
propertyNameToBeanPropertyAccessorMap(Class<B> beanClass, Method... methods)
          Returns a map of field names and BeanPropertyAccessor instances for all Java Bean properties related to at least one of the given Methods.
static
<B,A extends Annotation>
Map<String,A>
propertyNameToBeanPropertyAnnotationMap(Class<B> beanClass, Class<A> annotationType)
          Returns a Map with all properties of a given Java Bean class and an instance of the given Annotation type if the respective property does have one.
static
<B> Map<String,Set<Annotation>>
propertyNameToBeanPropertyAnnotationSetMap(Class<B> beanClass)
          Returns a Map with all property names of the given Java Bean and a Set of all available annotations for the properties, including the field, getter and setter methods.
static
<B,V> Map<String,V>
propertyNameToBeanPropertyValueMap(B bean)
          Returns a map of property names and their current values for all Java Bean properties or the given Java Bean object.
static
<B,V> Map<String,V>
propertyNameToBeanPropertyValueMap(B bean, Collection<String> propertyNameCollection)
          Returns a map of property names and their current values for the Java Bean properties determined by the given property names for the given Java Bean object.
static
<B,V> Map<String,V>
propertyNameToBeanPropertyValueMap(B bean, String... propertyNames)
          Returns a Map of property names and their current values for the Java Bean properties determined by the given property names for the given Java Bean object.
static
<B> Map<Class<?>,Set<BeanPropertyAccessor<B>>>
propertyTypeToBeanPropertyAccessorSetMap(Class<B> beanClass)
          Returns a map of property types for all Java Bean properties of the given Class and a Set of BeanPropertyAccessor instances for each type.
static
<B> List<B>
propertyValueList(B bean)
          Returns as List of the values of all available properties of the given JavaBean object instance.
static
<B> List<B>
propertyValueList(B bean, Collection<String> propertyNameCollection)
          Returns an new ordered List of all property values for the given property names from the given Java Bean object
static
<B> List<B>
propertyValueList(B bean, String... propertyNames)
          Returns an new ordered List of all property values for the given property names from the given Java Bean object
static
<B> Map<String,Object>
transformBeanIntoMap(B bean)
          Returns a new Map instance which contains the property names as keys and the values of the properties as map values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanUtils

public BeanUtils()
Method Detail

transformBeanIntoMap

public static <B> Map<String,Object> transformBeanIntoMap(B bean)
Returns a new Map instance which contains the property names as keys and the values of the properties as map values. Modifications to the returned map will have no impact on the original Java Bean object.

Type Parameters:
B -
Parameters:
bean -
Returns:
new LinkedHashMap
See Also:
PropertynameMapToTypeAdapter

numberOfProperties

public static int numberOfProperties(Class<?> beanClass)
Returns the number of available Java Bean properties for the given Java Bean type.

Parameters:
beanClass -
Returns:

beanPropertyAccessorSet

public static <B> Set<BeanPropertyAccessor<B>> beanPropertyAccessorSet(Class<B> beanClass)
Returns a set of BeanPropertyAccessor instances for all Java Bean properties of the given Class

Parameters:
beanClass -
Returns:
See Also:
propertyNameToBeanPropertyAccessorMap(Class), beanPropertyAccessor(Class, Field)

beanPropertyAccessors

public static <B> BeanPropertyAccessors<B> beanPropertyAccessors(Class<B> beanClass)
Returns the BeanPropertyAccessors for a given Java Bean type

Type Parameters:
B -
Parameters:
beanClass -
Returns:

propertyValueList

public static <B> List<B> propertyValueList(B bean,
                                            String... propertyNames)
Returns an new ordered List of all property values for the given property names from the given Java Bean object

Parameters:
bean -
propertyNames -
Returns:

propertyValueList

public static <B> List<B> propertyValueList(B bean,
                                            Collection<String> propertyNameCollection)
Returns an new ordered List of all property values for the given property names from the given Java Bean object

Parameters:
bean -
propertyNameCollection -
Returns:

propertyValueList

public static <B> List<B> propertyValueList(B bean)
Returns as List of the values of all available properties of the given JavaBean object instance.

Parameters:
bean -
Returns:
See Also:
propertyValueList(Object, String...)

propertyNameToBeanPropertyAnnotationMap

public static <B,A extends Annotation> Map<String,A> propertyNameToBeanPropertyAnnotationMap(Class<B> beanClass,
                                                                                             Class<A> annotationType)
Returns a Map with all properties of a given Java Bean class and an instance of the given Annotation type if the respective property does have one. Otherwise the map contains a key with a null value.

Parameters:
beanClass -
annotationType -
Returns:
See Also:
propertyNameToBeanPropertyAnnotationSetMap(Class)

propertyNameToBeanPropertyAnnotationSetMap

public static <B> Map<String,Set<Annotation>> propertyNameToBeanPropertyAnnotationSetMap(Class<B> beanClass)
Returns a Map with all property names of the given Java Bean and a Set of all available annotations for the properties, including the field, getter and setter methods.

Parameters:
beanClass -
Returns:

propertyNameToBeanPropertyValueMap

public static <B,V> Map<String,V> propertyNameToBeanPropertyValueMap(B bean,
                                                                     String... propertyNames)
Returns a Map of property names and their current values for the Java Bean properties determined by the given property names for the given Java Bean object. The Map will be ordered in the same way than the given property names

Type Parameters:
B - : type of the bean
V - : type of the values
Parameters:
bean -
propertyNames -
Returns:

propertyNameToBeanPropertyValueMap

public static <B,V> Map<String,V> propertyNameToBeanPropertyValueMap(B bean,
                                                                     Collection<String> propertyNameCollection)
Returns a map of property names and their current values for the Java Bean properties determined by the given property names for the given Java Bean object. The Map keys and values will have the same order as the given property names.

Type Parameters:
B - : type of the bean
V - : type of the values
Parameters:
bean -
propertyNameCollection -
Returns:

propertyNameToBeanPropertyValueMap

public static <B,V> Map<String,V> propertyNameToBeanPropertyValueMap(B bean)
Returns a map of property names and their current values for all Java Bean properties or the given Java Bean object.

Type Parameters:
B - : type of the bean
V - : type of the values
Parameters:
bean -
Returns:

propertyNameToBeanPropertyAccessorMap

public static <B> Map<String,BeanPropertyAccessor<B>> propertyNameToBeanPropertyAccessorMap(Class<B> beanClass,
                                                                                            Field... fields)
Returns a map of field names and BeanPropertyAccessor instances for all Java Bean properties related to at least one of the given Fields.

Type Parameters:
B -
Parameters:
beanClass -
fields -
Returns:

propertyNameToBeanPropertyAccessorMap

public static <B> Map<String,BeanPropertyAccessor<B>> propertyNameToBeanPropertyAccessorMap(Class<B> beanClass,
                                                                                            Method... methods)
Returns a map of field names and BeanPropertyAccessor instances for all Java Bean properties related to at least one of the given Methods.

Type Parameters:
B -
Parameters:
beanClass -
methods -
Returns:

propertyTypeToBeanPropertyAccessorSetMap

public static <B> Map<Class<?>,Set<BeanPropertyAccessor<B>>> propertyTypeToBeanPropertyAccessorSetMap(Class<B> beanClass)
Returns a map of property types for all Java Bean properties of the given Class and a Set of BeanPropertyAccessor instances for each type.

Parameters:
beanClass -
Returns:
See Also:
beanPropertyAccessorSet(Class), propertyNameToBeanPropertyAccessorMap(Class)

propertyNameToBeanPropertyAccessorMap

public static <B> Map<String,BeanPropertyAccessor<B>> propertyNameToBeanPropertyAccessorMap(Class<B> beanClass)
Returns a map of property names and BeanPropertyAccessor instances for all Java Bean properties of the given Class. The properties are in no order.

Parameters:
beanClass -
Returns:
See Also:
beanPropertyAccessorSet(Class)

beanPropertyAccessor

public static <B> BeanPropertyAccessor<B> beanPropertyAccessor(Class<B> beanClass,
                                                               String propertyName)
Returns a BeanPropertyAccessor object determined for the given property name

Parameters:
beanClass -
propertyName -
Returns:
See Also:
beanPropertyAccessor(Class, Field), beanPropertyAccessorSet(Class), propertyNameToBeanPropertyAccessorMap(Class)

beanPropertyAccessor

public static <B> BeanPropertyAccessor<B> beanPropertyAccessor(Class<B> beanClass,
                                                               Field field)
Returns a BeanPropertyAccessor object determined for the given Field.

Parameters:
beanClass -
field -
Returns:
See Also:
beanPropertyAccessor(Class, Method), beanPropertyAccessor(Class, String), beanPropertyAccessorSet(Class), propertyNameToBeanPropertyAccessorMap(Class)

beanPropertyAccessor

public static <B> BeanPropertyAccessor<B> beanPropertyAccessor(Class<B> beanClass,
                                                               Method method)
Returns a BeanPropertyAccessor object determined for the given Method.

Parameters:
beanClass -
method -
Returns:
See Also:
beanPropertyAccessor(Class, Field), beanPropertyAccessorSet(Class), propertyNameToBeanPropertyAccessorMap(Class)

beanMethodInformationSet

public static Set<BeanMethodInformation> beanMethodInformationSet(Class<?> clazz)
Returns a set of BeanMethodInformation instances for all methods of a given Class.

Parameters:
clazz -
Returns:

propertyNameToBeanMethodInformationMap

public static Map<String,Set<BeanMethodInformation>> propertyNameToBeanMethodInformationMap(Class<?> clazz)
Determines a Map with the referenced field names as keys and a Set of BeanMethodInformation for every field name.

Parameters:
clazz -
Returns:

methodNameToBeanMethodInformationMap

public static Map<String,BeanMethodInformation> methodNameToBeanMethodInformationMap(Class<?> type)
Determines a Map with the referenced Method.getName()s as keys and the respective BeanMethodInformation name.

Parameters:
type -
Returns:

propertyNameToBeanMethodInformationMap

public static Map<String,Set<BeanMethodInformation>> propertyNameToBeanMethodInformationMap(Method... methods)
Determines a map with the referenced field names as keys and a Set of BeanMethodInformation for every field name. The map keys are in no order.

Parameters:
methods -
Returns:

methodNameToBeanMethodInformationMap

public static Map<String,BeanMethodInformation> methodNameToBeanMethodInformationMap(Method... methods)
Determines a Map with all Method.getName()s and the respective BeanMethodInformation instances.

Parameters:
methods -
Returns:

beanMethodInformation

public static BeanMethodInformation beanMethodInformation(Method method)
Returns a BeanMethodInformation object determined for the given Method.


convert

public static <FROM,TO> TO convert(FROM sourceBean,
                                   BeanUtils.BeanConverter<FROM,TO> beanConverter)
Converts a given bean into another using a BeanUtils.BeanConverter.
Exceptions are catched and ignored by this method.

Parameters:
sourceBean -
beanConverter -
Returns:
a converted Java bean instance

copyPropertyValues

public static <S,D> void copyPropertyValues(S beanSource,
                                            D beanDestination)
Copies the property values from one Java Bean to another.

Type Parameters:
S -
D -
Parameters:
beanSource -
beanDestination -

copyPropertyValues

public static <S,D> void copyPropertyValues(S beanSource,
                                            D beanDestination,
                                            String... propertyNames)
Copies the property values for the given property names from one Java Bean to another.

Type Parameters:
S -
D -
Parameters:
beanSource -
beanDestination -
propertyNames -

copyPropertyValues

public static <S,D> void copyPropertyValues(S beanSource,
                                            D beanDestination,
                                            Collection<String> propertyNameCollection)
Copies the property values from one Java Bean to another but only for the given property names.

Type Parameters:
S -
D -
Parameters:
beanSource -
beanDestination -
propertyNameCollection -

propertyNameSetForMethodAccess

public static Set<String> propertyNameSetForMethodAccess(Class<?> clazz)
Determines the property names of a given bean class which are addressed by getter or setter.

Parameters:
clazz -
Returns:
Set
See Also:
propertyNamesForMethodAccess(Class)

propertyNamesForMethodAccess

public static String[] propertyNamesForMethodAccess(Class<?> type)
Determines the property names of a given bean class which are addressed by getter or setter. Returns always an array, even if no property name could be determined.

Parameters:
type -
Returns:
See Also:
propertyNameSetForMethodAccess(Class)

cloneBean

public static <B> B cloneBean(B bean)
Clones the given JavaBean by using the standard contructor to create a new instance and then to populate all properties using copyPropertyValues(Object, Object) to the new created instance.

Parameters:
bean -
Returns:

cloneBeanUsingInstanceOfMap

public static <B> B cloneBeanUsingInstanceOfMap(B bean)
Clones the given Java bean using a Map to store all properties and providing a proxy which accesses this underlying Map.

Parameters:
bean -
Returns:

cloneBeanUsingInstanceOfMap

public static <B> B cloneBeanUsingInstanceOfMap(B bean,
                                                boolean underlyingMapAware)
Clones the given Java bean using a Map to store all properties and providing a proxy which accesses this underlying Map.

Parameters:
bean -
underlyingMapAware -
Returns:

cloneBeanUsingNestedfMap

public static <B> B cloneBeanUsingNestedfMap(B bean)
Clones the given Java bean using BeanToNestedMapConverter.clone(Object)

Parameters:
bean -
Returns:

cloneBeanUsingNestedfMap

public static <B> B cloneBeanUsingNestedfMap(B bean,
                                             Map<Class<?>,Class<?>> sourceTypeTodestinationTypeMap)
Clones the given Java bean using a BeanToNestedMapConverter.clone(Object). Offers a Map parameter which needs source types and destination types. For all found source types the destination types are used for instantiation. Missing properties will just be ignored and no execpetion is thrown.

Parameters:
bean -
sourceTypeTodestinationTypeMap -
Returns:


Copyright © 2013. All Rights Reserved.