Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0e3e34b

Browse files
committed
Find annotations on parameters in overridden non-public methods
Prior to this commit, annotations were not found on parameters in anoverridden method unless the method was public. Specifically, thesearch algorithm in AnnotatedMethod did not consider a protected orpackage-private method in a superclass to be a potential overridecandidate. This affects parameter annotation searches inspring-messaging, spring-webmvc, spring-webflux, and any othercomponents that use or extend AnnotatedMethod.To address that, this commit revises the search algorithm inAnnotatedMethod to consider all non-final declared methods as potentialoverride candidates, thereby aligning with the search logic inAnnotationsScanner for the MergedAnnotations API.Closesgh-35349
1 parent4745c7c commit0e3e34b

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

‎spring-core/src/main/java/org/springframework/core/annotation/AnnotatedMethod.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
importjava.lang.annotation.Annotation;
2020
importjava.lang.reflect.Method;
21+
importjava.lang.reflect.Modifier;
2122
importjava.util.ArrayList;
2223
importjava.util.Arrays;
2324
importjava.util.List;
@@ -38,6 +39,7 @@
3839
* interface-declared parameter annotations from the concrete target method.
3940
*
4041
* @author Juergen Hoeller
42+
* @author Sam Brannen
4143
* @since 6.1
4244
* @see #getMethodAnnotation(Class)
4345
* @see #getMethodParameters()
@@ -181,7 +183,7 @@ private List<Annotation[][]> getInheritedParameterAnnotations() {
181183
clazz =null;
182184
}
183185
if (clazz !=null) {
184-
for (Methodcandidate :clazz.getMethods()) {
186+
for (Methodcandidate :clazz.getDeclaredMethods()) {
185187
if (isOverrideFor(candidate)) {
186188
parameterAnnotations.add(candidate.getParameterAnnotations());
187189
}
@@ -194,8 +196,9 @@ private List<Annotation[][]> getInheritedParameterAnnotations() {
194196
}
195197

196198
privatebooleanisOverrideFor(Methodcandidate) {
197-
if (!candidate.getName().equals(this.method.getName()) ||
198-
candidate.getParameterCount() !=this.method.getParameterCount()) {
199+
if (Modifier.isPrivate(candidate.getModifiers()) ||
200+
!candidate.getName().equals(this.method.getName()) ||
201+
(candidate.getParameterCount() !=this.method.getParameterCount())) {
199202
returnfalse;
200203
}
201204
Class<?>[]paramTypes =this.method.getParameterTypes();

‎spring-core/src/test/java/org/springframework/core/annotation/AnnotatedMethodTests.java‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
importjava.lang.annotation.Retention;
2020
importjava.lang.annotation.RetentionPolicy;
2121
importjava.lang.reflect.Method;
22+
importjava.lang.reflect.Modifier;
2223

2324
importorg.junit.jupiter.api.Test;
2425

2526
importorg.springframework.core.MethodParameter;
26-
importorg.springframework.util.ClassUtils;
27+
importorg.springframework.util.ReflectionUtils;
2728

29+
importstaticjava.util.Arrays.stream;
30+
importstaticjava.util.stream.Collectors.joining;
2831
importstaticorg.assertj.core.api.Assertions.assertThat;
2932

3033
/**
@@ -55,6 +58,12 @@ void shouldFindAnnotationOnMethodInGenericInterface() {
5558

5659
@Test
5760
voidshouldFindAnnotationOnMethodParameterInGenericAbstractSuperclass() {
61+
// Prerequisites for gh-35349
62+
MethodabstractMethod =ReflectionUtils.findMethod(GenericAbstractSuperclass.class,"processTwo",Object.class);
63+
assertThat(abstractMethod).isNotNull();
64+
assertThat(Modifier.isAbstract(abstractMethod.getModifiers())).as("abstract").isTrue();
65+
assertThat(Modifier.isPublic(abstractMethod.getModifiers())).as("public").isFalse();
66+
5867
MethodprocessTwo =getMethod("processTwo",String.class);
5968

6069
AnnotatedMethodannotatedMethod =newAnnotatedMethod(processTwo);
@@ -78,7 +87,14 @@ void shouldFindAnnotationOnMethodParameterInGenericInterface() {
7887

7988

8089
privatestaticMethodgetMethod(Stringname,Class<?>...parameterTypes) {
81-
returnClassUtils.getMethod(GenericInterfaceImpl.class,name,parameterTypes);
90+
Class<?>clazz =GenericInterfaceImpl.class;
91+
Methodmethod =ReflectionUtils.findMethod(clazz,name,parameterTypes);
92+
if (method ==null) {
93+
StringparameterNames =stream(parameterTypes).map(Class::getName).collect(joining(", "));
94+
thrownewIllegalStateException("Expected method not found: %s#%s(%s)"
95+
.formatted(clazz.getSimpleName(),name,parameterNames));
96+
}
97+
returnmethod;
8298
}
8399

84100

@@ -103,13 +119,14 @@ public void processOneAndTwo(Long value1, C value2) {
103119
}
104120

105121
@Handler
106-
publicabstractvoidprocessTwo(@ParamCvalue);
122+
// Intentionally NOT public
123+
abstractvoidprocessTwo(@ParamCvalue);
107124
}
108125

109126
staticclassGenericInterfaceImplextendsGenericAbstractSuperclass<String> {
110127

111128
@Override
112-
publicvoidprocessTwo(Stringvalue) {
129+
voidprocessTwo(Stringvalue) {
113130
}
114131
}
115132

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp