Skip to content

Commit fc28926

Browse files
committed
Merge branch '6.1.x'
2 parents 65faca8 + 5284981 commit fc28926

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/aot/CodeWarnings.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void register(String warning) {
5858
*/
5959
public CodeWarnings detectDeprecation(AnnotatedElement... elements) {
6060
for (AnnotatedElement element : elements) {
61-
register(element.getAnnotation(Deprecated.class));
61+
registerDeprecationIfNecessary(element);
6262
}
6363
return this;
6464
}
@@ -112,6 +112,16 @@ protected Set<String> getWarnings() {
112112
return Collections.unmodifiableSet(this.warnings);
113113
}
114114

115+
private void registerDeprecationIfNecessary(@Nullable AnnotatedElement element) {
116+
if (element == null) {
117+
return;
118+
}
119+
register(element.getAnnotation(Deprecated.class));
120+
if (element instanceof Class<?> type) {
121+
registerDeprecationIfNecessary(type.getEnclosingClass());
122+
}
123+
}
124+
115125
private void register(@Nullable Deprecated annotation) {
116126
if (annotation != null) {
117127
if (annotation.forRemoval()) {

spring-beans/src/test/java/org/springframework/beans/factory/aot/CodeWarningsTests.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,27 @@ void detectDeprecationOnAnnotatedElementWithDeprecated() {
9191
assertThat(this.codeWarnings.getWarnings()).containsOnly("deprecation");
9292
}
9393

94+
@Test
95+
@SuppressWarnings("deprecation")
96+
void detectDeprecationOnAnnotatedElementWhoseEnclosingElementIsDeprecated() {
97+
this.codeWarnings.detectDeprecation(DeprecatedBean.Nested.class);
98+
assertThat(this.codeWarnings.getWarnings()).containsExactly("deprecation");
99+
}
100+
94101
@Test
95102
@SuppressWarnings("removal")
96103
void detectDeprecationOnAnnotatedElementWithDeprecatedForRemoval() {
97104
this.codeWarnings.detectDeprecation(DeprecatedForRemovalBean.class);
98105
assertThat(this.codeWarnings.getWarnings()).containsOnly("removal");
99106
}
100107

108+
@Test
109+
@SuppressWarnings("removal")
110+
void detectDeprecationOnAnnotatedElementWhoseEnclosingElementIsDeprecatedForRemoval() {
111+
this.codeWarnings.detectDeprecation(DeprecatedForRemovalBean.Nested.class);
112+
assertThat(this.codeWarnings.getWarnings()).containsExactly("removal");
113+
}
114+
101115
@ParameterizedTest
102116
@MethodSource("resolvableTypesWithDeprecated")
103117
void detectDeprecationOnResolvableTypeWithDeprecated(ResolvableType resolvableType) {
@@ -107,11 +121,17 @@ void detectDeprecationOnResolvableTypeWithDeprecated(ResolvableType resolvableTy
107121

108122
@SuppressWarnings("deprecation")
109123
static Stream<Arguments> resolvableTypesWithDeprecated() {
124+
Class<?> deprecatedBean = DeprecatedBean.class;
125+
Class<?> nested = DeprecatedBean.Nested.class;
110126
return Stream.of(
111-
Arguments.of(ResolvableType.forClass(DeprecatedBean.class)),
112-
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class, DeprecatedBean.class)),
127+
Arguments.of(ResolvableType.forClass(deprecatedBean)),
128+
Arguments.of(ResolvableType.forClass(nested)),
129+
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class, deprecatedBean)),
130+
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class, nested)),
131+
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class,
132+
ResolvableType.forClassWithGenerics(GenericBean.class, deprecatedBean))),
113133
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class,
114-
ResolvableType.forClassWithGenerics(GenericBean.class, DeprecatedBean.class)))
134+
ResolvableType.forClassWithGenerics(GenericBean.class, nested)))
115135
);
116136
}
117137

@@ -124,11 +144,17 @@ void detectDeprecationOnResolvableTypeWithDeprecatedForRemoval(ResolvableType re
124144

125145
@SuppressWarnings("removal")
126146
static Stream<Arguments> resolvableTypesWithDeprecatedForRemoval() {
147+
Class<?> deprecatedBean = DeprecatedForRemovalBean.class;
148+
Class<?> nested = DeprecatedForRemovalBean.Nested.class;
127149
return Stream.of(
128-
Arguments.of(ResolvableType.forClass(DeprecatedForRemovalBean.class)),
129-
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class, DeprecatedForRemovalBean.class)),
150+
Arguments.of(ResolvableType.forClass(deprecatedBean)),
151+
Arguments.of(ResolvableType.forClass(nested)),
152+
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class, deprecatedBean)),
153+
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class, nested)),
154+
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class,
155+
ResolvableType.forClassWithGenerics(GenericBean.class, deprecatedBean))),
130156
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class,
131-
ResolvableType.forClassWithGenerics(GenericBean.class, DeprecatedForRemovalBean.class)))
157+
ResolvableType.forClassWithGenerics(GenericBean.class, nested)))
132158
);
133159
}
134160

spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/deprecation/DeprecatedBean.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,4 +23,8 @@
2323
*/
2424
@Deprecated
2525
public class DeprecatedBean {
26+
27+
// This isn't flag deprecated on purpose
28+
public static class Nested {}
29+
2630
}

spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/deprecation/DeprecatedForRemovalBean.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,4 +23,7 @@
2323
*/
2424
@Deprecated(forRemoval = true)
2525
public class DeprecatedForRemovalBean {
26+
27+
// This isn't flag deprecated on purpose
28+
public static class Nested {}
2629
}

0 commit comments

Comments
 (0)