Class BeanAnnotationFinder<T extends Annotation,L extends Annotation>

java.lang.Object
dev.orne.beans.BeanAnnotationFinder<T,L>
Type Parameters:
T - The supported annotation type
L - 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
  • Constructor Details

    • BeanAnnotationFinder

      public BeanAnnotationFinder(Class<T> annotationType)
      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 both annotationListType and extractor must be supplied.
      Parameters:
      annotationType - The searched annotation type
      annotationListType - The searched annotation list type
      extractor - The searched annotation list extractor
  • Method Details

    • getAnnotationType

      public Class<T> getAnnotationType()
      Returns the searched annotation type.
      Returns:
      The searched annotation type
    • getAnnotationListType

      public @Nullable Class<L> getAnnotationListType()
      Returns the searched annotation list type.
      Returns:
      The searched annotation list type
    • getExtractor

      public @Nullable BeanAnnotationFinder.AnnotationListExtractor<L,T> getExtractor()
      Returns the searched annotation list extractor.
      Returns:
      The searched annotation list extractor
    • getCache

      protected BeanAnnotationFinder.Cache getCache()
      Returns the cache to be used by this instance.
      Returns:
      The cache to be used by this instance
    • setCache

      protected BeanAnnotationFinder<T,L> setCache(@Nullable BeanAnnotationFinder.Cache cache)

      Sets the type level annotations cache for this instance. If cache is null shared cache will be used.

      Parameters:
      cache - The cache to be used by this instance
      Returns:
      This instance for method chaining
    • find

      public Set<T> find(Class<?> type)
      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

      protected Set<T> findAnnotations(Class<?> type, 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 on
      visitedTypes - The visited types record to prevent loops and repeated searches
      Returns:
      The found annotations
    • findAllAnnotations

      protected Set<T> findAllAnnotations(Class<?> type)
      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(Class<?> type, Set<T> annotations)
      Finds direct type level annotations of the supported annotation type in the specified type.
      Parameters:
      type - The type to found annotations on
      annotations - The set to add the found annotations on
    • addDirectAnnotationsList

      protected void addDirectAnnotationsList(Class<?> type, Set<T> annotations)
      Finds direct type level annotations of the supported annotation list type in the specified type, if configured. If annotationListType or extractor are null no searching is done.
      Parameters:
      type - The type to found annotations on
      annotations - 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 on
      annotations - The set to add the found annotations on
      visitedTypes - 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 on
      annotations - The set to add the found annotations on
      visitedTypes - The visited types record to prevent loops and repeated searches