Skip to content

Commit 5284981

Browse files
committed
Detect deprecation on enclosing classes as well
This commit improves CodeWarnings so that it detects if an enclosing class is deprecated. Previously, it would only consider the annotated element itself and the enclosing element is important for a class as it is required to refer to it. Closes gh-33273
1 parent bcdc991 commit 5284981

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

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

+12-2
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.
@@ -59,7 +59,7 @@ public void register(String warning) {
5959
*/
6060
public CodeWarnings detectDeprecation(AnnotatedElement... elements) {
6161
for (AnnotatedElement element : elements) {
62-
register(element.getAnnotation(Deprecated.class));
62+
registerDeprecationIfNecessary(element);
6363
}
6464
return this;
6565
}
@@ -113,6 +113,16 @@ protected Set<String> getWarnings() {
113113
return Collections.unmodifiableSet(this.warnings);
114114
}
115115

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

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

+33-7
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.
@@ -97,13 +97,27 @@ void detectDeprecationOnAnnotatedElementWithDeprecated() {
9797
assertThat(this.codeWarnings.getWarnings()).containsExactly("deprecation");
9898
}
9999

100+
@Test
101+
@SuppressWarnings("deprecation")
102+
void detectDeprecationOnAnnotatedElementWhoseEnclosingElementIsDeprecated() {
103+
this.codeWarnings.detectDeprecation(DeprecatedBean.Nested.class);
104+
assertThat(this.codeWarnings.getWarnings()).containsExactly("deprecation");
105+
}
106+
100107
@Test
101108
@SuppressWarnings("removal")
102109
void detectDeprecationOnAnnotatedElementWithDeprecatedForRemoval() {
103110
this.codeWarnings.detectDeprecation(DeprecatedForRemovalBean.class);
104111
assertThat(this.codeWarnings.getWarnings()).containsExactly("removal");
105112
}
106113

114+
@Test
115+
@SuppressWarnings("removal")
116+
void detectDeprecationOnAnnotatedElementWhoseEnclosingElementIsDeprecatedForRemoval() {
117+
this.codeWarnings.detectDeprecation(DeprecatedForRemovalBean.Nested.class);
118+
assertThat(this.codeWarnings.getWarnings()).containsExactly("removal");
119+
}
120+
107121
@ParameterizedTest
108122
@MethodSource("resolvableTypesWithDeprecated")
109123
void detectDeprecationOnResolvableTypeWithDeprecated(ResolvableType resolvableType) {
@@ -113,11 +127,17 @@ void detectDeprecationOnResolvableTypeWithDeprecated(ResolvableType resolvableTy
113127

114128
@SuppressWarnings("deprecation")
115129
static Stream<Arguments> resolvableTypesWithDeprecated() {
130+
Class<?> deprecatedBean = DeprecatedBean.class;
131+
Class<?> nested = DeprecatedBean.Nested.class;
116132
return Stream.of(
117-
Arguments.of(ResolvableType.forClass(DeprecatedBean.class)),
118-
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class, DeprecatedBean.class)),
133+
Arguments.of(ResolvableType.forClass(deprecatedBean)),
134+
Arguments.of(ResolvableType.forClass(nested)),
135+
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class, deprecatedBean)),
136+
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class, nested)),
137+
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class,
138+
ResolvableType.forClassWithGenerics(GenericBean.class, deprecatedBean))),
119139
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class,
120-
ResolvableType.forClassWithGenerics(GenericBean.class, DeprecatedBean.class)))
140+
ResolvableType.forClassWithGenerics(GenericBean.class, nested)))
121141
);
122142
}
123143

@@ -130,11 +150,17 @@ void detectDeprecationOnResolvableTypeWithDeprecatedForRemoval(ResolvableType re
130150

131151
@SuppressWarnings("removal")
132152
static Stream<Arguments> resolvableTypesWithDeprecatedForRemoval() {
153+
Class<?> deprecatedBean = DeprecatedForRemovalBean.class;
154+
Class<?> nested = DeprecatedForRemovalBean.Nested.class;
133155
return Stream.of(
134-
Arguments.of(ResolvableType.forClass(DeprecatedForRemovalBean.class)),
135-
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class, DeprecatedForRemovalBean.class)),
156+
Arguments.of(ResolvableType.forClass(deprecatedBean)),
157+
Arguments.of(ResolvableType.forClass(nested)),
158+
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class, deprecatedBean)),
159+
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class, nested)),
160+
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class,
161+
ResolvableType.forClassWithGenerics(GenericBean.class, deprecatedBean))),
136162
Arguments.of(ResolvableType.forClassWithGenerics(GenericBean.class,
137-
ResolvableType.forClassWithGenerics(GenericBean.class, DeprecatedForRemovalBean.class)))
163+
ResolvableType.forClassWithGenerics(GenericBean.class, nested)))
138164
);
139165
}
140166

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

+5-1
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

+4-1
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)