org.omnaest.utils.beans.result
Class BeanPropertyAccessor<B>

java.lang.Object
  extended by org.omnaest.utils.beans.result.BeanPropertyAccessor<B>
Type Parameters:
B - Java Bean type
All Implemented Interfaces:
Serializable

public class BeanPropertyAccessor<B>
extends Object
implements Serializable

JavaBean property access object for Methods and Field of a special Java Bean type.

The BeanPropertyAccessor is Serializable and immutable. Changing the BeanPropertyAccessor.PropertyAccessType e.g. does return a new instance. See newBeanPropertyAccessorWithPropertyAccessType(PropertyAccessType).

The default BeanPropertyAccessor.PropertyAccessType is on property level.

Author:
Omnaest
See Also:
BeanUtils, BeanProperty, Serialized Form

Nested Class Summary
static class BeanPropertyAccessor.PropertyAccessType
           
 
Field Summary
protected  Class<B> beanType
           
protected  Field field
           
protected  Method methodGetter
           
protected  Method methodSetter
           
protected  BeanPropertyAccessor.PropertyAccessType propertyAccessType
           
protected  String propertyName
           
 
Constructor Summary
BeanPropertyAccessor(Field field, Method methodGetter, Method methodSetter, String propertyName, Class<B> beanType)
           
BeanPropertyAccessor(Field field, Method methodGetter, Method methodSetter, String propertyName, Class<B> beanType, BeanPropertyAccessor.PropertyAccessType propertyAccessType)
           
 
Method Summary
 boolean copyPropertyValue(B beanSource, B beanDestination)
          Copies the value of one Java Bean for the underlying Java Bean property of this BeanPropertyAccessor to another Java Bean.
 boolean copyPropertyValue(B beanSource, BeanPropertyAccessor<B> beanPropertyAccessorTarget, B beanTarget)
          Copies the addressed bean value of the given source instance using this BeanPropertyAccessor to another instance using the given BeanPropertyAccessor
<A extends Annotation>
A
getAnnotation(Class<A> annotationType)
          Similar to getAnnotation(Class, PropertyAccessType) using the default BeanPropertyAccessor.PropertyAccessType
<A extends Annotation>
A
getAnnotation(Class<A> annotationType, BeanPropertyAccessor.PropertyAccessType propertyAccessType)
          Returns the Annotation instance declared on the field or methods for the related property depending on the BeanPropertyAccessor.PropertyAccessType.
 Class<B> getBeanClass()
          Returns the class object of accepted bean type
 Class<?> getDeclaringPropertyType()
          Similar to getDeclaringPropertyType(PropertyAccessType) using the default BeanPropertyAccessor.PropertyAccessType
 Class<?> getDeclaringPropertyType(BeanPropertyAccessor.PropertyAccessType... propertyAccessTypes)
          Similar to getDeclaringPropertyType(PropertyAccessType) but using all given BeanPropertyAccessor.PropertyAccessTypes.
 Class<?> getDeclaringPropertyType(BeanPropertyAccessor.PropertyAccessType propertyAccessType)
          Determines the Class type for the property.
 Field getField()
          Returns the underlying Field
 Method getMethodGetter()
          Returns the getter Method
 Method getMethodSetter()
          Returns the setter Method
 String getPropertyName()
          Returns the name of the property
 Object getPropertyValue(B bean)
          Returns the property value for the underlying Java Bean property from the given Java Bean object.
 Object getPropertyValue(B bean, BeanPropertyAccessor.PropertyAccessType... propertyAccessTypes)
          Similar to getPropertyValue(Object, ExceptionHandler, PropertyAccessType...) with no ExceptionHandler
 Object getPropertyValue(B bean, BeanPropertyAccessor.PropertyAccessType propertyAccessType)
          Returns the property value for the underlying Java Bean property from the given Java Bean object.
 Object getPropertyValue(B bean, BeanPropertyAccessor.PropertyAccessType propertyAccessType, ExceptionHandler exceptionHandler)
          Returns the property value for the underlying Java Bean property from the given Java Bean object.
 Object getPropertyValue(B bean, ExceptionHandler exceptionHandler, BeanPropertyAccessor.PropertyAccessType... propertyAccessTypes)
          Similar to getPropertyValue(Object, PropertyAccessType, ExceptionHandler) using all the given BeanPropertyAccessor.PropertyAccessTypes
 Tuple2<Object,Boolean> getPropertyValueAndSuccess(B bean, BeanPropertyAccessor.PropertyAccessType propertyAccessType, ExceptionHandler exceptionHandler)
          Returns the property value for the underlying Java Bean property from the given Java Bean object.
 boolean hasField()
          Returns true if an underlying field is available
 boolean hasGetter()
          Returns true, if the method has no parameters but a return type.
 boolean hasGetterAndSetter()
          Returns true, if the underlying Field has a getter and a setter method.
 boolean hasGetterOrSetter()
          Returns true, if the underlying Field has at least a getter or a setter method or both available.
 boolean hasSetter()
          Is true, if a method has only one parameter and begins with "set".
 boolean isReadable()
          Returns true if the BeanPropertyAccessor.PropertyAccessType is field and the class definition has a field or if BeanPropertyAccessor.PropertyAccessType is property and the class declares a getter.
 boolean isReadAndWritable()
          Returns true if the BeanPropertyAccessor.PropertyAccessType is field and the class definition has a field or if BeanPropertyAccessor.PropertyAccessType is property and the class declares a getter and a setter.
 boolean isWritable()
          Returns true if the BeanPropertyAccessor.PropertyAccessType is field and the class definition has a field or if BeanPropertyAccessor.PropertyAccessType is property and the class declares a setter.
static
<B> BeanPropertyAccessor<B>
merge(BeanPropertyAccessor<B> beanPropertyAccessorA, BeanPropertyAccessor<B> beanPropertyAccessorB)
          Merges two BeanPropertyAccessor instances into one if the underlying Class and property are equal.
 BeanPropertyAccessor<B> newBeanPropertyAccessorWithPropertyAccessType(BeanPropertyAccessor.PropertyAccessType propertyAccessType)
          Returns a new BeanPropertyAccessor which has the given BeanPropertyAccessor.PropertyAccessType set.
 Object readResolve()
           
 boolean setPropertyValue(B bean, Object value)
          Sets the property value for the underlying Java Bean property for the given Java Bean object.
 boolean setPropertyValue(B bean, Object value, BeanPropertyAccessor.PropertyAccessType... propertyAccessTypes)
          Similar to setPropertyValue(Object, Object, PropertyAccessType) using all given BeanPropertyAccessor.PropertyAccessTypes
 boolean setPropertyValue(B bean, Object value, BeanPropertyAccessor.PropertyAccessType propertyAccessType)
          Sets the property value for the underlying Java Bean property for the given Java Bean object using the given BeanPropertyAccessor.PropertyAccessType.
 boolean setPropertyValue(B bean, Object value, BeanPropertyAccessor.PropertyAccessType propertyAccessType, ExceptionHandler exceptionHandler)
          Sets the property value for the underlying Java Bean property for the given Java Bean object using the given BeanPropertyAccessor.PropertyAccessType.
 boolean setPropertyValue(B bean, Object value, ExceptionHandler exceptionHandler, BeanPropertyAccessor.PropertyAccessType... propertyAccessTypes)
          Similar to setPropertyValue(Object, Object, PropertyAccessType, ExceptionHandler) using all given BeanPropertyAccessor.PropertyAccessTypes
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

propertyName

protected String propertyName

field

protected transient Field field

methodGetter

protected transient Method methodGetter

methodSetter

protected transient Method methodSetter

beanType

protected Class<B> beanType

propertyAccessType

protected BeanPropertyAccessor.PropertyAccessType propertyAccessType
Constructor Detail

BeanPropertyAccessor

public BeanPropertyAccessor(Field field,
                            Method methodGetter,
                            Method methodSetter,
                            String propertyName,
                            Class<B> beanType)
Parameters:
field -
methodGetter -
methodSetter -
propertyName -
beanType -
See Also:
BeanPropertyAccessor

BeanPropertyAccessor

public BeanPropertyAccessor(Field field,
                            Method methodGetter,
                            Method methodSetter,
                            String propertyName,
                            Class<B> beanType,
                            BeanPropertyAccessor.PropertyAccessType propertyAccessType)
Parameters:
field -
methodGetter -
methodSetter -
propertyName -
beanType -
propertyAccessType -
See Also:
BeanPropertyAccessor
Method Detail

copyPropertyValue

public boolean copyPropertyValue(B beanSource,
                                 B beanDestination)
Copies the value of one Java Bean for the underlying Java Bean property of this BeanPropertyAccessor to another Java Bean.

Parameters:
beanSource -
beanDestination -
Returns:
true, if no error occurs

copyPropertyValue

public boolean copyPropertyValue(B beanSource,
                                 BeanPropertyAccessor<B> beanPropertyAccessorTarget,
                                 B beanTarget)
Copies the addressed bean value of the given source instance using this BeanPropertyAccessor to another instance using the given BeanPropertyAccessor

Parameters:
beanSource -
beanPropertyAccessorTarget -
beanTarget -
Returns:
true, if anything copied

getDeclaringPropertyType

public Class<?> getDeclaringPropertyType(BeanPropertyAccessor.PropertyAccessType... propertyAccessTypes)
Similar to getDeclaringPropertyType(PropertyAccessType) but using all given BeanPropertyAccessor.PropertyAccessTypes.

Returns:

getDeclaringPropertyType

public Class<?> getDeclaringPropertyType()
Similar to getDeclaringPropertyType(PropertyAccessType) using the default BeanPropertyAccessor.PropertyAccessType

Returns:

getDeclaringPropertyType

public Class<?> getDeclaringPropertyType(BeanPropertyAccessor.PropertyAccessType propertyAccessType)
Determines the Class type for the property. This is also known as the declaring return or field type not the type of any actual object instance.

Depending on the BeanPropertyAccessor.PropertyAccessType only the field type or the method return or parameter type is investigated, not all at the same time.

Parameters:
propertyAccessType - BeanPropertyAccessor.PropertyAccessType
Returns:

getAnnotation

public <A extends Annotation> A getAnnotation(Class<A> annotationType)
Similar to getAnnotation(Class, PropertyAccessType) using the default BeanPropertyAccessor.PropertyAccessType

Parameters:
annotationType - Class of the Annotation
Returns:
the Annotation instance or null

getAnnotation

public <A extends Annotation> A getAnnotation(Class<A> annotationType,
                                              BeanPropertyAccessor.PropertyAccessType propertyAccessType)
Returns the Annotation instance declared on the field or methods for the related property depending on the BeanPropertyAccessor.PropertyAccessType.

Parameters:
annotationType - Class of the Annotation
propertyAccessType - BeanPropertyAccessor.PropertyAccessType
Returns:
the Annotation instance or null

getPropertyValue

public Object getPropertyValue(B bean,
                               BeanPropertyAccessor.PropertyAccessType propertyAccessType)
Returns the property value for the underlying Java Bean property from the given Java Bean object.

Parameters:
bean -
propertyAccessType -
Returns:
value or null if no value could be resolved
See Also:
BeanPropertyAccessor.PropertyAccessType

getPropertyValue

public Object getPropertyValue(B bean,
                               BeanPropertyAccessor.PropertyAccessType... propertyAccessTypes)
Similar to getPropertyValue(Object, ExceptionHandler, PropertyAccessType...) with no ExceptionHandler

Parameters:
bean -
propertyAccessTypes -
Returns:

getPropertyValue

public Object getPropertyValue(B bean,
                               ExceptionHandler exceptionHandler,
                               BeanPropertyAccessor.PropertyAccessType... propertyAccessTypes)
Similar to getPropertyValue(Object, PropertyAccessType, ExceptionHandler) using all the given BeanPropertyAccessor.PropertyAccessTypes

Parameters:
bean -
exceptionHandler -
propertyAccessTypes -
Returns:

getPropertyValue

public Object getPropertyValue(B bean,
                               BeanPropertyAccessor.PropertyAccessType propertyAccessType,
                               ExceptionHandler exceptionHandler)
Returns the property value for the underlying Java Bean property from the given Java Bean object.

Parameters:
bean -
propertyAccessType -
exceptionHandler -
Returns:
value or null if no value could be resolved
See Also:
BeanPropertyAccessor.PropertyAccessType

getPropertyValueAndSuccess

public Tuple2<Object,Boolean> getPropertyValueAndSuccess(B bean,
                                                         BeanPropertyAccessor.PropertyAccessType propertyAccessType,
                                                         ExceptionHandler exceptionHandler)
Returns the property value for the underlying Java Bean property from the given Java Bean object.

Parameters:
bean -
propertyAccessType -
exceptionHandler -
Returns:
value or null if no value could be resolved
See Also:
BeanPropertyAccessor.PropertyAccessType

getPropertyValue

public Object getPropertyValue(B bean)
Returns the property value for the underlying Java Bean property from the given Java Bean object.

Parameters:
bean -
Returns:
value or null if no value could be resolved
See Also:
newBeanPropertyAccessorWithPropertyAccessType(PropertyAccessType)

setPropertyValue

public boolean setPropertyValue(B bean,
                                Object value)
Sets the property value for the underlying Java Bean property for the given Java Bean object.

Parameters:
bean -
value -
Returns:
true if no error occurs

setPropertyValue

public boolean setPropertyValue(B bean,
                                Object value,
                                BeanPropertyAccessor.PropertyAccessType propertyAccessType)
Sets the property value for the underlying Java Bean property for the given Java Bean object using the given BeanPropertyAccessor.PropertyAccessType.

Parameters:
bean -
value -
propertyAccessType -
Returns:
true if no error occurs

setPropertyValue

public boolean setPropertyValue(B bean,
                                Object value,
                                BeanPropertyAccessor.PropertyAccessType... propertyAccessTypes)
Similar to setPropertyValue(Object, Object, PropertyAccessType) using all given BeanPropertyAccessor.PropertyAccessTypes

Parameters:
bean -
value -
propertyAccessTypes -
Returns:

setPropertyValue

public boolean setPropertyValue(B bean,
                                Object value,
                                ExceptionHandler exceptionHandler,
                                BeanPropertyAccessor.PropertyAccessType... propertyAccessTypes)
Similar to setPropertyValue(Object, Object, PropertyAccessType, ExceptionHandler) using all given BeanPropertyAccessor.PropertyAccessTypes

Parameters:
bean -
value -
exceptionHandler - ExceptionHandler
propertyAccessTypes -
Returns:

setPropertyValue

public boolean setPropertyValue(B bean,
                                Object value,
                                BeanPropertyAccessor.PropertyAccessType propertyAccessType,
                                ExceptionHandler exceptionHandler)
Sets the property value for the underlying Java Bean property for the given Java Bean object using the given BeanPropertyAccessor.PropertyAccessType.

Parameters:
bean -
value -
propertyAccessType -
exceptionHandler -
Returns:
true if no error occurs

hasGetterOrSetter

public boolean hasGetterOrSetter()
Returns true, if the underlying Field has at least a getter or a setter method or both available.

Returns:

hasGetterAndSetter

public boolean hasGetterAndSetter()
Returns true, if the underlying Field has a getter and a setter method.

Returns:

isReadAndWritable

public boolean isReadAndWritable()
Returns true if the BeanPropertyAccessor.PropertyAccessType is field and the class definition has a field or if BeanPropertyAccessor.PropertyAccessType is property and the class declares a getter and a setter.

Returns:

isReadable

public boolean isReadable()
Returns true if the BeanPropertyAccessor.PropertyAccessType is field and the class definition has a field or if BeanPropertyAccessor.PropertyAccessType is property and the class declares a getter.

Returns:

isWritable

public boolean isWritable()
Returns true if the BeanPropertyAccessor.PropertyAccessType is field and the class definition has a field or if BeanPropertyAccessor.PropertyAccessType is property and the class declares a setter.

Returns:

hasField

public boolean hasField()
Returns true if an underlying field is available

Returns:

hasGetter

public boolean hasGetter()
Returns true, if the method has no parameters but a return type.

Returns:

hasSetter

public boolean hasSetter()
Is true, if a method has only one parameter and begins with "set". A return type is optional.

Returns:

merge

public static <B> BeanPropertyAccessor<B> merge(BeanPropertyAccessor<B> beanPropertyAccessorA,
                                                BeanPropertyAccessor<B> beanPropertyAccessorB)
Merges two BeanPropertyAccessor instances into one if the underlying Class and property are equal.

Type Parameters:
B -
Parameters:
beanPropertyAccessorA -
beanPropertyAccessorB -
Returns:

getBeanClass

public Class<B> getBeanClass()
Returns the class object of accepted bean type

Returns:

getPropertyName

public String getPropertyName()
Returns the name of the property

Returns:

getField

public Field getField()
Returns the underlying Field

Returns:

getMethodGetter

public Method getMethodGetter()
Returns the getter Method

Returns:

getMethodSetter

public Method getMethodSetter()
Returns the setter Method

Returns:

toString

public String toString()
Overrides:
toString in class Object

newBeanPropertyAccessorWithPropertyAccessType

public BeanPropertyAccessor<B> newBeanPropertyAccessorWithPropertyAccessType(BeanPropertyAccessor.PropertyAccessType propertyAccessType)
Returns a new BeanPropertyAccessor which has the given BeanPropertyAccessor.PropertyAccessType set. This does not changed the current instance.

Parameters:
propertyAccessType - BeanPropertyAccessor.PropertyAccessType
Returns:
this

readResolve

public Object readResolve()
                   throws ObjectStreamException
Throws:
ObjectStreamException


Copyright © 2013. All Rights Reserved.