atg.beans
Class DynamicBeans

java.lang.Object
  extended by atg.beans.DynamicBeans

public class DynamicBeans
extends java.lang.Object

Generic support for dynamic beans: objects exhibiting a set of dynamic properties at runtime.

A dynamic bean may be of any class, and need not implement any special interfaces. To access a dynamic bean's properties, an implementation of DynamicPropertyMapper must be registered for one of the bean's classes or interfaces by calling DynamicBeans.registerPropertyMapper(). Once this has been done, the methods DynamicBeans.getPropertyValue() and DynamicBeans.setPropertyValue() can be used to access dynamic properties of objects belonging to the registered class or interface. If no DynamicPropertyMapper is registered, these methods simply access the object's regular JavaBean properties.

This indirect approach permits existing classes like java.util.Hashtable or interfaces like java.sql.ResultSet to be treated as dynamic beans.

There is also a way to describe sets of similar dynamic beans and their dynamic properties. A DynamicBeanInfo can be obtained from either:

  1. a "dynamic bean type" object that describes some set of dynamic beans. Such an object plays the role of a type, although it need not be an instance of any particular class or interface.
  2. an instance of a dynamic bean

In the former case, the dynamic bean type object must register an an implementation of DynamicBeanTyper for one of the object's classes or interfaces. Once this has been done, the method DynamicBeans.getBeanInfoFromType() can be called to render the type object as a DynamicBeanInfo.

To continue the above example, one might want an object representing a particular SQL query to serve as a dynamic bean type, describing the dynamic properties of a ResultSet generated by the query, perhaps so this information could be used in designing the output of a report generator. A DynamicBeanTyper would be registered for this query object, allowing the dynamic properties of the query's ResultSet to be discovered from the query itself, before the ResultSet had actually been created.


Field Summary
static java.lang.String CLASS_VERSION
           
 
Constructor Summary
DynamicBeans()
           
 
Method Summary
static java.lang.annotation.Annotation getAnnotation(java.lang.Object pBean, java.lang.String pPropertyName, java.lang.Class pAnnotationClass)
          Gets a specific type of Annotation for a property on a bean.
static java.util.Set<java.lang.annotation.Annotation> getAnnotations(java.lang.Object pBean, java.lang.String pPropertyName)
          Gets the Annotations for a given property on a bean.
static DynamicBeanInfo getBeanInfo(java.lang.Object pBean)
          Returns a DynamicBeanInfo describing a particular dynamic bean instance.
static DynamicBeanInfo getBeanInfoFromType(java.lang.Object pDescription)
          Given a dynamic bean type for some set of dynamic beans, yield a DynamicBeanInfo describing that type.
static DynamicBeanTyper getBeanTyper(java.lang.Class pClass)
          Gets a dynamic beanTyper for the specified class.
static java.util.Set<java.lang.String> getPropertiesWithAnnotation(java.lang.Object pBean, java.lang.Class pAnnotationClass)
          Gets a set of property names that have a particular annotation.
static DynamicPropertyDescriptor getPropertyDescriptor(java.lang.Object pBean, java.lang.String pPropertyName)
          Returns the named property descriptor for the supplied object
static DynamicPropertyMapper getPropertyMapper(java.lang.Class pClass)
          Gets a property mapper for the specified class.
static java.lang.Object getPropertyValue(java.lang.Object pBean, java.lang.String pPropertyName)
          Returns the dynamic property value for the specified object using the a DynamicPropertyMapper objects.
static java.lang.String getPropertyValueAsString(java.lang.Object pBean, java.lang.String pPropertyName)
          Returns the dynamic property value for the specified object using the a DynamicPropertyMapper objects.
static java.lang.String getPropertyWithAnnotation(java.lang.Object pBean, java.lang.Class pAnnotationClass)
          Gets the property on an object that has a particular annotation.
static DynamicPropertyDescriptor getSubPropertyDescriptor(java.lang.Object pBean, java.lang.String pPropertyName)
          Like getPropertyDescriptor, but takes a hierarchy property name of the form code>propertyName1.subPropertyName2.subSubPropertyName3... If any of the intermediate property values is null, a NullPointerException is thrown.
static java.lang.Object getSubPropertyValue(java.lang.Object pBean, java.lang.String pPropertyName)
          Like getPropertyValue, but takes a hierarchy property name of the form propertyName1.subPropertyName2.subSubPropertyName3... If any of the intermediate property values is null, null is returned.
static java.lang.String getSubPropertyValueAsString(java.lang.Object pBean, java.lang.String pPropertyName)
          Like getPropertyValue, but takes a hierarchy property name of the form propertyName1.subPropertyName2.subSubPropertyName3... If any of the intermediate property values is null, null is returned.
static boolean isDynamicBean(java.lang.Class pClass)
          Returns true if the class specified has a DynamicPropertyMapper registered for it.
static boolean isInstanceOfBeanType(java.lang.Object pBean, java.lang.Object pDescription)
          Determines if a dynamic bean is an instance of a dynamic bean type.
static DynamicBeanInfo mergeBeanInfo(DynamicBeanInfo[] pBeanInfos)
          Given a set of DynamicBeanInfos returns a new DynamicBeanInfo which describes the combined information of all the bean infos
static void registerBeanTyper(java.lang.Class pClass, DynamicBeanTyper pBeanTyper)
          Adds a new property beanTyper.
static void registerPropertyMapper(java.lang.Class pClass, DynamicPropertyMapper pMapper)
          Adds a new property mapping definition.
static void setPropertyValue(java.lang.Object pBean, java.lang.String pPropertyName, java.lang.Object pValue)
          Sets the dynamic property value for the specified object using the list of registered DynamicPropertyMapper objects.
static void setPropertyValueFromString(java.lang.Object pBean, java.lang.String pPropertyName, java.lang.String pStringValue)
          Sets the dynamic property value for the specified object.
static void setSubPropertyValue(java.lang.Object pBean, java.lang.String pPropertyName, java.lang.Object pValue)
          Like setPropertyValue, but takes a hierarchy property name of the form propertyName1.subPropertyName2.subSubPropertyName3... If any of the intermediate property values is null, a NullPointerException is thrown.
static void setSubPropertyValueFromString(java.lang.Object pBean, java.lang.String pPropertyName, java.lang.String pStringValue)
          Like setPropertyValueFromString, but takes a hierarchy property name of the form propertyName1.subPropertyName2.subSubPropertyName3... If any of the intermediate property values is null, a NullPointerException is thrown.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS_VERSION

public static final java.lang.String CLASS_VERSION
See Also:
Constant Field Values
Constructor Detail

DynamicBeans

public DynamicBeans()
Method Detail

registerPropertyMapper

public static void registerPropertyMapper(java.lang.Class pClass,
                                          DynamicPropertyMapper pMapper)
Adds a new property mapping definition. Any instance of the specified class will use this DynamicPropertyMapper to get and set any properties that are not traditional bean properties on this object.


isDynamicBean

public static boolean isDynamicBean(java.lang.Class pClass)
Returns true if the class specified has a DynamicPropertyMapper registered for it.


getPropertyMapper

public static DynamicPropertyMapper getPropertyMapper(java.lang.Class pClass)
Gets a property mapper for the specified class. If no DynamicPropertyMapper has been registered for this class or a base class or interface of this class, then one is manufactured that will reference the class's compile-time properties.


registerBeanTyper

public static void registerBeanTyper(java.lang.Class pClass,
                                     DynamicBeanTyper pBeanTyper)
Adds a new property beanTyper. Any instance of the specified class will use this DynamicBeanTyper to yield a DynamicBeanInfo describing some set of dynamic properties.


getBeanTyper

public static DynamicBeanTyper getBeanTyper(java.lang.Class pClass)
Gets a dynamic beanTyper for the specified class. If no DynamicBeanTyper has been registered for this class or a base class of this class, null is returned.


getPropertyValue

public static java.lang.Object getPropertyValue(java.lang.Object pBean,
                                                java.lang.String pPropertyName)
                                         throws PropertyNotFoundException
Returns the dynamic property value for the specified object using the a DynamicPropertyMapper objects. If a DynamicPropertyMapper is registered for this particular class, this mapper is used to implement the "get" functionality for this object; otherwise the object's compile-time bean properties (if any) will be referenced.

Throws:
PropertyNotFoundException - if there is no property of this name on this object. The DynamicPropertyMapper determines whether or not to return null (meaning no current value for this property) or whether it should throw an exception (meaning this object does not support this property.

getPropertyValueAsString

public static java.lang.String getPropertyValueAsString(java.lang.Object pBean,
                                                        java.lang.String pPropertyName)
                                                 throws PropertyNotFoundException
Returns the dynamic property value for the specified object using the a DynamicPropertyMapper objects. If a DynamicPropertyMapper is registered for this particular class, this mapper is used to implement the "get" functionality for this object; otherwise the object's compile-time bean properties (if any) will be referenced. The property is then converted to a string

Parameters:
pBean - The object
pPropertyName - The property name
Returns:
The string representation of the property value
Throws:
PropertyNotFoundException

getSubPropertyValue

public static java.lang.Object getSubPropertyValue(java.lang.Object pBean,
                                                   java.lang.String pPropertyName)
                                            throws PropertyNotFoundException
Like getPropertyValue, but takes a hierarchy property name of the form propertyName1.subPropertyName2.subSubPropertyName3... If any of the intermediate property values is null, null is returned.

Throws:
PropertyNotFoundException

getSubPropertyValueAsString

public static java.lang.String getSubPropertyValueAsString(java.lang.Object pBean,
                                                           java.lang.String pPropertyName)
                                                    throws PropertyNotFoundException
Like getPropertyValue, but takes a hierarchy property name of the form propertyName1.subPropertyName2.subSubPropertyName3... If any of the intermediate property values is null, null is returned. The last property is then converted to a string.

Throws:
PropertyNotFoundException

setPropertyValue

public static void setPropertyValue(java.lang.Object pBean,
                                    java.lang.String pPropertyName,
                                    java.lang.Object pValue)
                             throws PropertyNotFoundException
Sets the dynamic property value for the specified object using the list of registered DynamicPropertyMapper objects. If a DynamicPropertyMapper is registered for this particular class, this mapper is used to implement the "set" functionality for this object. Otherwise the object's compile-time bean properties (if any) will be referenced.

Throws:
PropertyNotFoundException - if there is no dynamic property for this property name on this object.

setSubPropertyValue

public static void setSubPropertyValue(java.lang.Object pBean,
                                       java.lang.String pPropertyName,
                                       java.lang.Object pValue)
                                throws PropertyNotFoundException,
                                       java.lang.NullPointerException
Like setPropertyValue, but takes a hierarchy property name of the form propertyName1.subPropertyName2.subSubPropertyName3... If any of the intermediate property values is null, a NullPointerException is thrown.

Throws:
PropertyNotFoundException - if there is no dynamic property for this property name on this object.
java.lang.NullPointerException - if any of the intermediate property values is null.

setPropertyValueFromString

public static void setPropertyValueFromString(java.lang.Object pBean,
                                              java.lang.String pPropertyName,
                                              java.lang.String pStringValue)
                                       throws PropertyNotFoundException,
                                              java.lang.IllegalArgumentException
Sets the dynamic property value for the specified object. The supplied value for the property is a String and will be auto-converted to the correct data type based on dynamic introspection of the supplied Bean object. If a DynamicPropertyMapper is registered for this particular class, this mapper is used to implement the "set" functionality for this object. Otherwise the object's compile-time bean properties (if any) will be referenced.

Throws:
PropertyNotFoundException - if there is no dynamic property for this property name on this object.
java.lang.IllegalArgumentException - if the String representation of the value cannot be converted into the correct format.

setSubPropertyValueFromString

public static void setSubPropertyValueFromString(java.lang.Object pBean,
                                                 java.lang.String pPropertyName,
                                                 java.lang.String pStringValue)
                                          throws PropertyNotFoundException,
                                                 java.lang.NullPointerException,
                                                 java.lang.IllegalArgumentException
Like setPropertyValueFromString, but takes a hierarchy property name of the form propertyName1.subPropertyName2.subSubPropertyName3... If any of the intermediate property values is null, a NullPointerException is thrown.

Throws:
PropertyNotFoundException - if there is no dynamic property for this property name on this object.
java.lang.NullPointerException - if any of the intermediate property values is null.
java.lang.IllegalArgumentException - if the String representation of the value cannot be converted into the correct format.

getPropertyDescriptor

public static DynamicPropertyDescriptor getPropertyDescriptor(java.lang.Object pBean,
                                                              java.lang.String pPropertyName)
                                                       throws java.beans.IntrospectionException
Returns the named property descriptor for the supplied object

Throws:
java.beans.IntrospectionException - if no DynamicBeanInfo can be found for the object so the DynamicPropertyDescriptor can be returned

getSubPropertyDescriptor

public static DynamicPropertyDescriptor getSubPropertyDescriptor(java.lang.Object pBean,
                                                                 java.lang.String pPropertyName)
                                                          throws PropertyNotFoundException,
                                                                 java.lang.NullPointerException,
                                                                 java.beans.IntrospectionException
Like getPropertyDescriptor, but takes a hierarchy property name of the form code>propertyName1.subPropertyName2.subSubPropertyName3... If any of the intermediate property values is null, a NullPointerException is thrown.

Throws:
PropertyNotFoundException - if there is no dynamic property for this property name on this object
java.lang.NullPointerException - if any of the intermediate property values is null
java.beans.IntrospectionException - if no DynamicBeanInfo can be found for the object so the DynamicPropertyDescriptor can be returned

getBeanInfo

public static DynamicBeanInfo getBeanInfo(java.lang.Object pBean)
                                   throws java.beans.IntrospectionException
Returns a DynamicBeanInfo describing a particular dynamic bean instance.

Throws:
java.beans.IntrospectionException

getBeanInfoFromType

public static DynamicBeanInfo getBeanInfoFromType(java.lang.Object pDescription)
                                           throws java.beans.IntrospectionException
Given a dynamic bean type for some set of dynamic beans, yield a DynamicBeanInfo describing that type. The type object may be of an arbitrary class, and a DynamicBeanTyper must have been registered for that class.

Throws:
java.beans.IntrospectionException

isInstanceOfBeanType

public static boolean isInstanceOfBeanType(java.lang.Object pBean,
                                           java.lang.Object pDescription)
Determines if a dynamic bean is an instance of a dynamic bean type.


mergeBeanInfo

public static DynamicBeanInfo mergeBeanInfo(DynamicBeanInfo[] pBeanInfos)
                                     throws java.beans.IntrospectionException
Given a set of DynamicBeanInfos returns a new DynamicBeanInfo which describes the combined information of all the bean infos

Throws:
java.beans.IntrospectionException - if there was an error while constructing the new merged information

getAnnotations

public static java.util.Set<java.lang.annotation.Annotation> getAnnotations(java.lang.Object pBean,
                                                                            java.lang.String pPropertyName)
                                                                     throws java.beans.IntrospectionException
Gets the Annotations for a given property on a bean. The annotations for the getter and the setter are returned.

Parameters:
pBean - The object
pPropertyName - The propertyName
Returns:
The set of Annotations
Throws:
java.beans.IntrospectionException - Thrown if there is a problem getting the annotations from the AnnotationDynamicPropertyMapper

getPropertyWithAnnotation

public static java.lang.String getPropertyWithAnnotation(java.lang.Object pBean,
                                                         java.lang.Class pAnnotationClass)
                                                  throws java.beans.IntrospectionException
Gets the property on an object that has a particular annotation. Both the getter and setter are examined. The first property found with this annotation is returned.

Parameters:
pBean - The object
pAnnotationClass - The annotation class to look for
Returns:
The name of the property, null if one isn't found.
Throws:
java.beans.IntrospectionException - Thrown if there is a problem getting the property name from the AnnotationDynamicPropertyMapper

getPropertiesWithAnnotation

public static java.util.Set<java.lang.String> getPropertiesWithAnnotation(java.lang.Object pBean,
                                                                          java.lang.Class pAnnotationClass)
                                                                   throws java.beans.IntrospectionException
Gets a set of property names that have a particular annotation.

Parameters:
pBean - The object
pAnnotationClass - The type of annotation
Returns:
The set of property names, if any exist, with the annotation, null otherwise.
Throws:
java.beans.IntrospectionException - Thrown if there is a problem retrieving the annotation from the class

getAnnotation

public static java.lang.annotation.Annotation getAnnotation(java.lang.Object pBean,
                                                            java.lang.String pPropertyName,
                                                            java.lang.Class pAnnotationClass)
                                                     throws java.beans.IntrospectionException
Gets a specific type of Annotation for a property on a bean. The getter and setter are examined. Null is returned if the annotation doesn't exist.

Parameters:
pBean - The object
pPropertyName - The property name
pAnnotationClass - The class of annotation to get
Returns:
The annotation for this property
Throws:
java.beans.IntrospectionException - Thrown if there is a problem getting the annotation from the AnnotationDynamicPropertyMapper