dsto.dfc.util
Class Beans

java.lang.Object
  extended by dsto.dfc.util.Beans

public final class Beans
extends Object

Utilities for messing with JavaBeans.

Version:
$Revision$

Method Summary
static boolean addListener(Class listenerInterface, Object source, Object listener)
          Add an event listener to a JavaBean using reflection.
static boolean addListener(Object source, String eventType, Class listenerClass, Object listener)
          Dynamically add a listener to an event type using reflection.
static Class classForName(String name)
          Like Class.forName () but also handles the primitive classes "int", "float", etc.
static String convertPropertyToString(Object propertyValue)
          Convert an object to its stringified value using (if possible) its PropertyEditor.
static Object convertStringToProperty(Class propertyType, String propertyText)
          Convert a stringfied object to an instance by using (if possible) its PropertyEditor.
protected static Method findEventListenerMethod(Class sourceClass, String prefix, String eventType, Class listenerClass)
           
static PropertyDescriptor findProperty(PropertyDescriptor[] properties, String name)
          Find a property descriptor with a given name.
static PropertyEditor findPropertyEditor(Class type)
           
static Class getPropertyClass(Class beanClass, String property)
          Get the type of a property.
static Method getPropertyReadMethod(Class beanClass, String propertyName)
          Get the method used to read a JavaBean property.
static Object getPropertyValue(Object target, String property)
          Get the value of a given property.
static Method getPropertyWriteMethod(Class beanClass, String propertyName)
          Get the method used to write a given JavaBean property.
static Method getPropertyWriteMethod(Class targetClass, String propertyName, Class valueClass)
          Attempt to resolve a JavaBean's method for writing a property value.
static Object getSingletonInstance(Class theClass)
          Get the global instance of a singleton class.
static boolean hasWritableProperty(Class targetClass, String propertyName)
          Similar to hasWritableProperty(Class, String, Class) except the type of the property is not checked.
static boolean hasWritableProperty(Class targetClass, String propertyName, Class valueClass)
          True if a JavaBean has a writable property with a given name.
static boolean isBean(Class aClass)
          Test if a given class is a JavaBean.
static boolean isSingletonClass(Class theClass)
          Test if a class is a singleton class type (see Singleton for details on what constitues a singleton class).
static Class objectToPrimitive(Class theClass)
           
static Class primitiveToObject(Class theClass)
          Convert a class representing a primitive type (eg 'int') to its equivalent Object-based type (eg 'java.lang.Integer').
static boolean removeListener(Class listenerInterface, Object source, Object listener)
          Remove an event listener to a JavaBean using reflection.
static boolean removeListener(Object source, String eventType, Class listenerClass, Object listener)
          Dynamically remove a listener to an event type using reflection.
static void setPropertyValue(Object target, String propertyName, Object propertyValue)
          Set the value of a writeable JavaBean property.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isBean

public static boolean isBean(Class aClass)
Test if a given class is a JavaBean. The class is considered a bean if it has at least one readable bean property, ie one public, non-static, non-void getXXX () method. This method does not use the Beans introspector and hence will not recognise beans that use non-standard property accessors published via BeanInfo.

Parameters:
aClass - The class to test

getPropertyWriteMethod

public static Method getPropertyWriteMethod(Class beanClass,
                                            String propertyName)
Get the method used to write a given JavaBean property. This includes setter methods that are private/protected.

Parameters:
beanClass - The class of JavaBean to analyse.
propertyName - The name of the property.
Returns:
the property setter method, or null if none found.
See Also:
getPropertyWriteMethod(Class,String,Class), getPropertyReadMethod(Class, String)

getPropertyReadMethod

public static Method getPropertyReadMethod(Class beanClass,
                                           String propertyName)
                                    throws NoSuchMethodException
Get the method used to read a JavaBean property. This includes getter methods that are private/protected.

Parameters:
beanClass - The class of the bean.
propertyName - The property name.
Returns:
The method used to access the property.
Throws:
NoSuchMethodException - if no method for that property could be resolved.
See Also:
getPropertyWriteMethod(Class, String)

getPropertyWriteMethod

public static Method getPropertyWriteMethod(Class targetClass,
                                            String propertyName,
                                            Class valueClass)
Attempt to resolve a JavaBean's method for writing a property value.

Parameters:
targetClass - The class of JavaBean to analyse.
propertyName - The name of the property.
valueClass - The type of the property.
Returns:
The 'set' method that sets the property value, or null if no such method could be found.
See Also:
getPropertyWriteMethod(Class,String)

hasWritableProperty

public static boolean hasWritableProperty(Class targetClass,
                                          String propertyName,
                                          Class valueClass)
True if a JavaBean has a writable property with a given name.

Parameters:
targetClass - The JavaBean class.
propertyName - The property name.
valueClass - The type of property value.
Returns:
True if the JavaBean has such a writeable property.
See Also:
getPropertyWriteMethod(java.lang.Class, java.lang.String), setPropertyValue(java.lang.Object, java.lang.String, java.lang.Object)

hasWritableProperty

public static boolean hasWritableProperty(Class targetClass,
                                          String propertyName)
Similar to hasWritableProperty(Class, String, Class) except the type of the property is not checked. This is a little faster than the full version.

Parameters:
targetClass - The class to check.
propertyName - The name of the property.
Returns:
True if there is a property write method.

setPropertyValue

public static void setPropertyValue(Object target,
                                    String propertyName,
                                    Object propertyValue)
                             throws NoSuchMethodException,
                                    InvocationTargetException
Set the value of a writeable JavaBean property. Note: in the case of setting property value to null, resolution of the correct write method may behave unexpectedly if there are multiple overloaded 'set' methods (this would be a bad idea anyway, right?).

Parameters:
target - The JavaBean.
propertyName - The name of the property.
propertyValue - The new value of the property.
Throws:
NoSuchMethodException - if the property does not exist.
InvocationTargetException - if the property setter method threw an exception.

getPropertyValue

public static Object getPropertyValue(Object target,
                                      String property)
                               throws NoSuchMethodException
Get the value of a given property.

Parameters:
target - The object to read the value from.
property - The property name.
Returns:
The value of the property.
Throws:
NoSuchMethodException - if the property does not exist or an error occurred during the property method invocation.

getPropertyClass

public static Class getPropertyClass(Class beanClass,
                                     String property)
                              throws NoSuchMethodException
Get the type of a property.

Parameters:
beanClass - The class of the JavaBean.
property - The property name.
Returns:
The class of the given property.
Throws:
NoSuchMethodException - if the property does not exist.

primitiveToObject

public static Class primitiveToObject(Class theClass)
Convert a class representing a primitive type (eg 'int') to its equivalent Object-based type (eg 'java.lang.Integer').

Parameters:
theClass - The primitive class.
Returns:
The equivalent Object-based class, or simply theClass if theClass is not a primitive class.

objectToPrimitive

public static Class objectToPrimitive(Class theClass)

classForName

public static Class classForName(String name)
                          throws ClassNotFoundException
Like Class.forName () but also handles the primitive classes "int", "float", etc.

Parameters:
name - The class name.
Returns:
The Class with the associated name.
Throws:
ClassNotFoundException - if class is not defined.

isSingletonClass

public static boolean isSingletonClass(Class theClass)
Test if a class is a singleton class type (see Singleton for details on what constitues a singleton class).


getSingletonInstance

public static Object getSingletonInstance(Class theClass)
                                   throws IllegalArgumentException
Get the global instance of a singleton class. See Singleton for details on what constitues a singleton class.

Parameters:
theClass - The class to examine. For convenience, theClass does not have to implement the Singleton interface, it just has to have either a static getSingletonInstance() method or a static INSTANCE variable.
Throws:
IllegalArgumentException - if theClass is not a singleton class.

addListener

public static boolean addListener(Class listenerInterface,
                                  Object source,
                                  Object listener)
Add an event listener to a JavaBean using reflection.

Parameters:
listenerInterface - The event listener interface (eg PropertyChangeListener).
source - The event source bean.
listener - The event listener (must implement listenerInterface).
Returns:
True if the add succeeded.

removeListener

public static boolean removeListener(Class listenerInterface,
                                     Object source,
                                     Object listener)
Remove an event listener to a JavaBean using reflection.

Parameters:
listenerInterface - The event listener interface (eg PropertyChangeListener).
source - The event source bean.
listener - The event listener (must implement listenerInterface).
Returns:
True if the remove succeeded.

findPropertyEditor

public static PropertyEditor findPropertyEditor(Class type)

findProperty

public static PropertyDescriptor findProperty(PropertyDescriptor[] properties,
                                              String name)
Find a property descriptor with a given name.

Parameters:
properties - The properties to search.
name - The name to find.
Returns:
The descriptor or null if not found.

convertPropertyToString

public static String convertPropertyToString(Object propertyValue)
Convert an object to its stringified value using (if possible) its PropertyEditor. If no editor is available, toString () is used.

Parameters:
propertyValue - The value to convert.
Returns:
The stringified version of propertyValue.
See Also:
convertStringToProperty(java.lang.Class, java.lang.String)

convertStringToProperty

public static Object convertStringToProperty(Class propertyType,
                                             String propertyText)
                                      throws IllegalArgumentException
Convert a stringfied object to an instance by using (if possible) its PropertyEditor. If no property editor is available, but a constructor taking a single string argument is, then this will be used instead.

Parameters:
propertyType - The type of the new property value.
propertyText - The stringified property value
Returns:
A new instance of propertyType made by parsing propertyText.
Throws:
IllegalArgumentException - If propertyText could not be converted to an object (eg format error or no converter was found).
See Also:
convertPropertyToString(java.lang.Object)

addListener

public static boolean addListener(Object source,
                                  String eventType,
                                  Class listenerClass,
                                  Object listener)
Dynamically add a listener to an event type using reflection.

Parameters:
source - The object to add the listener to.
eventType - The event type eg "change" for ChangeListener, "propertyChange" for PropertyChangeListener.
listenerClass - The listener interface class eg PropertyChangeListener.class for "propertyChange" events.
listener - The listener class. Must implement the interface defined by listenerClass.
Returns:
True if the add succeeded.

removeListener

public static boolean removeListener(Object source,
                                     String eventType,
                                     Class listenerClass,
                                     Object listener)
Dynamically remove a listener to an event type using reflection.

Parameters:
source - The object to remove the listener from.
eventType - The event type eg "change" for ChangeListener, "propertyChange" for PropertyChangeListener.
listenerClass - The listener interface class eg PropertyChangeListener.class for "propertyChange" events.
listener - The listener class. Must implement the interface defined by listenerClass.
Returns:
True if the remove succeeded.

findEventListenerMethod

protected static Method findEventListenerMethod(Class sourceClass,
                                                String prefix,
                                                String eventType,
                                                Class listenerClass)
                                         throws NoSuchMethodException
Throws:
NoSuchMethodException


Copyright © 2008 Commonwealth of Australia