Skip to content

Remove TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search strategy for MergedAnnotations #28080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sbrannen opened this issue Feb 19, 2022 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@sbrannen
Copy link
Member

sbrannen commented Feb 19, 2022

Overview

Since #28207 has introduced support for providing a Predicate<Class<?>> that allows for complete control over the "enclosing classes" aspect of the search algorithm in MergedAnnotations, the deprecated TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search strategy can now be completely removed.

Related Issues

@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Feb 19, 2022
@sbrannen sbrannen added this to the 6.0.0-M3 milestone Feb 19, 2022
@sbrannen sbrannen self-assigned this Feb 19, 2022
@sbrannen sbrannen reopened this Mar 16, 2022
sbrannen added a commit that referenced this issue Mar 16, 2022
@sbrannen sbrannen changed the title Remove deprecated "enclosing classes" search strategy for MergedAnnotations Consider removing "enclosing classes" search strategy for MergedAnnotations Mar 16, 2022
@sbrannen sbrannen modified the milestones: 6.0.0-M3, 6.0.0-M4 Mar 16, 2022
sbrannen added a commit to sbrannen/spring-framework that referenced this issue Mar 16, 2022
This commit deprecates the TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search
strategy for MergedAnnotations in 6.0 M3, allowing consumers of 6.0
milestones and release candidates to provide feedback before
potentially completely removing the search strategy or providing an
alternate mechanism for achieving the same goal prior to 6.0 GA.

Closes spring-projectsgh-28079
See spring-projectsgh-28080
sbrannen added a commit to sbrannen/spring-framework that referenced this issue Mar 18, 2022
This commit is a "proof of concept" for introducing a
`Predicate<Class<?>> searchEnclosingClass` for use with the
TYPE_HIERARCHY search strategy as a replacement for the deprecated
TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search strategy.

Currently (in this commit) it is possible for a user to supply a
searchEnclosingClass predicate for any SearchStrategy; however, the
predicate is only honored if the search strategy is TYPE_HIERARCHY.
Thus, we may later choose to throw an exception is the user supplies a
predicate for a search strategy other than TYPE_HIERARCHY.

This commit also demonstrates that this new feature can be utilized in
TestContextAnnotationUtils for the implementation of hasAnnotation(...)
and findMergedAnnotation(...).

See spring-projectsgh-28080
sbrannen added a commit to sbrannen/spring-framework that referenced this issue Mar 18, 2022
This commit is a "proof of concept" for introducing a SearchAlgorithm
builder for simplifying the creation of a MergedAnnotations instance.

With this commit it is no longer possible for a user to supply a
searchEnclosingClass predicate for any SearchStrategy other than
TYPE_HIERARCHY.

See spring-projectsgh-28080
sbrannen added a commit to sbrannen/spring-framework that referenced this issue Mar 19, 2022
This commit is a "proof of concept" for introducing a fluent API for
MergedAnnotations searches.

This commit replaces the SearchAlgorithm from the previous commit by
adding a builder directly to MergedAnnotations and introducing static
`find*(...)` factory methods in MergedAnnotations such as
findDirectlyDeclaredAnnotations() and other similar methods matching
the available strategies in SearchStrategy.

A user can only supply a searchEnclosingClass predicate for
SearchStrategy.TYPE_HIERARCHY via
MergedAnnotations.findAnnotationsInTypeHierarchy(Predicate<Class<?>>).

This leads to usage such as the following in TestContextAnnotationUtils.

MergedAnnotations
	.findAnnotationsInTypeHierarchy(TestContextAnnotationUtils::searchEnclosingClass)
	.from(clazz)
	.isPresent(annotationType);

See spring-projectsgh-28080
@sbrannen sbrannen changed the title Consider removing "enclosing classes" search strategy for MergedAnnotations Remove TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search strategy for MergedAnnotations Mar 21, 2022
@sbrannen
Copy link
Member Author

Blocked until #28207 is implemented

@sbrannen sbrannen added the status: blocked An issue that's blocked on an external project change label Mar 21, 2022
sbrannen added a commit to sbrannen/spring-framework that referenced this issue Mar 22, 2022
Prior to this commit, searching for merged annotations on an
AnnotatedElement in the MergedAnnotations model was only supported via
various overloaded from(...) factory methods. In addition, it was not
possible to provide a custom AnnotationFilter without providing an
instance of RepeatableContainers.

This commit introduces a fluent API for searches in MergedAnnotations
to address these issues and improve the programming model for users of
MergedAnnotations.

To begin a search, invoke MergedAnnotations.search(SearchStrategy) with
the desired search strategy. Optional configuration can then be
provided via one of the with(...) methods. To perform a search, invoke
from(AnnotatedElement), supplying the element from which to begin the
search -- for example, a Class or a Method.

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);

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

In addition, this fluent search API paves the way for introducing
support for a predicate that controls the search on enclosing classes
(spring-projectsgh-28207) and subsequently for completely removing the
TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search strategy (spring-projectsgh-28080).

Closes spring-projectsgh-28208
@sbrannen sbrannen removed the status: blocked An issue that's blocked on an external project change label Mar 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant