Skip to content

Commit f3b6c22

Browse files
committed
Use ClassLoader for method or field in MergedAnnotation
Prior to this commit, the `return` keyword was missing in TypeMappedAnnotation's getClassLoader() implementation, which prevented the ClassLoader of the Member (Method or Field) from being used. This commit fixes that by adding the missing `return` keyword and adds a test using a custom ClassLoader to verify the correct behavior. Closes gh-36606
1 parent 8d390f4 commit f3b6c22

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

spring-core/src/main/java/org/springframework/core/annotation/TypeMappedAnnotation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ private boolean isFiltered(String attributeName) {
593593
return clazz.getClassLoader();
594594
}
595595
if (this.source instanceof Member member) {
596-
member.getDeclaringClass().getClassLoader();
596+
return member.getDeclaringClass().getClassLoader();
597597
}
598598
}
599599
return null;

spring-core/src/test/java/org/springframework/core/annotation/TypeMappedAnnotationTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.annotation.Annotation;
2121
import java.lang.annotation.Retention;
2222
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.reflect.Method;
2324
import java.util.Collections;
2425
import java.util.HashMap;
2526
import java.util.Map;
@@ -28,6 +29,7 @@
2829
import org.junit.jupiter.api.Nested;
2930
import org.junit.jupiter.api.Test;
3031

32+
import org.springframework.core.OverridingClassLoader;
3133
import org.springframework.core.annotation.MergedAnnotation.Adapt;
3234

3335
import static org.assertj.core.api.Assertions.assertThat;
@@ -124,6 +126,23 @@ void adaptFromStringToClass() {
124126
assertThat(annotation.getClass("classValue")).isEqualTo(InputStream.class);
125127
}
126128

129+
@Test // gh-36606
130+
void adaptFromStringToClassWithMemberSourceUsesMemberClassLoader() throws Exception {
131+
OverridingClassLoader classLoader = new OverridingClassLoader(getClass().getClassLoader()) {
132+
@Override
133+
protected boolean isEligibleForOverriding(String className) {
134+
return ClassAttributes.class.getName().equals(className);
135+
}
136+
};
137+
Class<?> sourceClass = classLoader.loadClass(ClassAttributes.class.getName());
138+
Method sourceMethod = sourceClass.getDeclaredMethod("classValue");
139+
140+
MergedAnnotation<?> annotation = TypeMappedAnnotation.of(null, sourceMethod,
141+
ClassAttributes.class, Map.of("classValue", sourceClass.getName()));
142+
143+
assertThat(annotation.getClass("classValue").getClassLoader()).isSameAs(classLoader);
144+
}
145+
127146
@Test
128147
void adaptFromStringArrayToClassArray() {
129148
MergedAnnotation<?> annotation = TypeMappedAnnotation.of(null, null, ClassAttributes.class,

0 commit comments

Comments
 (0)