From fc8f31ccfbecd2179d7ce216a5a3521f13397c05 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 16 Mar 2022 19:32:52 +0100 Subject: [PATCH] Deprecate "enclosing classes" search strategy for MergedAnnotations 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 gh-28079 See gh-28080 --- .../springframework/core/annotation/AnnotationsScanner.java | 5 ++++- .../springframework/core/annotation/MergedAnnotations.java | 3 +++ .../core/annotation/AnnotationsScannerTests.java | 5 ++++- .../core/annotation/MergedAnnotationsTests.java | 3 +++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationsScanner.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationsScanner.java index b044cb5a0c..fa90a703b4 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationsScanner.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationsScanner.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -92,6 +92,7 @@ abstract class AnnotationsScanner { } @Nullable + @SuppressWarnings("deprecation") private static R processClass(C context, Class source, SearchStrategy searchStrategy, AnnotationsProcessor processor) { @@ -229,6 +230,7 @@ abstract class AnnotationsScanner { } @Nullable + @SuppressWarnings("deprecation") private static R processMethod(C context, Method source, SearchStrategy searchStrategy, AnnotationsProcessor processor) { @@ -499,6 +501,7 @@ abstract class AnnotationsScanner { return (type.getName().startsWith("java.") || type == Ordered.class); } + @SuppressWarnings("deprecation") private static boolean isWithoutHierarchy(AnnotatedElement source, SearchStrategy searchStrategy) { if (source == Object.class) { return true; diff --git a/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotations.java b/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotations.java index 81822f690f..9800505d77 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotations.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotations.java @@ -492,7 +492,10 @@ public interface MergedAnnotations extends Iterable * whether the source type is an inner class, a {@code static} * nested class, or a nested interface. Thus, it may find more annotations * than you would expect. + * @deprecated as of Spring Framework 6.0 M3, for potential removal or + * replacement before 6.0 GA */ + @Deprecated TYPE_HIERARCHY_AND_ENCLOSING_CLASSES } diff --git a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationsScannerTests.java b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationsScannerTests.java index 83518d9b38..e848090b12 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationsScannerTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationsScannerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -421,6 +421,7 @@ class AnnotationsScannerTests { } @Test + @SuppressWarnings("deprecation") void typeHierarchyWithEnclosedStrategyOnEnclosedStaticClassScansAnnotations() { Class source = AnnotationEnclosingClassSample.EnclosedStatic.EnclosedStaticStatic.class; assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES)) @@ -428,6 +429,7 @@ class AnnotationsScannerTests { } @Test + @SuppressWarnings("deprecation") void typeHierarchyWithEnclosedStrategyOnEnclosedInnerClassScansAnnotations() { Class source = AnnotationEnclosingClassSample.EnclosedInner.EnclosedInnerInner.class; assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES)) @@ -435,6 +437,7 @@ class AnnotationsScannerTests { } @Test + @SuppressWarnings("deprecation") void typeHierarchyWithEnclosedStrategyOnMethodHierarchyUsesTypeHierarchyScan() { Method source = methodFrom(WithHierarchy.class); assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES)).containsExactly( diff --git a/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java b/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java index a64233d485..03666ff884 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java @@ -711,6 +711,7 @@ class MergedAnnotationsTests { } @Test + @SuppressWarnings("deprecation") void streamTypeHierarchyAndEnclosingClassesFromNonAnnotatedInnerClassWithAnnotatedEnclosingClass() { Stream> classes = MergedAnnotations.from(AnnotatedClass.NonAnnotatedInnerClass.class, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES).stream().map(MergedAnnotation::getType); @@ -718,6 +719,7 @@ class MergedAnnotationsTests { } @Test + @SuppressWarnings("deprecation") void streamTypeHierarchyAndEnclosingClassesFromNonAnnotatedStaticNestedClassWithAnnotatedEnclosingClass() { Stream> classes = MergedAnnotations.from(AnnotatedClass.NonAnnotatedStaticNestedClass.class, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES).stream().map(MergedAnnotation::getType); @@ -725,6 +727,7 @@ class MergedAnnotationsTests { } @Test + @SuppressWarnings("deprecation") void getFromMethodWithMethodAnnotationOnLeaf() throws Exception { Method method = Leaf.class.getMethod("annotatedOnLeaf"); assertThat(method.getAnnotation(Order.class)).isNotNull();