org.jbeans
Class JavaBeanTools

java.lang.Object
  |
  +--org.jbeans.JavaBeanTools

public class JavaBeanTools
extends Object

This class is a library of tools that can be used when working with JavaBeans.

Author:
Brian Pontarelli

Inner Class Summary
static class JavaBeanTools.IndexHelper
          This class is a small helper class that is used in the beans pacakge for assisting with indexing operations.
static class JavaBeanTools.NameHelper
          This class is a small helper class that is used in the beans pacakge for assisting with property names
 
Field Summary
static String GET_STRING
          The string that starts standard Java bean retrieval methods get
static String HANDLE_STRING
          The string that starts non-standard Java bean handle methods handle
static String IS_STRING
          The string that starts standard Java bean boolean retrieval methods is
static String SET_STRING
          The string that starts standard Java bean update methods set
 
Method Summary
static Object callGetter(BeanProperty prop, Object bean)
          Another convience method that calls a java bean getter and returns the value.
static Object callGetter(String property, Object bean)
          Another convience method for calling a getter on a java bean.
static Object callIndexedGetter(BeanProperty prop, Object bean, int index)
          Another convience method that calls an indexed java bean getter and returns the value.
static void callIndexedSetter(BeanProperty prop, Object bean, int index, Object param)
          Another convience method for calling a setter on a java bean.
static void callSetter(BeanProperty prop, Object bean, Object param)
          Another convience method for calling a setter on a java bean.
static void callSetter(String property, Object bean, Object param)
          Another convience method for calling a setter on a java bean.
static String capitalize(String lowerCase)
          Capitalizes the first letter of the given String.
static JavaBeanTools.IndexHelper findIndex(String propertyName)
          Given the property name string, the index and property name are extracted and returned in a new IndexHelper.
static JavaBeanTools.IndexHelper findIndex(String propertyName, JavaBeanTools.IndexHelper indexHelper)
          Given the property name string, the index and property name are extracted and returned in the given IndexHelper.
static Method findIndexedReadMethod(String property, Class beanClass)
          Finds a get method in the class given for the given property.
static Method findIndexedWriteMethod(String property, Class beanClass, Class type)
          Finds a set indexed method for the given property and parameter type
static Method findReadMethod(String property, Class beanClass)
          Finds either a get method or is method in the class given for the given property name.
static Method findWriteMethod(String property, Class beanClass, Class type)
          Finds a set method for the given property and parameter type
static String getPropertyName(Method method)
          Using the given Method, it returns the name of the java bean property.
Examples:
static boolean isValidGetter(Method method)
          Check if the method is a proper java bean getter-property method.
static boolean isValidGetter(Method method, boolean checkName)
          Does the same check as isValidGetter() is checkName is true, but does not check the name if checkName is false.
static boolean isValidIndexedGetter(Method method)
          Check if the method is a proper java bean indexed getter method.
static boolean isValidIndexedGetter(Method method, boolean checkName)
          Does the same check as isValidIndexedGetter() if checkName is true, but does not check the name if checkName is false.
static boolean isValidIndexedSetter(Method method)
          Check if the method is a proper java bean indexed setter method.
static boolean isValidIndexedSetter(Method method, boolean checkName)
          Does the same check as isValidIndexedSetter() if checkName is true, but does not check the name if checkName is false.
static boolean isValidSetter(Method method)
          Check if the method is a proper java bean setter-property method.
static boolean isValidSetter(Method method, boolean checkName)
          Does the same check as isValidSetter() is checkName is true, but does not check the name if checkName is false.
static String makeGetter(String propertyName)
          Using the propertyName, returns the Java Bean standard getter method name.
static String makeHandle(String propertyName)
          Using the propertyName, returns the non-standard Java Bean handle method name.
static String makeIs(String propertyName)
          Using the propertyName, returns the Java Bean standard boolean getter method name.
static String makeSetter(String propertyName)
          Using the propertyName, returns the Java Bean standard setter method name.
static JavaBeanTools.IndexHelper removeIndices(String propertyName)
          This method takes the given property string and removes all the indices from it and stores them in an List.
static JavaBeanTools.NameHelper splitName(String propertyName)
          Using the given nested property name, this returns the local property name and the remainder of the nested property name, if any.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GET_STRING

public static final String GET_STRING
The string that starts standard Java bean retrieval methods get

SET_STRING

public static final String SET_STRING
The string that starts standard Java bean update methods set

IS_STRING

public static final String IS_STRING
The string that starts standard Java bean boolean retrieval methods is

HANDLE_STRING

public static final String HANDLE_STRING
The string that starts non-standard Java bean handle methods handle
Method Detail

getPropertyName

public static String getPropertyName(Method method)
Using the given Method, it returns the name of the java bean property.
Examples:

getFoo -> foo
getX -> x
getURL -> URL

Parameters:
method - The method to translate
Returns:
The property name or null if this was not a valid property Method

capitalize

public static String capitalize(String lowerCase)
Capitalizes the first letter of the given String. If the String starts with white space, then it is returned as is.
Parameters:
lowerCase - The string to capitalize
Returns:
The capitalized string
Throws:
NullPointerException - If the parameter is null

makeGetter

public static String makeGetter(String propertyName)
Using the propertyName, returns the Java Bean standard getter method name. If the parameter String starts with white space or only contains white space or is empty, the it is simply concatenated to the GET constant of this class. It is the job of the calling code to make certain that the parameter is a properly formatted String if that check is desired.
Parameters:
propertyName - The property name to make into the name of the getter method
Returns:
The name of the getter method or null if propertyName is empty or null
Throws:
NullPointerException - If the parameter is null

makeSetter

public static String makeSetter(String propertyName)
Using the propertyName, returns the Java Bean standard setter method name. If the parameter String starts with white space or only contains white space or is empty, the it is simply concatenated to the SET constant of this class. It is the job of the calling code to make certain that the parameter is a properly formatted String if that check is desired.
Parameters:
propertyName - The property name to make into the name of the setter method
Returns:
The name of the setter method or null if propertyName is empty or null
Throws:
NullPointerException - If the parameter is null

makeIs

public static String makeIs(String propertyName)
Using the propertyName, returns the Java Bean standard boolean getter method name. If the parameter String starts with white space or only contains white space or is empty, the it is simply concatenated to the IS constant of this class. It is the job of the calling code to make certain that the parameter is a properly formatted String if that check is desired.
Parameters:
propertyName - The property name to make into the name of the is method
Returns:
The name of the is method or null if propertyName is empty or null
Throws:
NullPointerException - If the parameter is null

makeHandle

public static String makeHandle(String propertyName)
Using the propertyName, returns the non-standard Java Bean handle method name. If the parameter String starts with white space or only contains white space or is empty, the it is simply concatenated to the HANDLE constant of this class. It is the job of the calling code to make certain that the parameter is a properly formatted String if that check is desired.
Parameters:
propertyName - The property name to make into the name of the handle method
Returns:
The name of the handle method or null if propertyName is empty or null
Throws:
NullPointerException - If the parameter is null

isValidGetter

public static boolean isValidGetter(Method method)
Check if the method is a proper java bean getter-property method. This means that it starts with get, has the form getFoo or getFOO, has no parameters and returns a non-void value
Parameters:
method - The method to check
Returns:
True if valid, false otherwise

isValidIndexedGetter

public static boolean isValidIndexedGetter(Method method)
Check if the method is a proper java bean indexed getter method. This means that it starts with get, has the form getFoo or getFOO, has one parameter, an index, and returns a non-void value
Parameters:
method - The method to check
Returns:
True if valid, false otherwise

isValidGetter

public static boolean isValidGetter(Method method,
                                    boolean checkName)
Does the same check as isValidGetter() is checkName is true, but does not check the name if checkName is false. The only benefit of not checking the name is the performance increase. Checking the name is a simple procedure but requires a number of string comparisons and method calls.
Parameters:
method - The method to check
checkName - Whether or not to check the method's name
Returns:
True if the method is a valid getter and false otherwise

isValidIndexedGetter

public static boolean isValidIndexedGetter(Method method,
                                           boolean checkName)
Does the same check as isValidIndexedGetter() if checkName is true, but does not check the name if checkName is false. The only benefit of not checking the name is the performance increase. Checking the name is a simple procedure but requires a number of string comparisons and method calls.
Parameters:
method - The method to check
checkName - Whether or not to check the method name
Returns:
True if the method is a valid indexed getter and false otherwise

isValidSetter

public static boolean isValidSetter(Method method)
Check if the method is a proper java bean setter-property method. This means that it starts with set, has the form setFoo or setFOO, takes a single parameter and returns void
Parameters:
method - The method to check
Returns:
True if valid, false otherwise

isValidIndexedSetter

public static boolean isValidIndexedSetter(Method method)
Check if the method is a proper java bean indexed setter method. This means that it starts with set, has the form setFoo or setFOO, takes a two parameters, an index and a value and returns void
Parameters:
method - The method to check
Returns:
True if valid, false otherwise

isValidSetter

public static boolean isValidSetter(Method method,
                                    boolean checkName)
Does the same check as isValidSetter() is checkName is true, but does not check the name if checkName is false. The only benefit of not checking the name is the performance increase. Checking the name is a simple procedure but requires a number of string comparisons and method calls.
Parameters:
method - The method to check
checkName - Whether or not to check the method name
Returns:
True if the method is a valid setter and false otherwise

isValidIndexedSetter

public static boolean isValidIndexedSetter(Method method,
                                           boolean checkName)
Does the same check as isValidIndexedSetter() if checkName is true, but does not check the name if checkName is false. The only benefit of not checking the name is the performance increase. Checking the name is a simple procedure but requires a number of string comparisons and method calls.
Parameters:
method - The method to check
checkName - Whether or not to check the method name
Returns:
True if the method is a valid indexed setter and false otherwise

findReadMethod

public static Method findReadMethod(String property,
                                    Class beanClass)
                             throws BeanException
Finds either a get method or is method in the class given for the given property name. All white space on the property String parameter is trimmed automatically when locating the Method.
Parameters:
property - The name of the property to find the write method for
beanClass - The class object for the bean
Returns:
The read Method for the given property name and never null
Throws:
BeanException - If the write method can not be found
NullPointerException - If the property String is null

findIndexedReadMethod

public static Method findIndexedReadMethod(String property,
                                           Class beanClass)
                                    throws BeanException
Finds a get method in the class given for the given property.
Parameters:
property - The name of the property to find the indexed write method for
beanClass - The class object for the bean
Returns:
The indexed read Method for the given property name and never null
Throws:
BeanException - If the write method can not be found

findWriteMethod

public static Method findWriteMethod(String property,
                                     Class beanClass,
                                     Class type)
                              throws BeanException
Finds a set method for the given property and parameter type
Parameters:
property - The name of the property to find
beanClass - The class object for the bean
type - The parameter type to use when locating the write method
Returns:
The write Method for the given property name and type and never null
Throws:
BeanException - If the write method can not be found

findIndexedWriteMethod

public static Method findIndexedWriteMethod(String property,
                                            Class beanClass,
                                            Class type)
                                     throws BeanException
Finds a set indexed method for the given property and parameter type
Parameters:
property - The name of the property to find
beanClass - The class object for the bean
type - The parameter type to use when locating the write method
Returns:
The indexed write Method for the given property name and type and never null
Throws:
BeanException - If the write method can not be found

callGetter

public static Object callGetter(String property,
                                Object bean)
                         throws BeanException
Another convience method for calling a getter on a java bean. The given property name must describe a local property, not a nested property.
Parameters:
property - The name of the property
bean - The object to call it on
Returns:
The return value from calling the getter method for the given property
Throws:
BeanException - If something goes wrong

callGetter

public static Object callGetter(BeanProperty prop,
                                Object bean)
                         throws BeanException
Another convience method that calls a java bean getter and returns the value. This version uses the BeanProperty class to determine the property read method.
Parameters:
prop - The BeanProperty object to use
bean - The java bean object to call it on
Returns:
The return value from calling the getter method for the given property
Throws:
BeanException - If there was a problem calling the getter

callIndexedGetter

public static Object callIndexedGetter(BeanProperty prop,
                                       Object bean,
                                       int index)
                                throws BeanException
Another convience method that calls an indexed java bean getter and returns the value. This version uses the IndexedBeanProperty class to determine the property read method.
Parameters:
prop - The BeanProperty object to use
bean - The java bean object to call it on
index - The index to be passed to the getter method
Returns:
The return value from calling the indexed getter method for the given property
Throws:
BeanException - If there was a problem calling the getter

callSetter

public static void callSetter(String property,
                              Object bean,
                              Object param)
                       throws BeanException
Another convience method for calling a setter on a java bean. The given property name must describe a local property, not a nested property.
Parameters:
property - The name of the property of the setter
bean - The java bean object to call it on
param - The parameter to the setter (java bean standard)
Throws:
BeanException - If the property is not a valid property or a read only property or if the setter called threw an exception or if the param is the incorrect type for the setter method

callSetter

public static void callSetter(BeanProperty prop,
                              Object bean,
                              Object param)
                       throws BeanException
Another convience method for calling a setter on a java bean. This method uses the BeanProperty object to determine the write method
Parameters:
prop - The BeanProperty object to use when calling the setter
object - The object to call it on
param - The parameter to the setter (java bean standard)
Throws:
BeanException - If the property a read only property or if the setter called threw an exception or if the param is the incorrect type for the setter method

callIndexedSetter

public static void callIndexedSetter(BeanProperty prop,
                                     Object bean,
                                     int index,
                                     Object param)
                              throws BeanException
Another convience method for calling a setter on a java bean. This method uses the BeanProperty object to determine the write method.
Parameters:
prop - The BeanProperty object to use when calling the setter
object - The object to call it on
index - The index of the property to set
param - The parameter to the setter (java bean standard)
Throws:
BeanException - If the property a read only property or if the setter called threw an exception or if the param is the incorrect type for the setter method

splitName

public static JavaBeanTools.NameHelper splitName(String propertyName)
Using the given nested property name, this returns the local property name and the remainder of the nested property name, if any. This is done by returning a instance of the top level class NameHelper of this class
Parameters:
propertyName - The nested (or local) property name to extract the local and nested property names from.
Returns:
The NameHelper class for the names

findIndex

public static JavaBeanTools.IndexHelper findIndex(String propertyName)
                                           throws BeanException
Given the property name string, the index and property name are extracted and returned in a new IndexHelper. If the given propertyName string does not contain an index, then the index member of the IndexHelper returned will be -1.
Parameters:
propertyName - The name of the property to search for the index
Returns:
A new IndexHelper that contains all the information
Throws:
BeanException - If the property name contains an invild index or an unclosed index notation (i.e. '[1')

findIndex

public static JavaBeanTools.IndexHelper findIndex(String propertyName,
                                                  JavaBeanTools.IndexHelper indexHelper)
                                           throws BeanException
Given the property name string, the index and property name are extracted and returned in the given IndexHelper. If the given propertyName string does not contain an index, then the index member of the IndexHelper returned will be -1. Inside the IndexHelper, the property name is stored in the propertyName member and the index is stored in the index member.
Parameters:
propertyName - The name of the property to search for the index
indexHelper - The IndexHelper instance to store the new information in and the same IndexHelper instance that is returned
Returns:
The given IndexHelper instance that contains all the new information
Throws:
BeanException - If the property name contains an invalid index or an unclosed index notation (i.e. '[1')

removeIndices

public static JavaBeanTools.IndexHelper removeIndices(String propertyName)
                                               throws BeanException
This method takes the given property string and removes all the indices from it and stores them in an List. It then reconstructs the property String without the indices and stores the List and the new property String in a new IndexHelper class. Inside the IndexHelper, the property name is stored in the propertyName member and the indices list is stored in the indices member
Parameters:
propertyName - The name of the property to extract the indices from
Returns:
The new instance of the IndexHelper class that contains the information
Throws:
BeanException - If the property name contains one or invalid indice(s) or an unclosed index notation (i.e. '[1')