Class ELProperty<S,V>
- Type Parameters:
S- the type of source object that thisELPropertyoperates onV- the type of value that thisELPropertyrepresents
Property that allows Java Beans properties of
source objects to be addressed using a simple dot-separated path syntax
within an EL expression. For example, to create a simple property representing
a Person bean's mother's firstName:
ELProperty.create("${mother.firstName}")
Note that BeanProperty is more suitable for
such a simple property.
To create a property representing the concatenation of a Person bean's
firstName and lastName properties:
ELProperty.create("${firstName} ${lastName}");
To create a property that is true or false depending
on whether or not the Person's mother is older than 65:
BeanProperty.create("${mother.age > 65}");
Paths specified in the EL expressions are resolved against the source object with which the property is being used.
An instance of ELProperty is immutable and can be used with
different source objects. When a PropertyStateListener is added to
an ELProperty for a given source object, the ELProperty
starts listening to all objects along the paths in the expression (based on that source object)
for change notification, and reflects any changes by notifying the
listener associated with the property for that source object. So, for example,
if a PropertyStateListener is added to the property from the second example above
for an object Duke, the PropertyStateListener is
notified when either Duke's first name changes, or his last name changes.
If a listener is added to the property from the third example, the PropertyStateListener
is notified when either a change in Duke's mother or Duke's mother's age
results in a change to the result of the expression.
It is very important that any bean properties addressed via a ELProperty
follow the Java Beans specification, including firing property change notification;
otherwise, ELProperty cannot respond to change. As some beans outside
of your control may not follow the Java Beans specification, ELProperty
always checks the BeanAdapterFactory to
see if a delegate provider has been registered to provide a delegate bean to take
the place of an object for a given property. See the
ext package level documentation for more
details.
When there are no PropertyStateListeners installed on an ELProperty
for a given source, all Property methods act by evaluating the full expression,
thereby always providing "live" information.
On the contrary, when there are PropertyStateListeners installed, the beans
along the paths, and the final value, are cached, and only updated upon
notification of change from a bean. Again, this makes it very important that any
bean property that could change along the path fires property change notification.
Note: The setValue method is currently excluded from the previous
assertion; with the exception of checking the cache to determine if the property is
writeable, it always evaluates the entire expression. The result of this is that
when working with paths containing beans that don't fire property change notification,
you can end up with all methods (including getValue) working on cached
information, but setValue working on the live expression. There are plans
to resolve this inconsistency in a future release.
Readability of an ELProperty for a given source is defined as follows:
An ELProperty is readable for a given source if and only if the
following is true for all paths used in the expression:
a) each bean the path, starting with the source, defines a Java Beans getter
method for the the property to be read on it AND b) each bean in the path,
starting with the source and ending with the bean on which we read the final
property, is non-null. The final value being null does not
affect the readability.
So, in the third example given earlier, the ELProperty is readable for Duke when all
of the following are true: Duke defines a Java Beans getter for
mother, Duke's mother defines a Java Beans getter for
age, Duke is non-null, Duke's mother
is non-null. The ELProperty is therefore unreadable when
any of the following is true: Duke does not define a Java Beans
getter for mother, Duke's mother does not define a Java
Beans getter for age, Duke is null,
Duke's mother is null.
Writeability of an ELProperty for a given source is defined as follows:
An ELProperty is writeable for a given source if and only if
a) the EL expression itself is not read-only
(ie. it is a simple expression involving one path such as "${foo.bar.baz}" AND
b) each bean in the path, starting with the source and ending with the bean on
which we set the final property, defines a Java Beans getter method for the
property to be read on it AND c) the bean on which we set the final property
defines a Java Beans setter for the property to be set on it AND d) each bean
in the path, starting with the source and ending with the bean on which we
set the final property, is non-null. The final value being null
does not affect the writeability.
So in the first example given earlier (a simple path), the ELProperty
is writeable for Duke when all of the following are true: Duke defines a Java Beans getter for
mother, Duke's mother defines a Java Beans setter for
firstName, Duke is non-null, Duke's mother
is non-null. The ELProperty is therefore unreadable when
any of the following is true: Duke does not define a Java Beans
getter for mother, Duke's mother does not define a Java
Beans setter for firstName, Duke is null,
Duke's mother is null. The second and third examples above
both represent read-only ELExpressions and are therefore unwritable.
In addition to working on Java Beans properties, any object in the paths
can be an instance of Map. In this case, the Map's get
method is used with the property name as the getter, and the
Map's put method is used with the property name as the setter.
ELProperty can only respond to changes in Maps
if they are instances of ObservableMap.
Some methods in this class document that they can throw
PropertyResolutionException if an exception occurs while trying
to evaluate the expression. The throwing of this exception represents an abnormal
condition and if listeners are installed for the given source object,
leaves the ELProperty in an inconsistent state for that source object.
An ELProperty should not be used again for that same source object
after such an exception without first removing all listeners associated with
the ELProperty for that source object.
-
Method Summary
Modifier and TypeMethodDescriptionstatic final <S,V> ELProperty <S, V> Creates an instance ofELPropertyfor the given expression.static final <S,V> ELProperty <S, V> Creates an instance ofELPropertyfor the given base property and expression.Returns the value of thisPropertyfor the given source.getWriteType(S source) Returns the type of object that is suitable for setting as the value of thisPropertyby calls tosetValue.booleanisReadable(S source) Returns whether or not thePropertyis readable for the given source.booleanisWriteable(S source) Returns whether or not thePropertyis writeable for the given source.protected final voidlisteningStarted(S source) Called when thisPropertyHelperchanges from having no listeners installed for the given source object to having listeners installed for the given source object.protected final voidlisteningStopped(S source) Called when thisPropertyHelperchanges from having listeners installed for the given source object to having no listeners installed for the given source object.voidSets the value of thisPropertyfor the given source.toString()Returns a string representation of theELProperty.Methods inherited from class PropertyHelper
addPropertyStateListener, firePropertyStateChange, getPropertyStateListeners, isListening, removePropertyStateListener
-
Method Details
-
create
Creates an instance ofELPropertyfor the given expression.- Parameters:
expression- the expression- Returns:
- an instance of
ELPropertyfor the given expression - Throws:
IllegalArgumentException- if the path is null or emptyPropertyResolutionException- if there's a problem with the expression
-
create
Creates an instance ofELPropertyfor the given base property and expression. The expression is relative to the value of the base property.- Parameters:
baseProperty- the base propertyexpression- the expression- Returns:
- an instance of
ELPropertyfor the given base property and expression - Throws:
IllegalArgumentException- if the path is null or emptyPropertyResolutionException- if there's a problem with the expression
-
getWriteType
Returns the type of object that is suitable for setting as the value of thisPropertyby calls tosetValue.See the class level documentation for the definition of writeability.
- Specified by:
getWriteTypein classPropertyHelper<S,V> - Parameters:
source- the source object on which to operate- Returns:
- the type of object suitable for setting as the value
- Throws:
UnsupportedOperationException- if thePropertyis not writeable for the given sourcePropertyResolutionException- if an exception occurs while evaluating the expression- See Also:
-
getValue
Returns the value of thisPropertyfor the given source.See the class level documentation for the definition of readability.
- Specified by:
getValuein classPropertyHelper<S,V> - Parameters:
source- the source object on which to operate- Returns:
- the value of this
Propertyfor the given source - Throws:
UnsupportedOperationException- if thePropertyis not readable for the given sourcePropertyResolutionException- if an exception occurs while evaluating the expression- See Also:
-
setValue
Sets the value of thisPropertyfor the given source.See the class level documentation for the definition of writeability.
- Specified by:
setValuein classPropertyHelper<S,V> - Parameters:
source- the source object on which to operatevalue- the new value for theProperty- Throws:
UnsupportedOperationException- if thePropertyis not writeable for the given sourcePropertyResolutionException- if an exception occurs while evaluating the expression- See Also:
-
isReadable
Returns whether or not thePropertyis readable for the given source.See the class level documentation for the definition of readability.
- Specified by:
isReadablein classPropertyHelper<S,V> - Parameters:
source- the source object on which to operate- Returns:
- whether or not the
Propertyis readable for the given source. - See Also:
-
isWriteable
Returns whether or not thePropertyis writeable for the given source.See the class level documentation for the definition of writeability.
- Specified by:
isWriteablein classPropertyHelper<S,V> - Parameters:
source- the source object on which to operate- Returns:
- whether or not the
Propertyis writeable for the given source. - See Also:
-
listeningStarted
Description copied from class:PropertyHelperCalled when thisPropertyHelperchanges from having no listeners installed for the given source object to having listeners installed for the given source object. This is the ideal time for subclasses to install any listeners needed to track change on the source object.- Overrides:
listeningStartedin classPropertyHelper<S,V> - See Also:
-
listeningStopped
Description copied from class:PropertyHelperCalled when thisPropertyHelperchanges from having listeners installed for the given source object to having no listeners installed for the given source object. This is the ideal time for subclasses to remove any listeners that they've installed to track changes on the source object.- Overrides:
listeningStoppedin classPropertyHelper<S,V> - See Also:
-
toString
Returns a string representation of theELProperty. This method is intended to be used for debugging purposes only, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not benull.
-