mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 16:39:29 +00:00
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 f3b6c222f9)
This commit is contained in:
+1
-1
@@ -595,7 +595,7 @@ final class TypeMappedAnnotation<A extends Annotation> extends AbstractMergedAnn
|
||||
return clazz.getClassLoader();
|
||||
}
|
||||
if (this.source instanceof Member member) {
|
||||
member.getDeclaringClass().getClassLoader();
|
||||
return member.getDeclaringClass().getClassLoader();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
+20
@@ -20,12 +20,15 @@ import java.io.InputStream;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.OverridingClassLoader;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
@@ -109,6 +112,23 @@ class TypeMappedAnnotationTests {
|
||||
assertThat(annotation.getClass("classValue")).isEqualTo(InputStream.class);
|
||||
}
|
||||
|
||||
@Test // gh-36606
|
||||
void adaptFromStringToClassWithMemberSourceUsesMemberClassLoader() throws Exception {
|
||||
OverridingClassLoader classLoader = new OverridingClassLoader(getClass().getClassLoader()) {
|
||||
@Override
|
||||
protected boolean isEligibleForOverriding(String className) {
|
||||
return ClassAttributes.class.getName().equals(className);
|
||||
}
|
||||
};
|
||||
Class<?> sourceClass = classLoader.loadClass(ClassAttributes.class.getName());
|
||||
Method sourceMethod = sourceClass.getDeclaredMethod("classValue");
|
||||
|
||||
MergedAnnotation<?> annotation = TypeMappedAnnotation.of(null, sourceMethod,
|
||||
ClassAttributes.class, Map.of("classValue", sourceClass.getName()));
|
||||
|
||||
assertThat(annotation.getClass("classValue").getClassLoader()).isSameAs(classLoader);
|
||||
}
|
||||
|
||||
@Test
|
||||
void adaptFromStringArrayToClassArray() {
|
||||
MergedAnnotation<?> annotation = TypeMappedAnnotation.of(null, null, ClassAttributes.class,
|
||||
|
||||
Reference in New Issue
Block a user