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(@NotNull @NotNull Class<T> annotationType)
      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 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

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

      public Class<L> 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

      @NotNull protected @NotNull 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(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

      @NotNull public @NotNull Set<T> find(@NotNull @NotNull 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

      @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 on
      visitedTypes - The visited types record to prevent loops and repeated searches
      Returns:
      The found annotations
    • findAllAnnotations

      @NotNull protected @NotNull Set<T> findAllAnnotations(@NotNull @NotNull 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(@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 on
      annotations - 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. 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(@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 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(@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 on
      annotations - The set to add the found annotations on
      visitedTypes - The visited types record to prevent loops and repeated searches