Closed
Description
Overview
Due to the deprecation of SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES
(see #28079), we will introduce a way for users to provide a Predicate<Class<?>>
that is used to decide when the enclosing class for the class supplied to the predicate should be searched.
This will give the user complete control over the "enclosing classes" aspect of the search algorithm in MergedAnnotations
.
- To achieve the same behavior as
TYPE_HIERARCHY_AND_ENCLOSING_CLASSES
, a user can provideclazz -> true
as the predicate. - To limit the enclosing class search to inner classes, a user can provide
ClassUtils::isInnerClass
as the predicate. - To limit the enclosing class search to static nested classes, a user can provide
ClassUtils::isStaticClass
as the predicate. - For any other use case (such as in
TestContextAnnotationUtils
inspring-test
), the user can provide a custom predicate.
Proposal
Based on the outcome of #28208, a searchEnclosingClass
predicate could be supplied when using the TYPE_HIERARCHY
search strategy as follows.
MergedAnnotations annotations = MergedAnnotations
.search(SearchStrategy.TYPE_HIERARCHY)
.withEnclosingClasses(ClassUtils::isInnerClass)
.from(myClass);
By limiting when a searchEnclosingClass
predicate can be supplied in the fluent search API, we can prevent users from trying to supply such a predicate for other SearchStrategy
types.