com.healthmarketscience.common.util
Class ReflectionFactory<RetType,ExType extends Throwable>

java.lang.Object
  extended by com.healthmarketscience.common.util.ReflectionFactory<RetType,ExType>

public class ReflectionFactory<RetType,ExType extends Throwable>
extends Object

Instead of writing custom factories for custom types, this provides a single factory implementation that uses reflection to instantiate the desired objects. The factory supports two-stage construction of the objects by defining a "configure" method name. One custom exception type will be thrown by the creation method.

Note, there are a few methods that may be overridden to further customize the factory's behavior, see getClassLoader(), expandType(java.lang.String), and throwException(java.lang.String, java.lang.Throwable).

Author:
James Ahlborn

Constructor Summary
ReflectionFactory(Class<? extends RetType> requiredType, Class<? extends ExType> exType, Class<?>... constructorParamTypes)
           
ReflectionFactory(Class<? extends RetType> requiredType, Class<? extends ExType> exType, Class<?>[] constructorParamTypes, String configureMethodName, Class<?>[] configureParamTypes)
           
ReflectionFactory(Class<? extends RetType> requiredType, Class<? extends ExType> exType, String configureMethodName)
           
ReflectionFactory(Class<? extends RetType> requiredType, Class<? extends ExType> exType, String constructorMethodName, Class<?>[] constructorParamTypes, String configureMethodName, Class<?>[] configureParamTypes)
           
 
Method Summary
 RetType create(String typeName, Object... constructorParams)
          Constructs an object of the given type with the given constructor params.
 RetType create(String typeName, Object[] constructorParams, Object[] configureParams)
          Constructs and optionally configures an object of the given type with the given constructor params and optional configuration params.
protected  List<String> expandType(String typeName)
          May be overridden to provide "custom" expansions of the given type name (default package names, etc).
protected  ClassLoader getClassLoader()
          May be overridden to choose an alternate classloader for loading classes of the instantiated objects.
protected  String getConfigureMethodName()
           
protected  Class<?>[] getConfigureParamTypes()
           
protected  String getConstructorMethodName()
           
protected  Class<?>[] getConstructorParamTypes()
           
protected  Class<? extends ExType> getExceptionType()
           
protected  Class<? extends RetType> getRequiredType()
           
protected  void throwException(String msg, Throwable t)
          Must throw an exception, either of the desired type for this factory or a RuntimeException (or Error).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionFactory

public ReflectionFactory(Class<? extends RetType> requiredType,
                         Class<? extends ExType> exType,
                         Class<?>... constructorParamTypes)
Parameters:
requiredType - created classes must be of this type
exType - the type of exception to throw on failure. must have public constructor of the form <ExType>(String, Throwable)
constructorParamTypes - the constructor parameter types for the instantiated classes, may be null

ReflectionFactory

public ReflectionFactory(Class<? extends RetType> requiredType,
                         Class<? extends ExType> exType,
                         String configureMethodName)
Parameters:
requiredType - created classes must be of this type
exType - the type of exception to throw on failure. must have public constructor of the form <ExType>(String, Throwable)
configureMethodName - iff non-null the name of a public method that must be declared on the created instance, which does second-stage initialization. otherwise, no second-stage initialization is attempted

ReflectionFactory

public ReflectionFactory(Class<? extends RetType> requiredType,
                         Class<? extends ExType> exType,
                         Class<?>[] constructorParamTypes,
                         String configureMethodName,
                         Class<?>[] configureParamTypes)
Parameters:
requiredType - created classes must be of this type
exType - the type of exception to throw on failure. must have public constructor of the form <ExType>(String, Throwable)
constructorParamTypes - the constructor parameter types for the instantiated classes, may be null
configureMethodName - iff non-null the name of a public method that must be declared on the created instance, which does second-stage initialization. otherwise, no second-stage initialization is attempted
configureParamTypes - the second-stage initialization parameter types for the instantiated classes, may be null

ReflectionFactory

public ReflectionFactory(Class<? extends RetType> requiredType,
                         Class<? extends ExType> exType,
                         String constructorMethodName,
                         Class<?>[] constructorParamTypes,
                         String configureMethodName,
                         Class<?>[] configureParamTypes)
Parameters:
requiredType - created classes must be of this type
exType - the type of exception to throw on failure. must have public constructor of the form <ExType>(String, Throwable)
constructorMethodName - iff non-null the name of a public static method that must be declared on the classes, which is used to construct the instances. otherwise, a constructor is used
constructorParamTypes - the constructor parameter types for the instantiated classes, may be null. The caller should not modify this array after passing it in.
configureMethodName - iff non-null the name of a public method that must be declared on the created instance, which does second-stage initialization. otherwise, no second-stage initialization is attempted
configureParamTypes - the second-stage initialization parameter types for the instantiated classes, may be null. The caller should not modify this array after passing it in.
Method Detail

getRequiredType

protected Class<? extends RetType> getRequiredType()

getExceptionType

protected Class<? extends ExType> getExceptionType()

getConstructorMethodName

protected String getConstructorMethodName()

getConstructorParamTypes

protected Class<?>[] getConstructorParamTypes()

getConfigureMethodName

protected String getConfigureMethodName()

getConfigureParamTypes

protected Class<?>[] getConfigureParamTypes()

getClassLoader

protected ClassLoader getClassLoader()
May be overridden to choose an alternate classloader for loading classes of the instantiated objects.

Returns:
classloader to use to load classes for reflective object instantiation

expandType

protected List<String> expandType(String typeName)
                           throws ExType extends Throwable
May be overridden to provide "custom" expansions of the given type name (default package names, etc). All output type names are expected to be fully-qualified class names. The first type name to result in an actual Class will be used for the object creation attempt. Default implementation merely returns the given string.

Returns:
a list of strings which will be tried in order as prefixes for loading classes
Throws:
ExType extends Throwable

create

public RetType create(String typeName,
                      Object... constructorParams)
               throws ExType extends Throwable
Constructs an object of the given type with the given constructor params.

Throws:
ExType - due to some failure during object creation
ExType extends Throwable

create

public RetType create(String typeName,
                      Object[] constructorParams,
                      Object[] configureParams)
               throws ExType extends Throwable
Constructs and optionally configures an object of the given type with the given constructor params and optional configuration params.

Throws:
ExType - due to some failure during object creation
ExType extends Throwable

throwException

protected void throwException(String msg,
                              Throwable t)
                       throws ExType extends Throwable
Must throw an exception, either of the desired type for this factory or a RuntimeException (or Error). Default implementation rethrows the given exception if it is already the correct type, otherwise reflectively constructs the desired exception type using the <ExType>(String, Throwable) constructor. If that construction fails, a RuntimeException is thrown.

Parameters:
msg - message for the new exception
t - optional Throwable cause for the new exception
Throws:
ExType - due to some failure during object creation
ExType extends Throwable


Copyright © 2006-2016 Health Market Science. All Rights Reserved.