com.ge.research.utils
Class IntrospectionUtils

java.lang.Object
  extended bycom.ge.research.utils.IntrospectionUtils
Direct Known Subclasses:
IntrospectionUtils

public class IntrospectionUtils
extends java.lang.Object

A utility class with many conveinence methods for handling common introspection tasks. Most of the methods simply wrap those utilities found in the java.lang.reflect package, catching and suppressing any possible errors.

Version:
$Revision: 1.1 $ $Date: 2006/08/11 00:17:39 $
Author:
garbiras
Created on:
Aug 20, 2002

Constructor Summary
IntrospectionUtils()
           
 
Method Summary
static java.lang.Class[] getAllInheritedClasses(java.lang.Object o)
          Calls the getAllInheritedClasses(java.lang.Object, java.lang.Class) with arguments o and java.lang.Object.class.
static java.lang.Class[] getAllInheritedClasses(java.lang.Object o, java.lang.Class stopBefore)
          Returns an array of all the classes declared as a superclass or superinterface of the argument object, up to but not including the stopBefore class.
static java.lang.reflect.Field[] getAllMembers(java.lang.Object o)
          Returns the array of all fields (public, protected, and private) declared in the class of the o argument, and all of its superclasses.
static java.lang.reflect.Field getMember(java.lang.Object o, java.lang.String name)
          Executes the Class.getField(java.lang.String) method, trapping and suppressing any errors.
static java.lang.Class getMemberType(java.lang.Object o, java.lang.String name)
          Determines the Class type of the member field, specified by name.
static java.lang.Object getMemberValue(java.lang.Object o, java.lang.reflect.Field member)
          Executes the Field.get(java.lang.Object) method, trapping and suppressing any errors.
static java.lang.Object getMemberValue(java.lang.Object o, java.lang.String name)
          Calls getMemberValue(java.lang.Object, java.lang.reflect.Field) method with arguments o and the return value from the call to getMember(java.lang.Object, java.lang.String) method with arguments o and name.
static java.lang.reflect.Method getMethod(java.lang.Class cls, java.lang.String methodName, java.lang.Class[] paramTypes)
          Executes the Class.getMethod(java.lang.String, java.lang.Class[]) method, trapping and suppressing any errors.
static java.lang.reflect.Method getMethod(java.lang.Class cls, java.lang.String methodName, java.lang.Object[] args)
          Attempts to find a method named methodName on the given class such that the given arguments can be passed to that method successfully.
static java.lang.Object getNewInstance(java.lang.Class cls)
          Calls the getNewInstance(java.lang.Class, java.lang.Object[]) method with null as the second argument.
static java.lang.Object getNewInstance(java.lang.Class cls, java.lang.Object[] args)
          The args array is used to determine which constructor of the class to use, and passes the values of the array to that constructor to instatiate the class.
static java.lang.Object getNewInstance(java.lang.String classname, java.lang.Class classType)
          Calls the getNewInstance(java.lang.String, java.lang.Class, java.lang.Object[]) method with null as the third argument.
static java.lang.Object getNewInstance(java.lang.String classname, java.lang.Class classType, java.lang.Object[] args)
          Creates an instance of a class named classname.
static java.lang.Class getPrimitiveClass(java.lang.Number num)
          Returns the primitive number class represented by this Number object.
static java.lang.Object invokeMethod(java.lang.reflect.Method m, java.lang.Object o, java.lang.Object[] args)
          Executes the Method.invoke(java.lang.Object, java.lang.Object[]) method, trapping and suppressing any errors.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IntrospectionUtils

public IntrospectionUtils()
Method Detail

getAllInheritedClasses

public static java.lang.Class[] getAllInheritedClasses(java.lang.Object o)
Calls the getAllInheritedClasses(java.lang.Object, java.lang.Class) with arguments o and java.lang.Object.class. Returns an array of all the classes declared as a superclass or superinterface of the argument object (up to but not including java.lang.Object). The returned array also includes the class of the argument object.

Parameters:
o - the object to find all superclasses and superinterfaces for
Returns:
The array of class objects representing all superclasses and superinterfaces of the o argument, including the class type of the o argument
See Also:
getAllInheritedClasses(java.lang.Object, java.lang.Class)

getAllInheritedClasses

public static java.lang.Class[] getAllInheritedClasses(java.lang.Object o,
                                                       java.lang.Class stopBefore)
Returns an array of all the classes declared as a superclass or superinterface of the argument object, up to but not including the stopBefore class. The returned array also includes the class of the argument object.

Parameters:
o - the object to find all superclasses and superinterfaces for
stopBefore - the highest level of inherited classes to return as part of the array. The introspection stops when reaching this class type
Returns:
The array of class objects representing all superclasses and superinterfaces of the o argument, including the class type of the o argument

getAllMembers

public static java.lang.reflect.Field[] getAllMembers(java.lang.Object o)
Returns the array of all fields (public, protected, and private) declared in the class of the o argument, and all of its superclasses.

Parameters:
o - the object to introspect for all fields (public, protected, and private) declared in its class and all inherited classes
Returns:
The array of fields (public, protected, and private) declared in the argument class and all its inherited classes.
See Also:
getAllInheritedClasses(java.lang.Object)

getMember

public static java.lang.reflect.Field getMember(java.lang.Object o,
                                                java.lang.String name)
Executes the Class.getField(java.lang.String) method, trapping and suppressing any errors.

Parameters:
o - the object of which to find the field
name - the name of the field to find
Returns:
The field, named name of the class of the object o. Returns null if an error occured looking for the field.

getMemberType

public static java.lang.Class getMemberType(java.lang.Object o,
                                            java.lang.String name)
Determines the Class type of the member field, specified by name.

Parameters:
o - the object of which to find the field
name - the name of the field to find
Returns:
The field type, named name of the class of the object o. Returns null if an error occured looking for the field.

getMemberValue

public static java.lang.Object getMemberValue(java.lang.Object o,
                                              java.lang.String name)
Calls getMemberValue(java.lang.Object, java.lang.reflect.Field) method with arguments o and the return value from the call to getMember(java.lang.Object, java.lang.String) method with arguments o and name.

Parameters:
o - the object to get the field value from
name - the name of the field to get the value for
Returns:
The value of the field name in the object o Returns null if an error occured getting the value for the field.
See Also:
getMember(java.lang.Object, java.lang.String), getMemberValue(java.lang.Object, java.lang.reflect.Field)

getMemberValue

public static java.lang.Object getMemberValue(java.lang.Object o,
                                              java.lang.reflect.Field member)
Executes the Field.get(java.lang.Object) method, trapping and suppressing any errors.

Parameters:
o - the object to retrieve the field value from
member - the field to retrieve the value of
Returns:
The value of the field member in the object o Returns null if an error occured getting the value for the field.

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class cls,
                                                 java.lang.String methodName,
                                                 java.lang.Class[] paramTypes)
Executes the Class.getMethod(java.lang.String, java.lang.Class[]) method, trapping and suppressing any errors.

Parameters:
cls - the class to introspect and find the method of
methodName - the name of the method to find
paramTypes - the method signature types for the method
Returns:
The method, named methodName with signature paramTypes of class cls. Returns null if an error occured looking for the method.

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class cls,
                                                 java.lang.String methodName,
                                                 java.lang.Object[] args)
Attempts to find a method named methodName on the given class such that the given arguments can be passed to that method successfully.

Parameters:
cls - the class to introspect and find the method of
methodName - the name of the method to find
args - the method arguments for the method
Returns:
The method, named methodName with signature paramTypes of class cls. Returns null if an error occured looking for the method.

getNewInstance

public static java.lang.Object getNewInstance(java.lang.Class cls)
Calls the getNewInstance(java.lang.Class, java.lang.Object[]) method with null as the second argument.

Parameters:
cls - the class to create an instance object of
Returns:
The new instance of the cls class. Returns null if an error occured preventing the class from being instantiated.
See Also:
getNewInstance(java.lang.Class, java.lang.Object[])

getNewInstance

public static java.lang.Object getNewInstance(java.lang.Class cls,
                                              java.lang.Object[] args)
The args array is used to determine which constructor of the class to use, and passes the values of the array to that constructor to instatiate the class.

Parameters:
cls - the class to create an instance object of
args - the constructor argument objects to use to instantiate the new class
Returns:
The new instance of the cls class. Returns null if an error occured preventing the class from being instantiated.
See Also:
Class.getConstructor(java.lang.Class[]), Constructor.newInstance(java.lang.Object[])

getNewInstance

public static java.lang.Object getNewInstance(java.lang.String classname,
                                              java.lang.Class classType)
Calls the getNewInstance(java.lang.String, java.lang.Class, java.lang.Object[]) method with null as the third argument.

Parameters:
classname - the name of the class to instantiate
classType - the class (superclass, interface, or superinterface) the argument classname class should be checked to be assignable from Returns null if an error occured preventing the class from being instantiated.
See Also:
getNewInstance(java.lang.String, java.lang.Class, java.lang.Object[])

getNewInstance

public static java.lang.Object getNewInstance(java.lang.String classname,
                                              java.lang.Class classType,
                                              java.lang.Object[] args)
Creates an instance of a class named classname. The class is loaded, and then its type is checked to be a valid subtype of the classType argument. Finally calls the getNewInstance(java.lang.Class, java.lang.Object[]) method.

Parameters:
classname - the name of the class to instantiate
classType - the class (superclass, interface, or superinterface) the argument classname class should be checked to be assignable from
args - the constructor argument objects to use to instantiate the new class
Returns:
The new instance of the classname class. Returns null if an error occured preventing the class from being instantiated.
See Also:
Class.forName(java.lang.String), Class.isAssignableFrom(java.lang.Class), getNewInstance(java.lang.Class, java.lang.Object[])

getPrimitiveClass

public static final java.lang.Class getPrimitiveClass(java.lang.Number num)
Returns the primitive number class represented by this Number object.

Parameters:
num - the Number to get the primitive type for
Returns:
The primitive number class represented by this Number object.

invokeMethod

public static java.lang.Object invokeMethod(java.lang.reflect.Method m,
                                            java.lang.Object o,
                                            java.lang.Object[] args)
Executes the Method.invoke(java.lang.Object, java.lang.Object[]) method, trapping and suppressing any errors.

Parameters:
m - the method to invoke
o - the object on which to invoke the method
args - the object array of arguments for the method invoked
Returns:
The return value, if any, from the invoked method. Returns null if an error occured while trying to invoke the method.