Overview

The TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search strategy for MergedAnnotations was originally introduced to support @Nested test classes in JUnit Jupiter.

However, while implementing #19930, we determined that the TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search strategy unfortunately could not be used since it does not allow the user to control when to recurse up the enclosing class hierarchy. For example, this search strategy will automatically search on enclosing classes for static nested classes as well as for inner classes, when the user probably only wants one such category of "enclosing class" to be searched. Consequently, TestContextAnnotationUtils was introduced in the Spring TestContext Framework to address the shortcomings of the TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search strategy.

Since this search strategy is unlikely to be useful to general users, the team should consider deprecating this search strategy in Spring Framework 6.0.

Related Issues

  • 19930

  • 23378

  • 28080

Comment From: philwebb

@sbrannen We use this strategy in Spring Boot to find @ConstructorBinding annotations from nested classes. Can we reconsider deprecating it?

Comment From: sbrannen

Hi @philwebb,

I saw that you once used it in Boot's ConfigurationPropertiesBean, but that no longer seems to be the case.

Where is TYPE_HIERARCHY_AND_ENCLOSING_CLASSES still used in Spring Boot?

Comment From: snicoll

Here is one: https://github.com/spring-projects/spring-boot/blob/de321b00b7d0f2c5c1c79a77e7241b43fbcd8313/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/context/properties/ConfigurationPropertiesReportEndpoint.java#L559

We don't on main as the semantic of @ConstructorBinding has evolved in such a way that it is no longer necessary to search it this way.

Comment From: sbrannen

Thanks for the link and explanation, @snicoll.

If the search strategy is only used for @ConstructorBinding against Framework 5.3.x and is no longer used in Boot 3.0+ (relying on Framework 6.0+), is there still an issue with having the search strategy deprecated in Framework 5.3.x and removed in 6.0?

Comment From: snicoll

I think so, yes. Our policy is to not rely on deprecated code unless absolutely necessary. Getting in this situation for the whole duration of the 2.x line is far from ideal and we'd probably copy the code to avoid using deprecated code in framework.

While I have the opportunity, I disagree with the opening statements. It may have been introduced for a very specific use case but once it becomes public API, we can't really argue that this is the only use. It sounds like an addition in TestContextAnnotationUtils is fixing the problem. It doesn't, at least for us.

Comment From: sbrannen

For the 5.3.x line, the team has decided not to deprecate the TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search strategy.

Instead, we will add notes to the documentation to increase awareness of how the search strategy behaves.

In addition, we will reconsider deprecation/removal of the search strategy in 6.0.x.

Comment From: sbrannen

Team Decision: we have decided to deprecate the TYPE_HIERARCHY_AND_ENCLOSING_CLASSES search strategy in 6.0 M3, allowing consumers of 6.0 milestones and release candidates to provide feedback before potentially completely removing it and/or providing an alternate mechanism for achieving the same goal prior to 6.0 GA.

  • see #28080 for details