org.jlf.util
Class Reflection

java.lang.Object
  |
  +--org.jlf.util.Reflection

public class Reflection
extends java.lang.Object

The Reflection class is to help you with Java reflection apis and provide common code you can take advantage of.


Constructor Summary
protected Reflection()
          Protect the default constructor, so far no one should be creating an instance of this class.
 
Method Summary
static java.lang.Object executeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Class[] parameterTypes, java.lang.Object[] arguments)
          Executes a method on an object.
static java.lang.Class getClassForName(java.lang.String className)
          Helper method to get a class from a class name and throw a nice error if can't find the class through reflection.
static java.lang.Object getInstance(java.lang.Class subclass, java.lang.Class superclass)
          Creates an instance of the class given as input, validating it against the superclass given to make sure it is the same class or a subclass of the superclass.
static java.lang.Object getInstance(java.lang.Class subclass, java.lang.Class[] parameterTypes, java.lang.Object[] initArguments, java.lang.Class superclass)
          Creates an instance of the class given as input, validating it against the superclass given to make sure it is the same class or a subclass of the superclass, then trying to find a constructor according to the parameter types given.
static java.lang.Object getInstance(java.lang.String className, java.lang.Class superclass)
          Creates an instance of the class name given as input, validating it against the superclass given to make sure it is the same class or a subclass of the superclass.
static java.lang.Object getInstance(java.lang.String className, java.lang.Class[] parameterTypes, java.lang.Object[] initArguments, java.lang.Class superclass)
          Creates an instance of the class name given as input, validating it against the superclass given to make sure it is the same class or a subclass of the superclass, then trying to find a constructor according to the parameter types given.
static java.lang.Object getInstance(java.lang.String className, java.lang.String superclassName)
          Creates an instance of the class name given as input, validating it against the superclass given to make sure it is the same class or a subclass of the superclass.
static void validateClassAssignment(java.lang.Class subclass, java.lang.Class superclass)
          Helper method to validate a class assignment, making sure a subclass implements the interface of the desired superclass/interface.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Reflection

protected Reflection()
Protect the default constructor, so far no one should be creating an instance of this class.

Method Detail

getClassForName

public static java.lang.Class getClassForName(java.lang.String className)
Helper method to get a class from a class name and throw a nice error if can't find the class through reflection.


validateClassAssignment

public static void validateClassAssignment(java.lang.Class subclass,
                                           java.lang.Class superclass)
Helper method to validate a class assignment, making sure a subclass implements the interface of the desired superclass/interface.

Throws:
AppError - if validation unsuccessful

getInstance

public static java.lang.Object getInstance(java.lang.Class subclass,
                                           java.lang.Class superclass)
Creates an instance of the class given as input, validating it against the superclass given to make sure it is the same class or a subclass of the superclass.


getInstance

public static java.lang.Object getInstance(java.lang.String className,
                                           java.lang.Class superclass)
Creates an instance of the class name given as input, validating it against the superclass given to make sure it is the same class or a subclass of the superclass.


getInstance

public static java.lang.Object getInstance(java.lang.String className,
                                           java.lang.String superclassName)
Creates an instance of the class name given as input, validating it against the superclass given to make sure it is the same class or a subclass of the superclass.


executeMethod

public static java.lang.Object executeMethod(java.lang.Object object,
                                             java.lang.String methodName,
                                             java.lang.Class[] parameterTypes,
                                             java.lang.Object[] arguments)
Executes a method on an object. Does all sorts of error handling like checking if the method isn't available.

Returns:
AppError to wrap any reflection exceptions.

getInstance

public static java.lang.Object getInstance(java.lang.Class subclass,
                                           java.lang.Class[] parameterTypes,
                                           java.lang.Object[] initArguments,
                                           java.lang.Class superclass)
Creates an instance of the class given as input, validating it against the superclass given to make sure it is the same class or a subclass of the superclass, then trying to find a constructor according to the parameter types given.

Parameters:
subclass - The class object you wish to have a new instance of
parameterTypes - Class constructor parameter types to be used to find and call the desired constructor
initArguments - Class constructor parameters to send in when creating the new instance
superclass - The superclass or interface that you expect the subclass to extend/implement. The subclass is checked to make sure it can conform to the protocol in the superclass/interface given.
Returns:
A new instance of the subclass

getInstance

public static java.lang.Object getInstance(java.lang.String className,
                                           java.lang.Class[] parameterTypes,
                                           java.lang.Object[] initArguments,
                                           java.lang.Class superclass)
Creates an instance of the class name given as input, validating it against the superclass given to make sure it is the same class or a subclass of the superclass, then trying to find a constructor according to the parameter types given.

Parameters:
className - The string name for a class you wish to have a new instance of
parameterTypes - Class constructor parameter types to be used to find and call the desired constructor
initArguments - Class constructor parameters to send in when creating the new instance
superclass - The superclass or interface that you expect the subclass to extend/implement. The subclass is checked to make sure it can conform to the protocol in the superclass/interface given.
Returns:
A new instance of the subclass