Add MergedAnnotations support to meta-data classes
Add `AnnotatedTypeMetaData.getAnnotations()` that can be used to access annotation details using the `MergedAnnotations` interface. Where possible, the existing annotation methods have been migrated to call `getAnnotation()`, rather than needing their own implementation. The existing ASM based meta-data implementations have not been updated since they will be deprecated and replaced in a subsequent commit. See gh-22884
This commit is contained in:
committed by
Juergen Hoeller
parent
30ba80a3c3
commit
8c2ccfe6a3
@@ -21,6 +21,7 @@ import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.annotation.MergedAnnotation;
|
||||
import org.springframework.core.type.AbstractAnnotationMetadataTests.TestMemberClass.TestMemberClassInnerClass;
|
||||
import org.springframework.core.type.AbstractAnnotationMetadataTests.TestMemberClass.TestMemberClassInnerInterface;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
@@ -137,6 +138,16 @@ public abstract class AbstractAnnotationMetadataTests {
|
||||
assertThat(get(TestClass.class).getMemberClassNames()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAnnotationsReturnsDirectAnnotations() {
|
||||
AnnotationMetadata metadata = get(WithDirectAnnotations.class);
|
||||
assertThat(metadata.getAnnotations().stream().filter(
|
||||
MergedAnnotation::isDirectlyPresent).map(
|
||||
a -> a.getType().getName())).containsExactlyInAnyOrder(
|
||||
DirectAnnotation1.class.getName(),
|
||||
DirectAnnotation2.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAnnotatedWhenMatchesDirectAnnotationReturnsTrue() {
|
||||
assertThat(get(WithDirectAnnotations.class).isAnnotated(
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.annotation.MergedAnnotation;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
@@ -91,6 +92,16 @@ public abstract class AbstractMethodMetadataTests {
|
||||
assertThat(getTagged(WithPrivateMethod.class).isOverridable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAnnotationsReturnsDirectAnnotations() {
|
||||
MethodMetadata metadata = getTagged(WithDirectAnnotation.class);
|
||||
assertThat(metadata.getAnnotations().stream().filter(
|
||||
MergedAnnotation::isDirectlyPresent).map(
|
||||
a -> a.getType().getName())).containsExactlyInAnyOrder(
|
||||
Tag.class.getName(),
|
||||
DirectAnnotation.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAnnotatedWhenMatchesDirectAnnotationReturnsTrue() {
|
||||
assertThat(getTagged(WithDirectAnnotation.class).isAnnotated(
|
||||
|
||||
@@ -25,8 +25,11 @@ import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.type.AbstractAnnotationMetadataTests;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.core.type.classreading.AnnotationMetadataReadingVisitor;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Tests for {@link AnnotationMetadataReadingVisitor}.
|
||||
*
|
||||
@@ -58,4 +61,10 @@ public class AnnotationMetadataReadingVisitorTests
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getAnnotationsReturnsDirectAnnotations() {
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(
|
||||
super::getAnnotationsReturnsDirectAnnotations);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,13 +25,18 @@ import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.type.AbstractMethodMetadataTests;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.core.type.classreading.AnnotationMetadataReadingVisitor;
|
||||
import org.springframework.core.type.classreading.MethodMetadataReadingVisitor;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Tests for {@link MethodMetadataReadingVisitor}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class MethodMetadataReadingVisitorTests extends AbstractMethodMetadataTests {
|
||||
|
||||
@Override
|
||||
@@ -57,4 +62,10 @@ public class MethodMetadataReadingVisitorTests extends AbstractMethodMetadataTes
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getAnnotationsReturnsDirectAnnotations() {
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(
|
||||
super::getAnnotationsReturnsDirectAnnotations);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user