SPT Core API

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 4553 2008-12-24 10:34:16Z rakesh $
Author:
Rakesh Vidyadharan 2006-02-07

Method Summary
static Object execute(Object object, String name, Object... parameters)
          Invoke the specified method on the specified object instance using the specified parameters.
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.
static PropertyDescriptor fetchPropertyDescriptor(String name, Class cls)
          Return the PropertyDescriptor for the specified class identified by the specified property name.
static Map<String,PropertyDescriptor> fetchPropertyDescriptors(Class cls)
          Return the PropertyDescriptors for the specified class.
static Map<String,PropertyDescriptor> fetchPropertyDescriptors(Object object)
          Return the PropertyDescriptors for the specified object.
static Collection<String> fetchPropertyNames(Class cls)
          Return a collection of the property names that are accessible in the specified class.
static Collection<String> fetchPropertyNames(Object object)
          Return a collection of the property names that are accessible in the specified object.
static boolean isChildClass(Class child, Class parent)
          Determine if the specified class is a subclass of the specified parent.
static Object newInstance(String cls, Object... parameters)
          Create a new instance of the specified class type using the specified parameters.
static void setField(Object source, String field, Object value)
          Set the value of the Field to the specified value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

fetchConstructor

public static Constructor fetchConstructor(String cls,
                                           Class... parameterTypes)
                                    throws ClassNotFoundException
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).
Returns:
The constructor instance or null if no matching constructor is found.
Throws:
ClassNotFoundException - If the specified class could not be located.

fetchConstructor

public static Constructor fetchConstructor(Class cls,
                                           Class... parameterTypes)
Return the Constructor instance for the specified class. Note that this method returns null and does not throw a NoSuchMethodException.

Parameters:
cls - The class whose constructor is to be fetched.
parameterTypes - The class type(s) of the constructor parameter(s).
Returns:
The constructor instance for the specified parameters. Return null if a constructor cannot be initialised (interface, enum, primitive, ...), or if a matching constructor does not exist.

newInstance

public static Object newInstance(String cls,
                                 Object... parameters)
                          throws ClassNotFoundException,
                                 NoSuchMethodException,
                                 IllegalArgumentException,
                                 IllegalAccessException,
                                 InstantiationException,
                                 InvocationTargetException
Create a new instance of the specified class type using the specified parameters. Note that the class of the parameters must exactly match the declared parameters for the Constructor.

Parameters:
cls - The fully qualified name of the class to tbe instantiated.
parameters - The arguments for the constructor.
Returns:
The new instance of the class specified. Returns null if the cls specified represents an abstract class, or is an interface.
Throws:
ClassNotFoundException - If class is not found.
NoSuchMethodException - If method is not found.
IllegalArgumentException - If argument is improper.
IllegalAccessException - If access protection is in place.
InstantiationException - If new instance cannot be created.
InvocationTargetException - If invocation semantics are wrong.

fetchField

public static Field fetchField(String name,
                               Object object)
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.
Returns:
The field instance corresponding to name or null if no matching field is found. Returns null if no matching field exists. In particular this method does not throw a NoSuchFieldException.
See Also:
fetchField( String, Class )

fetchField

public static Field fetchField(String name,
                               Class cls)
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. Returns null if no such field exists. This method does not throw a NoSuchFieldException.

fetchObject

public static 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 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 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 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)

setField

public static void setField(Object source,
                            String field,
                            Object value)
                     throws IllegalAccessException,
                            IllegalArgumentException,
                            NoSuchFieldException
Set the value of the Field to the specified value.

Parameters:
source - The object whose field is to be set.
field - The name of the field that is to be set.
value - The new value to set for the field.
Throws:
IllegalAccessException - If the underlying field is inaccessible.
IllegalArgumentException - If the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if an unwrapping conversion fails.
NoSuchFieldException - If no field with the specified name exists in source.
See Also:
fetchField( String, Object )

fetchMethod

public 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. 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.
Returns:
The specified method instance or null.
See Also:
fetchMethod( Class, String, Class[] )

fetchMethod

public 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. 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.
Returns:
The specified method instance or null.
See Also:
fetchMethod( Class, String, Class[] )

fetchMethod

public 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. 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 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:
fetchPropertyNames(java.lang.Object), fetchAccessor( String, Class )

fetchAccessor

public static 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 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:
fetchPropertyNames(java.lang.Object), fetchMutator( String, Class )

fetchMutator

public static 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.

fetchPropertyDescriptors

public static Map<String,PropertyDescriptor> fetchPropertyDescriptors(Object object)
                                                               throws IntrospectionException
Return the PropertyDescriptors for the specified object.

Parameters:
object - The object whose property descriptors are to be retrieved.
Returns:
A map with the property name as the key and property descriptor as value.
Throws:
IntrospectionException - If reflection errors occur.
See Also:
fetchPropertyDescriptors( Class )

fetchPropertyDescriptors

public static Map<String,PropertyDescriptor> fetchPropertyDescriptors(Class cls)
                                                               throws IntrospectionException
Return the PropertyDescriptors for the specified class.

Parameters:
cls - The class whose property descriptors are to be retrieved.
Returns:
A map with the property name as the key and property descriptor as value.
Throws:
IntrospectionException - If reflection errors occur.

fetchPropertyDescriptor

public static PropertyDescriptor fetchPropertyDescriptor(String name,
                                                         Class cls)
                                                  throws IntrospectionException
Return the PropertyDescriptor for the specified class identified by the specified property name.

Parameters:
name - The property name to use to look up the descriptor.
cls - The class in which the property is declared.
Returns:
The property descriptor or null if no such property is found.
Throws:
IntrospectionException - If reflection errors occur.

fetchPropertyNames

public static Collection<String> fetchPropertyNames(Object object)
                                             throws IntrospectionException
Return a collection of the property names that are accessible in the specified object.

Parameters:
object - The object whose accessible property name are to be retrieved.
Returns:
The collection of property names.
Throws:
IntrospectionException - If reflection errors occur.
See Also:
fetchPropertyNames( Class )

fetchPropertyNames

public static Collection<String> fetchPropertyNames(Class cls)
                                             throws IntrospectionException
Return a collection of the property names that are accessible in the specified class.

Parameters:
cls - The class whose accessible property name are to be retrieved.
Returns:
The collection of property names.
Throws:
IntrospectionException - If reflection errors occur.

execute

public static Object execute(Object object,
                             String name,
                             Object... parameters)
                      throws IllegalArgumentException,
                             IllegalAccessException,
                             InvocationTargetException
Invoke the specified method on the specified object instance using the specified parameters. Note that the class type of the parameters much exactly match the defined parameter types for the method. Usual Java behaviour of invoking appropriate method matching super-class of defined parameters do not work.

Parameters:
object - The object on which the method is to be executed.
name - The name of the method to invoke.
parameters - The parameters to specify to the method when executing.
Returns:
The object returned by executing the method.
Throws:
IllegalArgumentException - If the arguments are improper.
InvocationTargetException - If the method invocation fails.
IllegalAccessException - If access protection is in effect.
See Also:
fetchMethod( Object, String, Class... )

isChildClass

public static boolean isChildClass(Class child,
                                   Class parent)
Determine if the specified class is a subclass of the specified parent.

Parameters:
child - The child class to be checked for inheritance.
parent - The parent class against which the check is performed.
Returns:
Return true if a child.

SPT Core API