Class MergedAnnotations.Search
- Enclosing interface:
- MergedAnnotations
MergedAnnotations model and performing a search.
- Configuration starts with an invocation of
MergedAnnotations.search(SearchStrategy), specifying whichMergedAnnotations.SearchStrategyto 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 TypeMethodDescriptionfrom(AnnotatedElement element) Perform a search for merged annotations beginning with the suppliedAnnotatedElement(such as aClassorMethod), using the configuration in thisSearchinstance.withAnnotationFilter(AnnotationFilter annotationFilter) Configure theAnnotationFilterto use.withEnclosingClasses(Predicate<Class<?>> searchEnclosingClass) Configure whether the search algorithm should search on enclosing classes.withRepeatableContainers(RepeatableContainers repeatableContainers) Configure theRepeatableContainersto use.
-
Method Details
-
withEnclosingClasses
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
Predicateevaluates totrue. Typically, the predicate will be used to differentiate between inner classes andstaticnested classes.- To limit the enclosing class search to inner classes, provide
ClassUtils::isInnerClassas the predicate. - To limit the enclosing class search to static nested classes, provide
ClassUtils::isStaticClassas the predicate. - To force the algorithm to always search enclosing classes, provide
clazz -> trueas 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, astaticnested class, or a nested interface. Thus, it may find more annotations than you would expect.- Parameters:
searchEnclosingClass- a predicate which evaluates totrueif a search should be performed on the enclosing class of the class supplied to the predicate- Returns:
- this
Searchinstance for chained method invocations - See Also:
- To limit the enclosing class search to inner classes, provide
-
withRepeatableContainers
Configure theRepeatableContainersto use.Defaults to
RepeatableContainers.standardRepeatables().- Parameters:
repeatableContainers- the repeatable containers that may be used by annotations or meta-annotations- Returns:
- this
Searchinstance for chained method invocations - See Also:
-
withAnnotationFilter
Configure theAnnotationFilterto use.Defaults to
AnnotationFilter.PLAIN.- Parameters:
annotationFilter- an annotation filter used to restrict the annotations considered- Returns:
- this
Searchinstance for chained method invocations - See Also:
-
from
Perform a search for merged annotations beginning with the suppliedAnnotatedElement(such as aClassorMethod), using the configuration in thisSearchinstance.- Parameters:
element- the source element- Returns:
- a new
MergedAnnotationsinstance containing all annotations and meta-annotations from the specified element and, depending on theMergedAnnotations.SearchStrategy, related inherited elements - See Also:
-