|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.unitils.core.util.AnnotatedInstanceManager<T,A>
T
- Type of the object that is configured by the annotationsA
- Type of the annotation that is used for configuring the instancepublic abstract class AnnotatedInstanceManager<T,A extends Annotation>
Class for managing and creating instances of a given type. A given annotation controls how a new instance will be created.
Instances will be created if an annotation instance is found that specifies values or if a custom create method is found. Custom create methods are methods that are marked with the annotation and have one of following signatures:
' @MyAnnotation("supervalue")
' public class SuperClass {
'
' @MyAnnotation
' protected MyType createMyType(List values)
' }
'
' @MyAnnotation({"value1", "value2"})
' public class MyClass extends SuperClass {
'
'}
Following steps are performed: there is annotation with 2 values on the sub class. These values override the value of
the annotation in the superclass and will be used for creating a new instance. The 2 values are then passed to the
createMyType custom create method to create the actual instance.
If no custom create method is found, the default createInstanceForValues(java.lang.Object, java.lang.Class>, java.util.List)
method is called for creating
the instance.
Created instances are cached on the level in the hierarchy that caused the creation of the instance. That is,
if a subclass did not contain any annotations with values or any custom create methods, the super class is tried,
if an instance for that super class was already created, that instance will be returned. This way, instances are
reused as much as possible.
If an instance needs to be recreated (for example because a test made modification to it), it can be removed from
the cache by calling invalidateInstance(java.lang.Class>...)
Field Summary | |
---|---|
protected Class<A> |
annotationClass
The annotation type |
protected Class<T> |
instanceClass
The type of the managed instances |
protected Map<Class<?>,T> |
instances
All created intances per class |
Constructor Summary | |
---|---|
protected |
AnnotatedInstanceManager(Class<T> instanceClass,
Class<A> annotationClass)
Creates a manager |
Method Summary | |
---|---|
protected void |
afterInstanceCreate(T instance,
Object testObject,
Class<?> testClass)
Hook method that can be overriden to perform extra initialization after the instance was created. |
protected T |
createCustomCreatedInstance(Method customCreateMethod,
Object testObject,
Class<?> testClass,
List<String> annotationValues)
|
protected T |
createCustomCreatedInstanceFromCustomCreateMethodResult(Object testObject,
Class<?> testClass,
Object customCreateMethodResult)
|
protected abstract T |
createInstanceForValues(Object testObject,
Class<?> testClass,
List<String> values)
Creates an instance for the given values. |
protected abstract List<String> |
getAnnotationValues(A annotation)
Gets the values that are specified for the given annotation. |
protected List<String> |
getAnnotationValues(Class<?> testClass)
Gets the values of the annotations on the given class. |
protected Method |
getCustomCreateMethod(Class<?> testClass,
boolean searchSuperClasses)
Gets the custom create methods on the given class. |
protected Class<?> |
getCustomCreateMethodReturnType()
|
protected T |
getInstance(Object testObject)
Gets an instance for the given test. |
protected T |
getInstanceImpl(Object testObject,
Class<?> testClass)
Recursive implementation of getInstance(Object) . |
protected boolean |
hasInstance(Object testObject)
Checks whether getInstance(java.lang.Object) will return an instance. |
protected boolean |
hasInstanceImpl(Object testObject,
Class<?> testClass)
Recursive implementation of hasInstance(Object) . |
protected void |
invalidateInstance(Class<?>... classes)
Forces the recreation of the instance the next time that it is requested. |
protected Object |
invokeCustomCreateMethod(Method customCreateMethod,
Object testObject,
List<String> annotationValues)
Creates an instance by calling a custom create method (if there is one). |
protected boolean |
isCustomCreateMethod(Method method)
Checks whether the given method is a custom create method. |
protected void |
registerInstance(Class<?> testClass,
T instance)
Registers an instance for a given class. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected Map<Class<?>,T> instances
protected Class<T> instanceClass
protected Class<A extends Annotation> annotationClass
Constructor Detail |
---|
protected AnnotatedInstanceManager(Class<T> instanceClass, Class<A> annotationClass)
instanceClass
- The type of the managed instancesannotationClass
- The annotation typeMethod Detail |
---|
protected T getInstance(Object testObject)
createInstanceForValues(java.lang.Object, java.lang.Class>, java.util.List)
is called to create the instance.
testObject
- The test object, not null
protected void registerInstance(Class<?> testClass, T instance)
getInstance(java.lang.Object)
to return the given instance
if the testObject is of the given test type.
testClass
- The test type, not nullinstance
- The instance, not nullprotected boolean hasInstance(Object testObject)
getInstance(java.lang.Object)
will return an instance. If false is returned, getInstance(java.lang.Object)
will
return null.
testObject
- The test object, not null
protected void invalidateInstance(Class<?>... classes)
classes
- The classes for which to reset the instancesprotected boolean hasInstanceImpl(Object testObject, Class<?> testClass)
hasInstance(Object)
.
testObject
- The test object, not nulltestClass
- The level in the hierarchy
protected T getInstanceImpl(Object testObject, Class<?> testClass)
getInstance(Object)
.
testObject
- The test object, not nulltestClass
- The level in the hierarchy
protected void afterInstanceCreate(T instance, Object testObject, Class<?> testClass)
instance
- The instance, not nulltestObject
- The test object, not nulltestClass
- The level in the hierarchyprotected List<String> getAnnotationValues(Class<?> testClass)
testClass
- The test class, not null
protected Method getCustomCreateMethod(Class<?> testClass, boolean searchSuperClasses)
testClass
- The test class, not nullsearchSuperClasses
- True to look recursively in superclasses
protected boolean isCustomCreateMethod(Method method)
method
- The method, not null
protected T createCustomCreatedInstance(Method customCreateMethod, Object testObject, Class<?> testClass, List<String> annotationValues)
protected Object invokeCustomCreateMethod(Method customCreateMethod, Object testObject, List<String> annotationValues)
customCreateMethod
- The create method, not nulltestObject
- The test object, not nullannotationValues
- The specified locations if there are any, not null
protected T createCustomCreatedInstanceFromCustomCreateMethodResult(Object testObject, Class<?> testClass, Object customCreateMethodResult)
protected Class<?> getCustomCreateMethodReturnType()
protected abstract List<String> getAnnotationValues(A annotation)
annotation
- The annotation, not null
protected abstract T createInstanceForValues(Object testObject, Class<?> testClass, List<String> values)
testObject
- TODOtestClass
- TODOvalues
- The values, not null
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |