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> extends Object
' @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<java.lang.String>) 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<?>...)| Modifier and Type | Field and Description |
|---|---|
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
|
| Modifier | Constructor and Description |
|---|---|
protected |
AnnotatedInstanceManager(Class<T> instanceClass,
Class<A> annotationClass)
Creates a manager
|
| Modifier and Type | Method and Description |
|---|---|
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.
|
protected Class<A extends Annotation> annotationClass
protected T getInstance(Object testObject)
createInstanceForValues(java.lang.Object, java.lang.Class<?>, java.util.List<java.lang.String>) is called to create the instance.testObject - The test object, not nullprotected 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 nullprotected 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 hierarchyprotected T getInstanceImpl(Object testObject, Class<?> testClass)
getInstance(Object).testObject - The test object, not nulltestClass - The level in the hierarchyprotected 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 nullprotected Method getCustomCreateMethod(Class<?> testClass, boolean searchSuperClasses)
testClass - The test class, not nullsearchSuperClasses - True to look recursively in superclassesprotected boolean isCustomCreateMethod(Method method)
method - The method, not nullprotected 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 nullprotected T createCustomCreatedInstanceFromCustomCreateMethodResult(Object testObject, Class<?> testClass, Object customCreateMethodResult)
protected Class<?> getCustomCreateMethodReturnType()
protected abstract List<String> getAnnotationValues(A annotation)
annotation - The annotation, not nullCopyright © 2016. All Rights Reserved.