com.taco.lang.reflect
Class ReflectionUtilities

java.lang.Object
  extended by com.taco.lang.reflect.ReflectionUtilities

public class ReflectionUtilities
extends java.lang.Object

A utility class that contains useful methods dealing with reflection.


Constructor Summary
protected ReflectionUtilities()
          Since this class is a static utility class, its sole constructor is protected.
 
Method Summary
static java.lang.String computeGetMethodName(java.lang.String propertyName)
          Compute the name of the get method that used to get the property value, based on the name of property.
static java.lang.String computeSetMethodName(java.lang.String propertyName)
          Compute the name of the set method that used to set the property value, based on the name of property.
static java.util.Map createEmptyMap(java.util.Map map)
          Create an empty map with the same type as the argument.
static java.util.Map createMapCopy(java.util.Map map)
          Create a clone of the argument map, preferrably using the single map argument constructor of the argument map's class.
static java.lang.reflect.Method findSetMethod(java.lang.Class compositeClass, java.lang.String setMethodName, java.lang.Object propertyValue, boolean unwrap)
          Given the class object of an object which has a settable property, the name of the set method, and new property value, return the method that sets the value.
static java.lang.reflect.Method findSetMethodForNonNullValue(java.lang.Class compositeClass, java.lang.String setMethodName, java.lang.Object propertyValue, boolean unwrap)
          Find the method to set a non-null value.
static java.lang.reflect.Method findSetMethodForNullValue(java.lang.Class compositeClass, java.lang.String setMethodName)
          Find the method to set a null value.
static java.lang.Object forceClone(java.lang.Object obj)
          Using reflection, try to clone obj.
static boolean isPrimitiveWrapperClass(java.lang.Class c)
          Return true if c is a class that wraps on of the primitive types.
static java.lang.Class primitiveTypeForWrapperClass(java.lang.Class c)
          If c is a primitive wrapper class, return the class object for the type it wraps.
static void setProperty(java.lang.Object composite, java.lang.String propertyName, java.lang.Object propertyValue, boolean unwrap)
          Set a property of a bean.
static java.lang.Class wrapperClassToPrimitiveType(java.lang.Class c)
          If c is a class object for a primitive type, return the class object for the corresponding wrapper class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionUtilities

protected ReflectionUtilities()
Since this class is a static utility class, its sole constructor is protected.

Method Detail

forceClone

public static final java.lang.Object forceClone(java.lang.Object obj)
                                         throws java.lang.CloneNotSupportedException
Using reflection, try to clone obj. If obj is null, return null. If obj does not implement Cloneable or an exception occurs invoking clone(), throw a CloneNotSupportedException.

Parameters:
obj - The object to clone
Returns:
A clone of the object
Throws:
java.lang.NullPointerException - if obj is null
java.lang.CloneNotSupportedException - if the argument map doesn't have a default constructor or the constructor throws an exception, and the map cannot be cloned using clone().

createEmptyMap

public static final java.util.Map createEmptyMap(java.util.Map map)
                                          throws java.lang.CloneNotSupportedException
Create an empty map with the same type as the argument. First, check if the argument's class has a constructor with no arguments. If so, return the result of construction. Otherwise, clone the argument with forceClone() and clear the map before returning it to the caller.

Parameters:
map - The map to clone
Returns:
A clone of the map
Throws:
java.lang.NullPointerException - if map is null
java.lang.CloneNotSupportedException - if the argument map doesn't have a default constructor or the constructor throws an exception, and the map cannot be cloned using clone().

createMapCopy

public static final java.util.Map createMapCopy(java.util.Map map)
                                         throws java.lang.CloneNotSupportedException
Create a clone of the argument map, preferrably using the single map argument constructor of the argument map's class. If that is not possible, clone the map using forceClone().

Parameters:
map - The map to clone
Returns:
A clone of the map
Throws:
java.lang.NullPointerException - if map is null
java.lang.CloneNotSupportedException - if the argument map doesn't have a single map argument constructor or the constructor throws an exception, and the map cannot be cloned using clone().

setProperty

public static final void setProperty(java.lang.Object composite,
                                     java.lang.String propertyName,
                                     java.lang.Object propertyValue,
                                     boolean unwrap)
                              throws java.lang.NoSuchMethodException,
                                     java.lang.IllegalAccessException,
                                     java.lang.reflect.InvocationTargetException
Set a property of a bean. If unwrap is true and propertyValue is an instance of a wrapper class for a primitive type, look for a method that has a single argument with the corresponding primitive type.

Throws:
java.lang.NoSuchMethodException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException

computeGetMethodName

public static final java.lang.String computeGetMethodName(java.lang.String propertyName)
Compute the name of the get method that used to get the property value, based on the name of property.

Parameters:
propertyName - the name of the property to get
Returns:
The name of the get method used to get the property value
Throws:
java.lang.NullPointerException - if propertyName is null

computeSetMethodName

public static final java.lang.String computeSetMethodName(java.lang.String propertyName)
Compute the name of the set method that used to set the property value, based on the name of property.

Parameters:
propertyName - the name of the property to set
Returns:
The name of the set method used to get the property value
Throws:
java.lang.NullPointerException - if propertyName is null

findSetMethod

public static final java.lang.reflect.Method findSetMethod(java.lang.Class compositeClass,
                                                           java.lang.String setMethodName,
                                                           java.lang.Object propertyValue,
                                                           boolean unwrap)
                                                    throws java.lang.NoSuchMethodException
Given the class object of an object which has a settable property, the name of the set method, and new property value, return the method that sets the value. If unwrap is true and propertyValue is an instance of a wrapper class for a primitive type, look for a method that has a single argument with the corresponding primitive type.

Throws:
java.lang.NoSuchMethodException

findSetMethodForNullValue

public static final java.lang.reflect.Method findSetMethodForNullValue(java.lang.Class compositeClass,
                                                                       java.lang.String setMethodName)
                                                                throws java.lang.NoSuchMethodException
Find the method to set a null value. This implementation searches all the methods of the composite class, looking for one that matches the argument name and takes a single non-primitive parameter. If no matching method is found, throw a NoSuchMethodException.

Throws:
java.lang.NoSuchMethodException

findSetMethodForNonNullValue

public static final java.lang.reflect.Method findSetMethodForNonNullValue(java.lang.Class compositeClass,
                                                                          java.lang.String setMethodName,
                                                                          java.lang.Object propertyValue,
                                                                          boolean unwrap)
                                                                   throws java.lang.NoSuchMethodException
Find the method to set a non-null value. This implementation searches for a method with a single argument of the same type as propertyValue. If found, it returns it immediately. Otherwise, it repeats using the superclass of propertyValue's type as the argument type. If no method can be found, throw a NoSuchMethodException.

Throws:
java.lang.NoSuchMethodException

isPrimitiveWrapperClass

public static final boolean isPrimitiveWrapperClass(java.lang.Class c)
Return true if c is a class that wraps on of the primitive types.


primitiveTypeForWrapperClass

public static final java.lang.Class primitiveTypeForWrapperClass(java.lang.Class c)
If c is a primitive wrapper class, return the class object for the type it wraps. Otherwise, return null.

Parameters:
c - The class to get the primitive class for
Returns:
The corresponding primitive class for the argument class, or null if the argument class is not a primitive wrapper class
Throws:
java.lang.NullPointerException - if c is null

wrapperClassToPrimitiveType

public static final java.lang.Class wrapperClassToPrimitiveType(java.lang.Class c)
If c is a class object for a primitive type, return the class object for the corresponding wrapper class. Otherwise, return null.

Parameters:
c - The class to get the primitive wrapper class for
Returns:
The corresponding wrapper class for the argument class, or null if the argument class is not a primitive class
Throws:
java.lang.NullPointerException - if c is null