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:
Sam Brannen
2026-04-07 18:24:04 +02:00
parent 2a9afd6ede
commit b6a246989f
2 changed files with 21 additions and 1 deletions
@@ -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,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,