Class MergedAnnotations.Search
- Enclosing interface:
- MergedAnnotations
MergedAnnotations
model and performing a search.
- Configuration starts with an invocation of
MergedAnnotations.search(SearchStrategy)
, specifying whichMergedAnnotations.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 TypeMethodDescriptionfrom
(AnnotatedElement element) Perform a search for merged annotations beginning with the suppliedAnnotatedElement
(such as aClass
orMethod
), using the configuration in thisSearch
instance.withAnnotationFilter
(AnnotationFilter annotationFilter) Configure theAnnotationFilter
to use.withEnclosingClasses
(Predicate<Class<?>> searchEnclosingClass) Configure whether the search algorithm should search on enclosing classes.withRepeatableContainers
(RepeatableContainers repeatableContainers) Configure theRepeatableContainers
to 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
Predicate
evaluates totrue
. Typically, the predicate will be used to differentiate between inner classes andstatic
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, astatic
nested class, or a nested interface. Thus, it may find more annotations than you would expect.- Parameters:
searchEnclosingClass
- a predicate which evaluates totrue
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:
- To limit the enclosing class search to inner classes, provide
-
withRepeatableContainers
Configure theRepeatableContainers
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
Configure theAnnotationFilter
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:
-
from
Perform a search for merged annotations beginning with the suppliedAnnotatedElement
(such as aClass
orMethod
), using the configuration in thisSearch
instance.- Parameters:
element
- the source element- Returns:
- a new
MergedAnnotations
instance containing all annotations and meta-annotations from the specified element and, depending on theMergedAnnotations.SearchStrategy
, related inherited elements - See Also:
-