org.spicefactory.cinnamon.reflect
Class Property

java.lang.Object
  extended by org.spicefactory.cinnamon.reflect.Property

public class Property
extends Object

Represents a single Property of a Bean. Provides information on the type of the Property (correctly dealing with generics in contrast to the Introspector in Java SE 5/6 which is basically broken for generic properties). It also provides methods for reading and writing property values including any necessary type conversion (as long as a matching Converter instance was registered in the ReflectionFactory). Property instances must be obtained through Bean instances (which in turn must be obtained through ReflectionFactory instances).

Author:
Jens Halm

Method Summary
 Object convert(Object value)
          Converts the specified value to the target type of this Property if possible.
<A extends Annotation>
A
getAnnotation(Class<A> annotationClass)
          Returns the Annotation for the specified type if present on the getter or setter method of this property, otherwise null.
 Annotation[] getAnnotations()
          Returns all Annotations present on either the getter or setter method of this Property.
 Bean getBean()
          Deprecated. 
 Type getGenericType()
          Returns the resolved generic type of this property.
 String getName()
          The name of the property according to Java Bean conventions.
 Bean getOwnerBean()
          Returns the owner Bean instance this Property belongs to.
 Class<?> getRawType()
          The resolved raw type of this property.
 Bean getTypeAsBean()
          The type of this property as a Bean instance.
 Object getValue(Object bean)
          Returns the value of this property in the given bean instance.
 boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
          Returns true if an annotation for the specified type is present on either the getter or setter method of this Property (or both).
 boolean isReadable()
          Checks if this property is readable.
 boolean isWritable()
          Checks if this property is writable.
 void setValue(Object bean, Object value)
          Set the value of this property on the given bean instance.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

getName

public String getName()
The name of the property according to Java Bean conventions.

Returns:
the name of the property

getBean

@Deprecated
public Bean getBean()
Deprecated. 

Deprecated since the name of the method is ambigous compared to getTypeAsBean, use getOwnerBean instead.

Returns:
the Bean instance this Property belongs to

getOwnerBean

public Bean getOwnerBean()
Returns the owner Bean instance this Property belongs to.

Returns:
the Bean instance this Property belongs to

getGenericType

public Type getGenericType()
Returns the resolved generic type of this property. Resolved means that if this property is declared as a generic type and the declaring class is a supertype of the class that this Property instance belongs to, the resolved type will take into account any TypeVariables that have been resolved in this subtype. An Example: If the property was declared with T getProperty() in a class Bean<T>, and this Property instance belongs to an instance of type SubBean<F extends Person> extends Bean<F>, than the resolved generic type will be the TypeVariable F with bound Person. This differs from the behaviour of the builtin Java Bean Introspector which would still report TypeVariable T for this subtype unless the read and write methods were overwritten in the subtype.

Returns:
the resolved generic type of this property

getRawType

public Class<?> getRawType()
The resolved raw type of this property. Resolved means that if this property is declared as a generic type and the declaring class is a supertype of the class that this Property instance belongs to, the resolved type will take into account any TypeVariables that have been resolved in this subtype. An Example: If the property was declared with T getProperty() in a class Bean<T>, and this Property instance belongs to an instance of type SubBean<F extends Person> extends Bean<F>, than the resolved type will be Person. This differs from the behaviour of the builtin Java Bean Introspector which would still report Object for this subtype unless the read and write methods were overwritten in the subtype.

Returns:
the resolved raw type of this property

getTypeAsBean

public Bean getTypeAsBean()
The type of this property as a Bean instance. Allows to recursively reflect on property types which are themselves Beans.

Returns:
the type of this property as a Bean instance

isReadable

public boolean isReadable()
Checks if this property is readable. For a property named foo there must be a method getFoo without parameters. The only exception is for boolean types for which the read method could also be named isFoo.

Returns:
true if this property is readable

isWritable

public boolean isWritable()
Checks if this property is writable. For a property named foo there must be a method setFoo with exactly one parameter.

Returns:
true if this property is readable

getAnnotation

public <A extends Annotation> A getAnnotation(Class<A> annotationClass)
Returns the Annotation for the specified type if present on the getter or setter method of this property, otherwise null. If the Annotation is present on both the getter and setter method the one found on the getter will be returned.

Parameters:
annotationClass - the Annotation type to look for
Returns:
the Annotation for the specified type or null if no such Annotation exists for this Property

isAnnotationPresent

public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
Returns true if an annotation for the specified type is present on either the getter or setter method of this Property (or both).

Parameters:
annotationClass - the Annotation type to look for
Returns:
true if an annotation for the specified type is present on this Property

getAnnotations

public Annotation[] getAnnotations()
Returns all Annotations present on either the getter or setter method of this Property.

Returns:
all Annotations present on either the getter or setter method of this Property

getValue

public Object getValue(Object bean)
                throws PropertyException
Returns the value of this property in the given bean instance.

Parameters:
bean - the bean from which the property value should be read
Returns:
the value of this property in the given bean instance
Throws:
PropertyException - if this property is write-only or if the getter threw an Exception

setValue

public void setValue(Object bean,
                     Object value)
              throws PropertyException
Set the value of this property on the given bean instance. Any necessary type conversion will be applied before setting the value as long as a matching Converter instance was available in the corresponding ReflectionFactory.

Parameters:
bean - the bean on which the property value should be set
value - the new value of the property
Throws:
PropertyException - if this property is read-only, type conversion failed or the setter method threw an Exception

convert

public Object convert(Object value)
               throws ConversionException
Converts the specified value to the target type of this Property if possible. The returned value is the same as it would be set on a target instance with setValue. This Property must not be read-only, in this case an Excpetion will be thrown.

Parameters:
value - the value to convert to the target type of this Property
Returns:
the converted value
Throws:
ConversionException - if the conversion fails

toString

public String toString()
Overrides:
toString in class Object