Module dev.orne.beans
Package dev.orne.beans
Class BeanAnnotationFinder<T extends Annotation,L extends Annotation>
java.lang.Object
dev.orne.beans.BeanAnnotationFinder<T,L>
- Type Parameters:
T- The supported annotation typeL- The supported annotation list type
@API(status=INTERNAL,
since="0.1")
public class BeanAnnotationFinder<T extends Annotation,L extends Annotation>
extends Object
Type level annotation finder. Detects annotations in classes, directly implemented interfaces, super classes and inherited interfaces. Supports detection of annotation list annotations.
Examples:
To search for type level annotations:
Set<?> annotations = new BeanAnnotationFinder<>(annotationType)
.find(beanType);
// Real case
Set<NotNull> annotations = new BeanAnnotationFinder<>(NotNull.class)
.find(beanType);
To search for type level annotations with annotation list support:
Set<?> annotations = new BeanAnnotationFinder<>(
annotationType,
annotationListType,
annotationListExtractor)
.find(beanType);
// Real case
Set<NotNull> annotations = new BeanAnnotationFinder<>(
NotNull.class,
NotNull.List.class,
NotNull.List::values)
.find(beanType);
Instances are reusable and thread-safe.
- Since:
- 0.1
- Version:
- 1.0, 2020-05
- Author:
- (w) Iker Hernaez
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceBeanAnnotationFinder.AnnotationListExtractor<L extends Annotation,T extends Annotation> Functional interface for extractor of individual annotations from annotation list annotations.protected static interfaceInterface for type level annotations cache.protected static final classClass level annotations cache entry.protected static classImplementation ofCachebased onWeakHashMap. -
Constructor Summary
ConstructorsConstructorDescriptionBeanAnnotationFinder(Class<T> annotationType) Creates a new instance.BeanAnnotationFinder(Class<T> annotationType, @Nullable Class<L> annotationListType, @Nullable BeanAnnotationFinder.AnnotationListExtractor<L, T> extractor) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddDirectAnnotation(Class<?> type, Set<T> annotations) Finds direct type level annotations of the supported annotation type in the specified type.protected voidaddDirectAnnotationsList(Class<?> type, Set<T> annotations) Finds direct type level annotations of the supported annotation list type in the specified type, if configured.protected voidFinds type level annotations of the supported annotation type in the interfaces implemented by the specified type, if any.protected voidFinds type level annotations of the supported annotation type in the super class of the specified type, if any.Finds the type level annotations this instance supports in the specified type.findAllAnnotations(Class<?> type) Finds type level annotations of the supported annotation type in the specified type.findAnnotations(Class<?> type, Set<Class<?>> visitedTypes) Finds type level annotations of the supported annotation type in the specified type.Returns the searched annotation list type.Returns the searched annotation type.protected BeanAnnotationFinder.CachegetCache()Returns the cache to be used by this instance.@Nullable BeanAnnotationFinder.AnnotationListExtractor<L,T> Returns the searched annotation list extractor.protected BeanAnnotationFinder<T,L> setCache(@Nullable BeanAnnotationFinder.Cache cache) Sets the type level annotations cache for this instance.
-
Constructor Details
-
BeanAnnotationFinder
Creates a new instance.- Parameters:
annotationType- The searched annotation type
-
BeanAnnotationFinder
public BeanAnnotationFinder(Class<T> annotationType, @Nullable Class<L> annotationListType, @Nullable BeanAnnotationFinder.AnnotationListExtractor<L, T> extractor) Creates a new instance. To support annotation list bothannotationListTypeandextractormust be supplied.- Parameters:
annotationType- The searched annotation typeannotationListType- The searched annotation list typeextractor- The searched annotation list extractor
-
-
Method Details
-
getAnnotationType
Returns the searched annotation type.- Returns:
- The searched annotation type
-
getAnnotationListType
Returns the searched annotation list type.- Returns:
- The searched annotation list type
-
getExtractor
Returns the searched annotation list extractor.- Returns:
- The searched annotation list extractor
-
getCache
Returns the cache to be used by this instance.- Returns:
- The cache to be used by this instance
-
setCache
Sets the type level annotations cache for this instance. If
cacheisnullshared cache will be used.- Parameters:
cache- The cache to be used by this instance- Returns:
- This instance for method chaining
-
find
Finds the type level annotations this instance supports in the specified type.- Parameters:
type- The type to search for supported annotations- Returns:
- The type level annotations of the type
-
findAnnotations
Finds type level annotations of the supported annotation type in the specified type. If the type has been already processed returns an empty set. Found annotations are cached for future uses.- Parameters:
type- The type to found annotations onvisitedTypes- The visited types record to prevent loops and repeated searches- Returns:
- The found annotations
-
findAllAnnotations
Finds type level annotations of the supported annotation type in the specified type. Finds direct annotations (detecting annotation lists if configured) and annotations in super class and implemented interfaces.- Parameters:
type- The type to found annotations on- Returns:
- The found annotations
-
addDirectAnnotation
Finds direct type level annotations of the supported annotation type in the specified type.- Parameters:
type- The type to found annotations onannotations- The set to add the found annotations on
-
addDirectAnnotationsList
Finds direct type level annotations of the supported annotation list type in the specified type, if configured. IfannotationListTypeorextractorarenullno searching is done.- Parameters:
type- The type to found annotations onannotations- The set to add the found annotations on
-
addSuperclassAnnotations
protected void addSuperclassAnnotations(Class<?> type, Set<T> annotations, Set<Class<?>> visitedTypes) Finds type level annotations of the supported annotation type in the super class of the specified type, if any.- Parameters:
type- The type to found annotations onannotations- The set to add the found annotations onvisitedTypes- The visited types record to prevent loops and repeated searches
-
addInterfacesAnnotations
protected void addInterfacesAnnotations(Class<?> type, Set<T> annotations, Set<Class<?>> visitedTypes) Finds type level annotations of the supported annotation type in the interfaces implemented by the specified type, if any.- Parameters:
type- The type to found annotations onannotations- The set to add the found annotations onvisitedTypes- The visited types record to prevent loops and repeated searches
-