|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.primix.tapestry.util.prop.PropertyHelper
Streamlines access to all the properties of a given JavaBean. Static methods acts as a factory for PropertyHelper instances, which are specific to a particular Bean class.
A PropertyHelper
for a bean class simplifies getting and setting properties
on the bean, handling (and caching) the lookup of methods as well as the dynamic
invocation of those methods. It uses an instance of IPropertyAccessor
for each property.
PropertyHelper allows properties to be specified in terms of a path. A path is a
series of property names seperate by periods. So a property path of
'visit.user.address.street' is effectively the same as
the code getVisit().getUser().getAddress().getStreet()
(and just as likely to throw a NullPointerException
).
Typical usage:
ProperyHelper helper = PropertyHelper.forInstance(instance); helper.set(instance, "propertyName", newValue);
Only single-valued properties (not indexed properties) are supported, and a minimum of type checking is performed.
A mechanism exists to register custom PropertyHelper
subclasses
for specific classes. The two default registrations are
PublicBeanPropertyHelper
for the IPublicBean
interface, and
MapHelper
for the Map
interface.
Field Summary | |
protected java.util.Map |
accessors
Map of PropertyAccessors for the helper's bean class. |
protected java.lang.Class |
beanClass
The Java Beans class for which this helper is configured. |
static char |
PATH_SEPERATOR
The separator character used to divide up different properties in a nested property name. |
Constructor Summary | |
protected |
PropertyHelper(java.lang.Class beanClass)
|
Method Summary | |
protected void |
buildPropertyAccessors()
Uses JavaBeans introspection to find all the properties of the bean class. |
static PropertyHelper |
forClass(java.lang.Class beanClass)
Factory method which returns a PropertyHelper for the given
JavaBean class. |
static PropertyHelper |
forInstance(java.lang.Object instance)
A convienience method; simply invokes forClass(Class) . |
java.lang.Object |
get(java.lang.Object object,
java.lang.String propertyName)
Returns the value of the named property for the given object. |
IPropertyAccessor |
getAccessor(java.lang.Object instance,
java.lang.String propertyName)
Finds an accessor for the given property name. |
IPropertyAccessor |
getAccessorPath(java.lang.Object instance,
java.lang.String[] propertyPath)
Finds an accessor using a split property path. |
java.util.Collection |
getAccessors(java.lang.Object instance)
Returns a collection of IPropertyAccessor instances. |
java.lang.Object |
getPath(java.lang.Object object,
java.lang.String propertyPath)
Gets the value of a property from the given object. |
java.lang.Object |
getPath(java.lang.Object object,
java.lang.String[] propertyPath)
Gets the object, using a pre-split property path. |
protected java.util.Collection |
getSyntheticPropertyNames(java.lang.Object instance)
Returns a Collection of the names of any synthetic properties. |
boolean |
isReadable(java.lang.Object instance,
java.lang.String propertyName)
|
boolean |
isWritable(java.lang.Object instance,
java.lang.String propertyName)
|
static void |
register(java.lang.Class beanClass,
java.lang.Class helperClass)
Registers a particular PropertyHelper subclass as the correct
class to use with a specific class of bean (or any class derived from
the bean class). |
void |
set(java.lang.Object object,
java.lang.String propertyName,
java.lang.Object value)
Sets the value of a property of the named object. |
void |
setPath(java.lang.Object object,
java.lang.String[] propertyPath,
java.lang.Object value)
Changes the value of one of a bean's properties. |
void |
setPath(java.lang.Object object,
java.lang.String propertyPath,
java.lang.Object value)
Changes the value of a some bean's property, by following a property path. |
static java.lang.String[] |
splitPropertyPath(java.lang.String propertyPath)
Splits a property path into an array of individual properties. |
java.lang.String |
toString()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected java.util.Map accessors
protected java.lang.Class beanClass
public static final char PATH_SEPERATOR
Constructor Detail |
protected PropertyHelper(java.lang.Class beanClass)
Method Detail |
protected void buildPropertyAccessors()
accessors
variable (it will
have been null), and adds all the well-defined JavaBeans properties.
Subclasses may invoke this method before adding thier own accessors.
This method is invoked from within a synchronized block. Subclasses do not have to worry about synchronization.
public static PropertyHelper forInstance(java.lang.Object instance)
forClass(Class)
.public static PropertyHelper forClass(java.lang.Class beanClass)
PropertyHelper
for the given
JavaBean class.
Finding the right helper class involves a sequential lookup, first for an
exact match, then for an exact match on the superclass, the a search
by interface. If no specific
match is found, then PropertyHelper
itself is used, which is
the most typical case.
register(Class, Class)
public java.lang.Object get(java.lang.Object object, java.lang.String propertyName)
propertyName must be a simple property name, not a path,
use getPath(Object,String)
to use a property path.
public java.lang.Object getPath(java.lang.Object object, java.lang.String propertyPath)
getPath(Object,String[])
.object
- The object to retrieve a property from.propertyPath
- a list of properties to get, seperated
by periodspublic java.lang.Object getPath(java.lang.Object object, java.lang.String[] propertyPath)
public IPropertyAccessor getAccessor(java.lang.Object instance, java.lang.String propertyName)
propertyName
- the simple property name of the property to
get.public IPropertyAccessor getAccessorPath(java.lang.Object instance, java.lang.String[] propertyPath)
public boolean isReadable(java.lang.Object instance, java.lang.String propertyName)
public boolean isWritable(java.lang.Object instance, java.lang.String propertyName)
public static void register(java.lang.Class beanClass, java.lang.Class helperClass)
PropertyHelper
subclass as the correct
class to use with a specific class of bean (or any class derived from
the bean class). An interface may be specified as well, in which case
beans that match the interface (i.e., implement the interface directly
or indirectly) will use the registered helper class.public void set(java.lang.Object object, java.lang.String propertyName, java.lang.Object value)
setPath(Object,String,Object)
instead.object
- the object to changepropertyName
- the name of the property to changevalue
- the value to assign to the propertypublic void setPath(java.lang.Object object, java.lang.String propertyPath, java.lang.Object value)
setPath(Object,String[],Object)
.public void setPath(java.lang.Object object, java.lang.String[] propertyPath, java.lang.Object value)
getPath(Object,String[])
, since the goal for those
properties is to traverse to the correct object.
On the final property in the path, we update instead of reading,
just like set(Object,String,Object)
.
public java.lang.String toString()
toString
in class java.lang.Object
public static java.lang.String[] splitPropertyPath(java.lang.String propertyPath)
public java.util.Collection getAccessors(java.lang.Object instance)
IPropertyAccessor
instances.
These are generated from the "natural" JavaBeans properties. Many subclasses create "synthesized" properties, which should be added to the result and returned.
The collection returned may be modified freely. Subclasses should invoke this implementation, then add additional values as necessary.
TODO: Deal with conflicts between natural and synthesized properties.
protected java.util.Collection getSyntheticPropertyNames(java.lang.Object instance)
Subclasses should override this to interrogate the instance
in an appropriate way, so as to extract this list of properties.
For example, MapHelper
casts the instance to Map
,
and returns the map's keySet.
Subclasses may override this implementation without invoking it. This implementation returns null.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |