Skip to content

Commit b6a2469

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 (cherry picked from commit f3b6c22)
1 parent 2a9afd6 commit b6a2469

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-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
@@ -595,7 +595,7 @@ private ClassLoader getClassLoader() {
595595
return clazz.getClassLoader();
596596
}
597597
if (this.source instanceof Member member) {
598-
member.getDeclaringClass().getClassLoader();
598+
return member.getDeclaringClass().getClassLoader();
599599
}
600600
}
601601
return null;

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
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;
2627

2728
import org.junit.jupiter.api.Test;
2829

30+
import org.springframework.core.OverridingClassLoader;
31+
2932
import static org.assertj.core.api.Assertions.assertThat;
3033

3134
/**
@@ -109,6 +112,23 @@ void adaptFromStringToClass() {
109112
assertThat(annotation.getClass("classValue")).isEqualTo(InputStream.class);
110113
}
111114

115+
@Test // gh-36606
116+
void adaptFromStringToClassWithMemberSourceUsesMemberClassLoader() throws Exception {
117+
OverridingClassLoader classLoader = new OverridingClassLoader(getClass().getClassLoader()) {
118+
@Override
119+
protected boolean isEligibleForOverriding(String className) {
120+
return ClassAttributes.class.getName().equals(className);
121+
}
122+
};
123+
Class<?> sourceClass = classLoader.loadClass(ClassAttributes.class.getName());
124+
Method sourceMethod = sourceClass.getDeclaredMethod("classValue");
125+
126+
MergedAnnotation<?> annotation = TypeMappedAnnotation.of(null, sourceMethod,
127+
ClassAttributes.class, Map.of("classValue", sourceClass.getName()));
128+
129+
assertThat(annotation.getClass("classValue").getClassLoader()).isSameAs(classLoader);
130+
}
131+
112132
@Test
113133
void adaptFromStringArrayToClassArray() {
114134
MergedAnnotation<?> annotation = TypeMappedAnnotation.of(null, null, ClassAttributes.class,

0 commit comments

Comments
 (0)