com.sptci
Class ReflectionUtility

java.lang.Object
  extended by com.sptci.ReflectionUtility

public final class ReflectionUtility
extends Object

A utility class to handle common reflection tasks.

Copyright 2006 Sans Pareil Technologies, Inc.

Version:
$Id: ReflectionUtility.java 3296 2007-05-26 11:53:26Z rakesh $
Author:
Rakesh Vidyadharan 2006-02-07

Nested Class Summary
private static class ReflectionUtility.MethodEntry
          A holder object to store a combination of a method name and parameter array.
 
Field Summary
private static Map<Class,Map<ReflectionUtility.MethodEntry,Constructor>> constructors
          A map used to cache classes and their constructors for efficiency.
private static Map<Class,Map<String,Field>> fields
          A map used to cache classes and their fields for efficiency.
private static Map<Class,Map<ReflectionUtility.MethodEntry,Method>> methods
          A map used to cache classes and their methods for efficiency.
 
Constructor Summary
private ReflectionUtility()
          Default constructor.
 
Method Summary
static Method fetchAccessor(String property, Class cls)
          Fetches the accessor method for the specified property in the specified class.
static Method fetchAccessor(String property, Object object)
          Fetches the accessor method for the specified property in the specified object.
static Constructor fetchConstructor(Class cls, Class... parameterTypes)
          Return the Constructor instance for the specified class.
static Constructor fetchConstructor(String cls, Class... parameterTypes)
          Return the Constructor instance for the specified class.
static Field fetchField(String name, Class cls)
          Return the Field instance with the specified name from the Class specified.
static Field fetchField(String name, Object object)
          Return the Field instance with the specified name from the object.
static Map<String,Field> fetchFields(Class cls)
          Return the declared fields for the specified Class.
static Map<String,Field> fetchFields(Object object)
          Return the declared fields for the specified object.
static Method fetchMethod(Class cls, String name, Class... parameters)
          Fetch the Method defined in the specified class with the specified name that accepts the specified parameters.
static Method fetchMethod(Field field, String name, Class... parameters)
          Fetch the Method defined in the specified field with the specified name that accepts the specified parameters.
static Method fetchMethod(Object object, String name, Class... parameters)
          Fetch the Method defined in the specified object with the specified name that accepts the specified parameters.
static Method fetchMutator(String property, Class cls)
          Fetches the mutator method for the specified property in the specified class.
static Method fetchMutator(String property, Object object)
          Fetches the mutator method for the specified property in the specified object.
static Object fetchObject(String name, Object object)
          Return the Object instance represented by the specified name from object.
static Map<String,Object> fetchObjects(Object object)
          Return the Object instances representing the declared fields for the specified object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fields

private static final Map<Class,Map<String,Field>> fields
A map used to cache classes and their fields for efficiency.


methods

private static final Map<Class,Map<ReflectionUtility.MethodEntry,Method>> methods
A map used to cache classes and their methods for efficiency.


constructors

private static final Map<Class,Map<ReflectionUtility.MethodEntry,Constructor>> constructors
A map used to cache classes and their constructors for efficiency.

Constructor Detail

ReflectionUtility

private ReflectionUtility()
Default constructor. Cannot be instantiated.

Method Detail

fetchConstructor

public static final Constructor fetchConstructor(String cls,
                                                 Class... parameterTypes)
                                          throws ClassNotFoundException,
                                                 NoSuchMethodException
Return the Constructor instance for the specified class.

Parameters:
cls - The fully qualified name of the class.
parameterTypes - The class type(s) of the constructor parameter(s).
Throws:
ClassNotFoundException - If the specified class could not be located.
NoSuchMethodException - If the specified constructor method could not be found.

fetchConstructor

public static final Constructor fetchConstructor(Class cls,
                                                 Class... parameterTypes)
                                          throws NoSuchMethodException
Return the Constructor instance for the specified class.

Parameters:
cls - The class whose constructor is to be fetched.
parameterTypes - The class type(s) of the constructor parameter(s).
Throws:
NoSuchMethodException - If the specified constructor method could not be found.

fetchField

public static final Field fetchField(String name,
                                     Object object)
                              throws NoSuchFieldException
Return the Field instance with the specified name from the object. Sets the field accessible if it is not accessible.

Parameters:
name - The name of the field to look up.
object - The object from which the field is to be retrieved.
Throws:
NoSuchFieldException - If no field with the specified name exists in object.
See Also:
fetchField( String, Class )

fetchField

public static final Field fetchField(String name,
                                     Class cls)
                              throws NoSuchFieldException
Return the Field instance with the specified name from the Class specified. Sets the field accessible if it is not accessible.

Parameters:
name - The name of the field to look up.
cls - The class from which the field is to be retrieved.
Returns:
Returns the appropriate field instance.
Throws:
NoSuchFieldException - If no field with the specified name exists in object.

fetchObject

public static final Object fetchObject(String name,
                                       Object object)
                                throws NoSuchFieldException,
                                       IllegalAccessException
Return the Object instance represented by the specified name from object.

Parameters:
name - The name of the object to fetch.
object - The object from which the field is to be retrieved.
Returns:
Object The object instance for the specified field.
Throws:
NoSuchFieldException - If no field with the specified name exists in object.
IllegalAccessException - If the field cannot be accessed due to custom security policies.
See Also:
fetchField(java.lang.String, java.lang.Object)

fetchFields

public static final Map<String,Field> fetchFields(Object object)
Return the declared fields for the specified object.

Parameters:
object - The object whose declared fields are to be retrieved.
Returns:
Map A map with the field names as key and the field as value
See Also:
fetchFields( Class )

fetchFields

public static final Map<String,Field> fetchFields(Class cls)
Return the declared fields for the specified Class.

Parameters:
cls - The class whose declared fields are to be retrieved.
Returns:
Map A map with the field names as key and the field as value

fetchObjects

public static final Map<String,Object> fetchObjects(Object object)
                                             throws IllegalAccessException
Return the Object instances representing the declared fields for the specified object.

Parameters:
object - The object for which objects for the declared fields are to be retrieved.
Returns:
Map A map with the field names as key and the object as value
Throws:
IllegalAccessException - If a custom security policy prevents access to the fields.
See Also:
fetchFields(java.lang.Object)

fetchMethod

public static final Method fetchMethod(Field field,
                                       String name,
                                       Class... parameters)
Fetch the Method defined in the specified field with the specified name that accepts the specified parameters. Please note that this method does not throw a NoSuchMethodException, but returns null instead.

Parameters:
field - The field whose method is to be retrieved.
name - The name of the method to retrieve.
parameters - The parameters accepted by the method.
See Also:
fetchMethod( Class, String, Class[] )

fetchMethod

public static final Method fetchMethod(Object object,
                                       String name,
                                       Class... parameters)
Fetch the Method defined in the specified object with the specified name that accepts the specified parameters. Please note that this method does not throw a NoSuchMethodException, but returns null instead.

Parameters:
object - The object whose method is to be retrieved.
name - The name of the method to retrieve.
parameters - The parameters accepted by the method.
See Also:
fetchMethod( Class, String, Class[] )

fetchMethod

public static final Method fetchMethod(Class cls,
                                       String name,
                                       Class... parameters)
Fetch the Method defined in the specified class with the specified name that accepts the specified parameters. Please note that this method does not throw a NoSuchMethodException, but returns null instead.

Parameters:
cls - The cls whose method is to be retrieved.
name - The name of the method to retrieve.
parameters - The parameters accepted by the method.
Returns:
The method instance for the specified name or null if no such method could be found.

fetchAccessor

public static final Method fetchAccessor(String property,
                                         Object object)
                                  throws IntrospectionException
Fetches the accessor method for the specified property in the specified object.

Parameters:
property - The property whose accessor is to be fetched.
object - The object to which the property belongs.
Returns:
Method The accessor method for the property.
Throws:
IntrospectionException - If errors are encountered while introspecting the specified class.
See Also:
fetchAccessor( String, Class )

fetchAccessor

public static final Method fetchAccessor(String property,
                                         Class cls)
                                  throws IntrospectionException
Fetches the accessor method for the specified property in the specified class.

Parameters:
property - The property whose accessor is to be fetched.
cls - The class to which the property belongs.
Returns:
Method The accessor method for the property.
Throws:
IntrospectionException - If errors are encountered while introspecting the specified class.

fetchMutator

public static final Method fetchMutator(String property,
                                        Object object)
                                 throws IntrospectionException
Fetches the mutator method for the specified property in the specified object.

Parameters:
property - The property whose mutator is to be fetched.
object - The object to which the property belongs.
Returns:
Method The mutator method for the property.
Throws:
IntrospectionException - If errors are encountered while introspecting the specified class.
See Also:
fetchMutator( String, Class )

fetchMutator

public static final Method fetchMutator(String property,
                                        Class cls)
                                 throws IntrospectionException
Fetches the mutator method for the specified property in the specified class.

Parameters:
property - The property whose mutator is to be fetched.
cls - The class to which the property belongs.
Returns:
Method The mutator method for the property.
Throws:
IntrospectionException - If errors are encountered while introspecting the specified class.