public final class Property
extends java.lang.Object
implements java.lang.reflect.AnnotatedElement
Property provides instrospected information about, and dynamic
access to, a single property of a Bean.
Accessors are matched based on the "get", "is" (for boolean)
and "set" Method prefix. The following patterns are
automatically discovered as valid property accessors:
- Read Method: T getField() || boolean isField()
- Write Method: void setField(T field)
Static and synthetic members are not considered valid accessors of a property. Read-only and write-only properties are all valid options.
If a backing Field with the same name and type as the
property is present is the declaring class, it will be retrieved not
taking into account its modifier. If it is present in a superclass
instead, it will be retrieved only if it has a not-private modifier.
In any case, accessors don't have to specifically map the backing field with the same name, and the field is only used as an annotations provider, considering that annotating directly the backing field is a well established pattern.
While public properties are guaranteed to have at least a public accessor method (and consequently are always readable or writable), for declared properties it may happen that all the accessors methods are not accessible.
To preserve immutability, this class not provides a way to forcefully alter the property's accessibility. It is responsability of the developer to deal with it using directly the property's accessors.
Introspected information (like type or annotations) is collected from
all the property's Members (and their ancestors), following a
"best-available" design pattern.
Bean,
Bean.getProperties(),
Bean.getProperty(String),
Bean.getDeclaredProperties(),
Bean.getDeclaredProperty(String)| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(java.lang.Object obj)
Compares this property against the specified object.
|
java.lang.Object |
get(java.lang.Object obj)
Returns the value of the representation of this property from the specified object.
|
java.lang.Class<?> |
getActualType()
Returns the actual type of this property, from simple type or generics.
|
<T extends java.lang.annotation.Annotation> |
getAnnotation(java.lang.Class<T> annotationClass)
Returns an annotation collectively present on this property's members, else
null. |
java.lang.annotation.Annotation[] |
getAnnotations()
Returns all the annotations that are collectively present on this
property's members.
|
<T extends java.lang.annotation.Annotation> |
getDeclaredAnnotation(java.lang.Class<T> annotationClass)
Returns this property's annotation for the specified type if
such an annotation is collectively and directly present,
else
null. |
java.lang.annotation.Annotation[] |
getDeclaredAnnotations()
Returns all the annotations that are collectively and directly present
on this property's members.
|
Bean<?> |
getDeclaringBean()
Returns the
Bean object that declares this property. |
java.lang.reflect.Field |
getField()
Returns a copy of this property's
Field, if present, else null. |
java.lang.reflect.Type |
getGenericType()
Returns a
Type object that represents the declared type
for the property represented by this Property object. |
java.lang.String |
getName()
Returns the simple name of this property.
|
java.lang.reflect.Method |
getReadMethod()
Returns a copy of this property's read
Method, if present, else null. |
java.lang.Class<?> |
getType()
Returns the type of this property.
|
java.lang.reflect.Method |
getWriteMethod()
Returns a copy of this property's write
Method, if present, else null. |
int |
hashCode()
Returns a hashcode for this
Property. |
boolean |
isAnnotationPresent(java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Returns true if an annotation for the specified type is
collectively present on this property's members, else false.
|
boolean |
isReadable()
Returns true if this property has a valid getter\isser.
|
boolean |
isWritable()
Returns true if this property has a valid setter.
|
void |
set(java.lang.Object obj,
java.lang.Object value)
Sets a new value to the representation of this property on the specified object.
|
java.lang.String |
toString()
Returns a textual rapresentation of this property.
|
public java.lang.annotation.Annotation[] getAnnotations()
Annotation may be inherited from members declared in a superclass.
In the case of collision of an annotation with the same type in the leaf
class from a different AnnotatedElement, the overriding order is:
field, readMethod and finally writeMethod.
If there are no annotations found, the return value is an array of length 0.
The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.
The elements in the returned array are not sorted and are not in any particular order.
getAnnotations in interface java.lang.reflect.AnnotatedElementpublic java.lang.annotation.Annotation[] getDeclaredAnnotations()
If there are no annotations collectively and directly present on this property's members, the return value is an array of length 0.
The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.
The elements in the returned array are not sorted and are not in any particular order.
getDeclaredAnnotations in interface java.lang.reflect.AnnotatedElementpublic <T extends java.lang.annotation.Annotation> T getAnnotation(java.lang.Class<T> annotationClass)
null.getAnnotation in interface java.lang.reflect.AnnotatedElementT - the type of the annotation to query for and return, if collectively presentannotationClass - the Class object corresponding to the annotation typenulljava.lang.NullPointerException - if the given annotation class is nullpublic <T extends java.lang.annotation.Annotation> T getDeclaredAnnotation(java.lang.Class<T> annotationClass)
null.
This method ignores inherited annotations. and returns null
if no annotations are collectively and directly present on
this property's members.
getDeclaredAnnotation in interface java.lang.reflect.AnnotatedElementT - the type of the annotation to query for and return if
collectively and directly presentannotationClass - the Class corresponding to the annotation typenulljava.lang.NullPointerException - if the given annotation class is nullpublic boolean isAnnotationPresent(java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
isAnnotationPresent in interface java.lang.reflect.AnnotatedElementannotationClass - the Class object corresponding to the annotation typejava.lang.NullPointerException - if the given annotationClass is nullpublic Bean<?> getDeclaringBean()
Bean object that declares this property.public java.lang.String getName()
The name is the lowercased method's name without the relative prefix (get\is\set).
public java.lang.Class<?> getType()
public java.lang.Class<?> getActualType()
The actual type is the plain type for simple property, the generic
element resolved type for List, Iterable, array
and Map property, or null for not resolved generic type.
null if unresolvedpublic java.lang.reflect.Type getGenericType()
Type object that represents the declared type
for the property represented by this Property object.public java.lang.reflect.Method getReadMethod()
Method, if present, else null.null if this property has no valid getter/isserpublic java.lang.reflect.Method getWriteMethod()
Method, if present, else null.null if this proprety has no valid setterpublic java.lang.reflect.Field getField()
Field, if present, else null.null if this property has no fieldpublic boolean isReadable()
public boolean isWritable()
public java.lang.Object get(java.lang.Object obj)
throws ReflectionException
The underlying property's value is obtained trying to invoke the readMethod.
If this Property object has no public readMethod,
it is considered write-only, and the action will be prevented throwing
a ReflectionException.
The value is automatically wrapped in an object if it has a primitive type.
obj - object from which the property's value is to be extractedobjReflectionException - if access to the underlying method throws an exceptionReflectionException - if this property is write-only (no public readMethod)public void set(java.lang.Object obj,
java.lang.Object value)
throws ReflectionException
The underlying property's value will be updated trying
to invoke the writeMethod.
If this Property object has no public writeMethod, it is considered read-only,
and the action will be prevented throwing a ReflectionException.
The new value is automatically unwrapped if the property has a primitive type.
obj - the object whose property should be modifiedvalue - the new value for the property of obj, can be nullReflectionException - if accessing to the underlying method throws an exceptionReflectionException - if this property is read-only (no public writeMethod)public boolean equals(java.lang.Object obj)
Two properties are the same if they were declared in the same bean and have the same name and the same accessors. Under the right circumstances a property and a declared property can be "technically" equivalent.
equals in class java.lang.Objectobj - the object to comparepublic int hashCode()
Property.
This is computed as the exclusive-or of the hashcodes for the underlying
bean's type class name and its simple name.hashCode in class java.lang.Objectpublic java.lang.String toString()
toString in class java.lang.ObjectCopyright © 2015 Minimalcode. All Rights Reserved.