|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.healthmarketscience.common.util.ReflectionFactory<RetType,ExType>
public class ReflectionFactory<RetType,ExType extends Throwable>
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).
| 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 |
|---|
public ReflectionFactory(Class<? extends RetType> requiredType,
Class<? extends ExType> exType,
Class<?>... constructorParamTypes)
requiredType - created classes must be of this typeexType - 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
public ReflectionFactory(Class<? extends RetType> requiredType,
Class<? extends ExType> exType,
String configureMethodName)
requiredType - created classes must be of this typeexType - 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
public ReflectionFactory(Class<? extends RetType> requiredType,
Class<? extends ExType> exType,
Class<?>[] constructorParamTypes,
String configureMethodName,
Class<?>[] configureParamTypes)
requiredType - created classes must be of this typeexType - 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
nullconfigureMethodName - 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 attemptedconfigureParamTypes - the second-stage initialization parameter
types for the instantiated classes, may be
null
public ReflectionFactory(Class<? extends RetType> requiredType,
Class<? extends ExType> exType,
String constructorMethodName,
Class<?>[] constructorParamTypes,
String configureMethodName,
Class<?>[] configureParamTypes)
requiredType - created classes must be of this typeexType - 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 usedconstructorParamTypes - 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 attemptedconfigureParamTypes - 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 |
|---|
protected Class<? extends RetType> getRequiredType()
protected Class<? extends ExType> getExceptionType()
protected String getConstructorMethodName()
protected Class<?>[] getConstructorParamTypes()
protected String getConfigureMethodName()
protected Class<?>[] getConfigureParamTypes()
protected ClassLoader getClassLoader()
protected List<String> expandType(String typeName)
throws ExType extends Throwable
ExType extends Throwable
public RetType create(String typeName,
Object... constructorParams)
throws ExType extends Throwable
ExType - due to some failure during object creation
ExType extends Throwable
public RetType create(String typeName,
Object[] constructorParams,
Object[] configureParams)
throws ExType extends Throwable
ExType - due to some failure during object creation
ExType extends Throwable
protected void throwException(String msg,
Throwable t)
throws ExType extends Throwable
<ExType>(String, Throwable) constructor. If that
construction fails, a RuntimeException is thrown.
msg - message for the new exceptiont - optional Throwable cause for the new exception
ExType - due to some failure during object creation
ExType extends Throwable
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||