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
Modifier and TypeClassDescriptionstatic interface
BeanAnnotationFinder.AnnotationListExtractor<L extends Annotation,
T extends Annotation> Functional interface for extractor of individual annotations from annotation list annotations.protected static interface
Interface for type level annotations cache.protected static final class
Class level annotations cache entry.protected static class
Implementation ofCache
based onWeakHashMap
. -
Constructor Summary
ConstructorDescriptionBeanAnnotationFinder
(@NotNull Class<T> annotationType) Creates a new instance.BeanAnnotationFinder
(@NotNull Class<T> annotationType, Class<L> annotationListType, BeanAnnotationFinder.AnnotationListExtractor<L, T> extractor) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addDirectAnnotation
(@NotNull Class<?> type, @NotNull Set<T> annotations) Finds direct type level annotations of the supported annotation type in the specified type.protected void
addDirectAnnotationsList
(@NotNull Class<?> type, @NotNull Set<T> annotations) Finds direct type level annotations of the supported annotation list type in the specified type, if configured.protected void
addInterfacesAnnotations
(@NotNull Class<?> type, @NotNull Set<T> annotations, @NotNull Set<Class<?>> visitedTypes) Finds type level annotations of the supported annotation type in the interfaces implemented by the specified type, if any.protected void
addSuperclassAnnotations
(@NotNull Class<?> type, @NotNull Set<T> annotations, @NotNull Set<Class<?>> visitedTypes) Finds 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
(@NotNull Class<?> type) Finds type level annotations of the supported annotation type in the specified type.findAnnotations
(@NotNull Class<?> type, @NotNull 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 @NotNull BeanAnnotationFinder.Cache
getCache()
Returns the cache to be used by this instance.Returns the searched annotation list extractor.protected BeanAnnotationFinder<T,
L> 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(@NotNull @NotNull Class<T> annotationType, Class<L> annotationListType, BeanAnnotationFinder.AnnotationListExtractor<L, T> extractor) Creates a new instance. To support annotation list bothannotationListType
andextractor
must 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
cache
isnull
shared 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
@NotNull protected @NotNull Set<T> findAnnotations(@NotNull @NotNull Class<?> type, @NotNull @NotNull Set<Class<?>> visitedTypes) 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
protected void addDirectAnnotation(@NotNull @NotNull Class<?> type, @NotNull @NotNull Set<T> annotations) 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
protected void addDirectAnnotationsList(@NotNull @NotNull Class<?> type, @NotNull @NotNull Set<T> annotations) Finds direct type level annotations of the supported annotation list type in the specified type, if configured. IfannotationListType
orextractor
arenull
no searching is done.- Parameters:
type
- The type to found annotations onannotations
- The set to add the found annotations on
-
addSuperclassAnnotations
protected void addSuperclassAnnotations(@NotNull @NotNull Class<?> type, @NotNull @NotNull Set<T> annotations, @NotNull @NotNull 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(@NotNull @NotNull Class<?> type, @NotNull @NotNull Set<T> annotations, @NotNull @NotNull 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
-