Package org.springframework.core.annotation

# Class MergedAnnotations.Search

java.lang.Object
org.springframework.core.annotation.MergedAnnotations.Search

Enclosing interface:
  MergedAnnotations

public static final class MergedAnnotations.Search extends Object
Fluent API for configuring the search algorithm used in the `MergedAnnotations` model and performing a search.

- Configuration starts with an invocation of `MergedAnnotations.search(SearchStrategy)`, specifying which `MergedAnnotations.SearchStrategy` to use.
- Optional configuration can be provided via one of the `with*()` methods.
- The actual search is performed by invoking `from(AnnotatedElement)` with the source element from which the search should begin.

For example, the following performs a search on `MyClass` within the entire type hierarchy of that class while ignoring repeatable annotations.

     MergedAnnotations mergedAnnotations =
         MergedAnnotations.search(SearchStrategy.TYPE_HIERARCHY)
             .withRepeatableContainers(RepeatableContainers.none())
             .from(MyClass.class);

If you wish to reuse search configuration to perform the same type of search on multiple elements, you can save the `Search` instance as demonstrated in the following example.

     Search search = MergedAnnotations.search(SearchStrategy.TYPE_HIERARCHY)
                         .withRepeatableContainers(RepeatableContainers.none());

     MergedAnnotations mergedAnnotations = search.from(MyClass.class);
     // do something with the MergedAnnotations for MyClass
     mergedAnnotations = search.from(AnotherClass.class);
     // do something with the MergedAnnotations for AnotherClass

Since:
  6.0

## Method Summary

Modifier and Type
Method
Description
`MergedAnnotations`
`from(AnnotatedElement element)`
Perform a search for merged annotations beginning with the supplied `AnnotatedElement` (such as a `Class` or `Method`), using the configuration in this `Search` instance.
`MergedAnnotations.Search`
`withAnnotationFilter(AnnotationFilter annotationFilter)`
Configure the `AnnotationFilter` to use.
`MergedAnnotations.Search`
`withEnclosingClasses(Predicate<Class<?>> searchEnclosingClass)`
Configure whether the search algorithm should search on enclosing classes.
`MergedAnnotations.Search`
`withRepeatableContainers(RepeatableContainers repeatableContainers)`
Configure the `RepeatableContainers` to use.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Method Details

### withEnclosingClasses

public MergedAnnotations.Search withEnclosingClasses(Predicate<Class<?>> searchEnclosingClass)
Configure whether the search algorithm should search on enclosing classes.

This feature is disabled by default and is only supported when using `MergedAnnotations.SearchStrategy.TYPE_HIERARCHY`.

Enclosing classes will be recursively searched if the supplied `Predicate` evaluates to `true`. Typically, the predicate will be used to differentiate between inner classes and `static` nested classes.

- To limit the enclosing class search to inner classes, provide `ClassUtils::isInnerClass` as the predicate.
- To limit the enclosing class search to static nested classes, provide `ClassUtils::isStaticClass` as the predicate.
- To force the algorithm to always search enclosing classes, provide `clazz -> true` as the predicate.
- For any other use case, provide a custom predicate.

WARNING: if the supplied predicate always evaluates to `true`, the algorithm will search recursively for annotations on an enclosing class for any source type, regardless whether the source type is an inner class, a `static` nested class, or a nested interface. Thus, it may find more annotations than you would expect.

Parameters:
  `searchEnclosingClass` - a predicate which evaluates to `true` if a search should be performed on the enclosing class of the class supplied to the predicate
Returns:
  this `Search` instance for chained method invocations
See Also:
  - `MergedAnnotations.SearchStrategy.TYPE_HIERARCHY`
  - `withRepeatableContainers(RepeatableContainers)`
  - `withAnnotationFilter(AnnotationFilter)`
  - `from(AnnotatedElement)`
  - `org.springframework.test.context.TestContextAnnotationUtils#searchEnclosingClass(Class)`

### withRepeatableContainers

public MergedAnnotations.Search withRepeatableContainers(RepeatableContainers repeatableContainers)
Configure the `RepeatableContainers` to use.

Defaults to `RepeatableContainers.standardRepeatables()`.

Parameters:
  `repeatableContainers` - the repeatable containers that may be used by annotations or meta-annotations
Returns:
  this `Search` instance for chained method invocations
See Also:
  - `withAnnotationFilter(AnnotationFilter)`
  - `from(AnnotatedElement)`

### withAnnotationFilter

public MergedAnnotations.Search withAnnotationFilter(AnnotationFilter annotationFilter)
Configure the `AnnotationFilter` to use.

Defaults to `AnnotationFilter.PLAIN`.

Parameters:
  `annotationFilter` - an annotation filter used to restrict the annotations considered
Returns:
  this `Search` instance for chained method invocations
See Also:
  - `withRepeatableContainers(RepeatableContainers)`
  - `from(AnnotatedElement)`

### from

public MergedAnnotations from(AnnotatedElement element)
Perform a search for merged annotations beginning with the supplied `AnnotatedElement` (such as a `Class` or `Method`), using the configuration in this `Search` instance.

Parameters:
  `element` - the source element
Returns:
  a new `MergedAnnotations` instance containing all annotations and meta-annotations from the specified element and, depending on the `MergedAnnotations.SearchStrategy`, related inherited elements
See Also:
  - `withEnclosingClasses(Predicate)`
  - `withRepeatableContainers(RepeatableContainers)`
  - `withAnnotationFilter(AnnotationFilter)`
  - `MergedAnnotations.from(AnnotatedElement, SearchStrategy, RepeatableContainers, AnnotationFilter)`