Merge branch '7.0.x'

This commit is contained in:
Sam Brannen
2026-04-01 10:15:08 +02:00
2 changed files with 62 additions and 15 deletions
@@ -22,6 +22,7 @@ import java.lang.annotation.RetentionPolicy;
import org.junit.jupiter.api.Test;
import org.springframework.core.OverridingClassLoader;
import org.springframework.core.type.AbstractAnnotationMetadataTests;
import org.springframework.core.type.AnnotationMetadata;
@@ -50,21 +51,44 @@ class DefaultAnnotationMetadataTests extends AbstractAnnotationMetadataTests {
}
@Test
void getClassAttributeWhenUnknownClass() {
var annotation = get(WithClassMissingFromClasspath.class).getAnnotations().get(ClassAttributes.class);
assertThat(annotation.getStringArray("types")).contains("javax.annotation.meta.When");
assertThatIllegalArgumentException().isThrownBy(() -> annotation.getClassArray("types"));
void getClassAttributeWhenUnknownClass() throws IOException {
var classLoader = new FilteringClassLoader(getClass().getClassLoader());
var mergedAnnotation = MetadataReaderFactory.create(classLoader)
.getMetadataReader(WithClassMissingFromClasspath.class.getName())
.getAnnotationMetadata()
.getAnnotations()
.get(ClassAttributes.class);
assertThat(mergedAnnotation.getStringArray("types")).contains("javax.annotation.meta.When");
assertThatIllegalArgumentException().isThrownBy(() -> mergedAnnotation.getClassArray("types"));
}
private static class FilteringClassLoader extends OverridingClassLoader {
FilteringClassLoader(ClassLoader parent) {
super(parent);
}
@Override
protected boolean isEligibleForOverriding(String className) {
return className.startsWith("javax.annotation.");
}
@Override
protected Class<?> loadClassForOverriding(String name) throws ClassNotFoundException {
throw new ClassNotFoundException(name);
}
}
@ClassAttributes(types = {javax.annotation.meta.When.class})
@javax.annotation.Nonnull(when = javax.annotation.meta.When.MAYBE)
public static class WithClassMissingFromClasspath {
static class WithClassMissingFromClasspath {
}
@Retention(RetentionPolicy.RUNTIME)
public @interface ClassAttributes {
@interface ClassAttributes {
Class<?>[] types();
}
@@ -22,6 +22,7 @@ import java.lang.annotation.RetentionPolicy;
import org.junit.jupiter.api.Test;
import org.springframework.core.OverridingClassLoader;
import org.springframework.core.type.AbstractAnnotationMetadataTests;
import org.springframework.core.type.AnnotationMetadata;
@@ -40,9 +41,8 @@ class SimpleAnnotationMetadataTests extends AbstractAnnotationMetadataTests {
@Override
protected AnnotationMetadata get(Class<?> source) {
try {
return new SimpleMetadataReaderFactory(
source.getClassLoader()).getMetadataReader(
source.getName()).getAnnotationMetadata();
return new SimpleMetadataReaderFactory(source.getClassLoader())
.getMetadataReader(source.getName()).getAnnotationMetadata();
}
catch (IOException ex) {
throw new IllegalStateException(ex);
@@ -50,21 +50,44 @@ class SimpleAnnotationMetadataTests extends AbstractAnnotationMetadataTests {
}
@Test
void getClassAttributeWhenUnknownClass() {
var annotation = get(WithClassMissingFromClasspath.class).getAnnotations().get(ClassAttributes.class);
assertThat(annotation.getStringArray("types")).contains("javax.annotation.meta.When");
assertThatIllegalArgumentException().isThrownBy(() -> annotation.getClassArray("types"));
void getClassAttributeWhenUnknownClass() throws IOException {
var classLoader = new FilteringClassLoader(getClass().getClassLoader());
var mergedAnnotation = new SimpleMetadataReaderFactory(classLoader)
.getMetadataReader(WithClassMissingFromClasspath.class.getName())
.getAnnotationMetadata()
.getAnnotations()
.get(ClassAttributes.class);
assertThat(mergedAnnotation.getStringArray("types")).contains("javax.annotation.meta.When");
assertThatIllegalArgumentException().isThrownBy(() -> mergedAnnotation.getClassArray("types"));
}
private static class FilteringClassLoader extends OverridingClassLoader {
FilteringClassLoader(ClassLoader parent) {
super(parent);
}
@Override
protected boolean isEligibleForOverriding(String className) {
return className.startsWith("javax.annotation.");
}
@Override
protected Class<?> loadClassForOverriding(String name) throws ClassNotFoundException {
throw new ClassNotFoundException(name);
}
}
@ClassAttributes(types = {javax.annotation.meta.When.class})
@javax.annotation.Nonnull(when = javax.annotation.meta.When.MAYBE)
public static class WithClassMissingFromClasspath {
static class WithClassMissingFromClasspath {
}
@Retention(RetentionPolicy.RUNTIME)
public @interface ClassAttributes {
@interface ClassAttributes {
Class<?>[] types();
}