T
- The type of the annotationE
- The type of the componentpublic class ComponentFactory<T extends java.lang.annotation.Annotation,E>
extends java.lang.Object
Given the example below:
@ValidatorClass(NotNullValidator.class) public @interface NotNull { }
An instance of a NotNullValidator
can be created using something
annotated with the NotNull
annotation just by passing the
annotated element to this factory.
Constructor and Description |
---|
ComponentFactory(java.lang.Class<T> annotationType)
Creates a new factory that searches for the implementation by looking at
the "value" property of the annotation.
|
ComponentFactory(java.lang.Class<T> annotationType,
java.lang.String classElement)
Creates a new factory that searches for the implementation by looking at
the given property name of the annotation.
|
Modifier and Type | Method and Description |
---|---|
E |
create(java.lang.reflect.AnnotatedElement element)
Creates a component using the first annotation that maps to a component.
|
E |
create(java.lang.annotation.Annotation annotation)
Tries to create the component based on the given annotation.
|
java.util.List<E> |
createAll(java.lang.reflect.AnnotatedElement element)
Creates a list of components based on the annotations of the given element.
|
static java.util.function.BiConsumer<Context,java.lang.annotation.Annotation> |
defaults()
Returns the default consumer to configure the context for creating
components.
|
ComponentFactory |
toConfigure(java.util.function.BiConsumer<Context,java.lang.annotation.Annotation> consumer)
Changes the way the context is configured by replacing the consumer.
|
ComponentFactory |
toCreate(java.util.function.BiFunction<java.lang.reflect.Constructor,java.lang.Object[],java.lang.Object> createFunction)
Sets the function used to create the objects.
|
public ComponentFactory(java.lang.Class<T> annotationType)
annotationType
- the type of the annotation to searchpublic ComponentFactory(java.lang.Class<T> annotationType, java.lang.String classElement)
annotationType
- the type of the annotation to searchclassElement
- the property name that contains the implementationpublic ComponentFactory toCreate(java.util.function.BiFunction<java.lang.reflect.Constructor,java.lang.Object[],java.lang.Object> createFunction)
createFunction
- the function to usepublic ComponentFactory toConfigure(java.util.function.BiConsumer<Context,java.lang.annotation.Annotation> consumer)
You can add behaviour by composing the default consumer:
factory.toConfigure( defaults().andThen( (context, annotation) -> yourConfigurations ) );
consumer
- the consumer to use for configuring the context to create
the components.defaults()
public E create(java.lang.annotation.Annotation annotation)
If the annotation type T
is present in the given annotation,
then the implementation defined will be instantiated.
annotation
- the annotation to check if the component can be created.null
if the annotation is not
annotated with the required annotation.public E create(java.lang.reflect.AnnotatedElement element)
Use this method if only one component is allowed by target.
element
- the element to search for annotationsnull
if no annotations in the
element could create a component.public java.util.List<E> createAll(java.lang.reflect.AnnotatedElement element)
element
- the element to search for annotationscreate(java.lang.annotation.Annotation)
public static java.util.function.BiConsumer<Context,java.lang.annotation.Annotation> defaults()
The context for creating the component is composed by the elements of the annotation and the annotation itself. For example:
@ComponentClass(MyComponent.class) public @interface MyAnnotation { boolean check(); String name(); } @MyAnnotation(check = false, name = "someName") private String someField;
A context to create the component will have:
false
mapped to a boolean parameter named
"check"someName
mapped to a String parameter named
"name"MyAnnotation
itself mapped to a parameter
of the same type