ecologylab.generic
Class ReflectionTools

java.lang.Object
  extended by ecologylab.generic.Debug
      extended by ecologylab.generic.ReflectionTools

public class ReflectionTools
extends Debug

Utility routines for working with reflection.

Author:
andruid

Field Summary
static java.lang.Object BAD_ACCESS
           
 
Constructor Summary
ReflectionTools()
           
 
Method Summary
static java.lang.reflect.Field getDeclaredField(java.lang.Class thatClass, java.lang.String fieldName)
          Get the Field object with name fieldName, in thatClass.
static java.lang.reflect.Field getField(java.lang.Class thatClass, java.lang.String fieldName)
          Get the Field object with name fieldName, in thatClass.
static java.lang.Object getFieldValue(java.lang.Object that, java.lang.reflect.Field field)
          Return the value of the Field in the Object, or BAD_ACCESS if it can't be accessed.
static
<T> T
getInstance(java.lang.Class<T> thatClass)
          Wraps the no argument getInstance() method.
static
<T> T
getInstance(java.lang.Class<T> thatClass, java.lang.Class[] parameterTypes, java.lang.Object[] args)
          Wraps the no argument getInstance() method.
static java.lang.reflect.Method getMethod(java.lang.Class context, java.lang.String name, java.lang.Class[] types)
          Find a Method object if there is one in the context class, or return null if not.
static java.lang.reflect.Type[] getParameterizedTypeTokens(java.lang.reflect.Field field)
          Get the parameterized type tokens that the generic Field was declared with.
static boolean isAnnotationPresent(java.lang.reflect.Field field, java.lang.Class annotationClass)
          See if the Field has the annotation in its declaration.
static boolean setFieldValue(java.lang.Object that, java.lang.reflect.Field field, java.lang.Object value)
          Set a reference type Field to a value.
 
Methods inherited from class ecologylab.generic.Debug
classSimpleName, closeLoggingFile, debug, debug, debug, debug, debugA, debugA, debugA, debugI, debugI, debugI, error, error, getClassName, getClassName, getInteractive, getPackageName, getPackageName, getPackageName, initialize, level, level, level, logToFile, print, print, println, println, println, println, println, println, printlnA, printlnA, printlnA, printlnI, printlnI, printlnI, printlnI, setLoggingFile, show, show, superString, toggleInteractive, toString, toString, warning, warning, weird, weird
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

BAD_ACCESS

public static final java.lang.Object BAD_ACCESS
Constructor Detail

ReflectionTools

public ReflectionTools()
Method Detail

getField

public static java.lang.reflect.Field getField(java.lang.Class thatClass,
                                               java.lang.String fieldName)
Get the Field object with name fieldName, in thatClass.

Parameters:
thatClass -
fieldName -
Returns:
The Field object in thatClass, or null if there is none accessible.

getDeclaredField

public static java.lang.reflect.Field getDeclaredField(java.lang.Class thatClass,
                                                       java.lang.String fieldName)
Get the Field object with name fieldName, in thatClass.

Parameters:
thatClass -
fieldName -
Returns:
The Field object in thatClass, or null if there is none accessible.

getFieldValue

public static java.lang.Object getFieldValue(java.lang.Object that,
                                             java.lang.reflect.Field field)
Return the value of the Field in the Object, or BAD_ACCESS if it can't be accessed.

Parameters:
that -
field -
Returns:

setFieldValue

public static boolean setFieldValue(java.lang.Object that,
                                    java.lang.reflect.Field field,
                                    java.lang.Object value)
Set a reference type Field to a value.

Parameters:
that - Object that the field is in.
field - Reference type field within that object.
value - Value to set the reference field to.
Returns:
true if the set succeeds.

getInstance

public static <T> T getInstance(java.lang.Class<T> thatClass)
Wraps the no argument getInstance() method. Checks to see if the class object passed in is null, or if any exceptions are thrown by newInstance().

Parameters:
thatClass -
Returns:
An instance of an object of the specified class, or null if the Class object was null or an InstantiationException or IllegalAccessException was thrown in the attempt to instantiate.

getInstance

public static <T> T getInstance(java.lang.Class<T> thatClass,
                                java.lang.Class[] parameterTypes,
                                java.lang.Object[] args)
Wraps the no argument getInstance() method. Checks to see if the class object passed in is null, or if any exceptions are thrown by newInstance().

Parameters:
thatClass -
Returns:
An instance of an object of the specified class, or null if the Class object was null or an InstantiationException or IllegalAccessException was thrown in the attempt to instantiate.

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class context,
                                                 java.lang.String name,
                                                 java.lang.Class[] types)
Find a Method object if there is one in the context class, or return null if not.

Parameters:
context - Class to find the Method in.
name - Name of the method.
types - Array of Class objects indicating parameter types.
Returns:
The associated Method object, or null if non is accessible.

isAnnotationPresent

public static boolean isAnnotationPresent(java.lang.reflect.Field field,
                                          java.lang.Class annotationClass)
See if the Field has the annotation in its declaration.

Parameters:
field -
annotationClass -
Returns:

getParameterizedTypeTokens

public static java.lang.reflect.Type[] getParameterizedTypeTokens(java.lang.reflect.Field field)
Get the parameterized type tokens that the generic Field was declared with.

Parameters:
reflectType -
Returns: