From 3b9d1a00b08f15d8852bb867b68b3a9f14e70a63 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 5 Dec 2019 22:37:00 +0100 Subject: [PATCH] Polishing --- .../core/type/StandardAnnotationMetadata.java | 3 +- .../SimpleAnnotationMetadata.java | 2 - .../type/AbstractAnnotationMetadataTests.java | 139 ++++++------------ 3 files changed, 46 insertions(+), 98 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java b/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java index b7dbbdab51..c99c72a6f6 100644 --- a/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java +++ b/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java @@ -100,8 +100,7 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements public Set getAnnotationTypes() { Set annotationTypes = this.annotationTypes; if (annotationTypes == null) { - annotationTypes = Collections.unmodifiableSet( - AnnotationMetadata.super.getAnnotationTypes()); + annotationTypes = Collections.unmodifiableSet(AnnotationMetadata.super.getAnnotationTypes()); this.annotationTypes = annotationTypes; } return annotationTypes; diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/SimpleAnnotationMetadata.java b/spring-core/src/main/java/org/springframework/core/type/classreading/SimpleAnnotationMetadata.java index 340eecb6c6..f19399df8b 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/SimpleAnnotationMetadata.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/SimpleAnnotationMetadata.java @@ -156,6 +156,4 @@ final class SimpleAnnotationMetadata implements AnnotationMetadata { return this.annotations; } - - } diff --git a/spring-core/src/test/java/org/springframework/core/type/AbstractAnnotationMetadataTests.java b/spring-core/src/test/java/org/springframework/core/type/AbstractAnnotationMetadataTests.java index 0f088c0f6c..583787b9db 100644 --- a/spring-core/src/test/java/org/springframework/core/type/AbstractAnnotationMetadataTests.java +++ b/spring-core/src/test/java/org/springframework/core/type/AbstractAnnotationMetadataTests.java @@ -38,8 +38,7 @@ public abstract class AbstractAnnotationMetadataTests { @Test public void getClassNameReturnsClassName() { - assertThat(get(TestClass.class).getClassName()).isEqualTo( - TestClass.class.getName()); + assertThat(get(TestClass.class).getClassName()).isEqualTo(TestClass.class.getName()); } @Test @@ -93,16 +92,13 @@ public abstract class AbstractAnnotationMetadataTests { @Test public void getEnclosingClassNameWhenHasNoEnclosingClassReturnsNull() { - assertThat(get( - AbstractAnnotationMetadataTests.class).getEnclosingClassName()).isNull(); + assertThat(get(AbstractAnnotationMetadataTests.class).getEnclosingClassName()).isNull(); } @Test public void getSuperClassNameWhenHasSuperClassReturnsName() { - assertThat(get(TestSubclass.class).getSuperClassName()).isEqualTo( - TestClass.class.getName()); - assertThat(get(TestClass.class).getSuperClassName()).isEqualTo( - Object.class.getName()); + assertThat(get(TestSubclass.class).getSuperClassName()).isEqualTo(TestClass.class.getName()); + assertThat(get(TestClass.class).getSuperClassName()).isEqualTo(Object.class.getName()); } @Test @@ -114,11 +110,8 @@ public abstract class AbstractAnnotationMetadataTests { @Test public void getInterfaceNamesWhenHasInterfacesReturnsNames() { - assertThat(get(TestSubclass.class).getInterfaceNames()).containsExactlyInAnyOrder( - TestInterface.class.getName()); - assertThat(get( - TestSubInterface.class).getInterfaceNames()).containsExactlyInAnyOrder( - TestInterface.class.getName()); + assertThat(get(TestSubclass.class).getInterfaceNames()).containsExactlyInAnyOrder(TestInterface.class.getName()); + assertThat(get(TestSubInterface.class).getInterfaceNames()).containsExactlyInAnyOrder(TestInterface.class.getName()); } @Test @@ -128,10 +121,8 @@ public abstract class AbstractAnnotationMetadataTests { @Test public void getMemberClassNamesWhenHasMemberClassesReturnsNames() { - assertThat(get( - TestMemberClass.class).getMemberClassNames()).containsExactlyInAnyOrder( - TestMemberClassInnerClass.class.getName(), - TestMemberClassInnerInterface.class.getName()); + assertThat(get(TestMemberClass.class).getMemberClassNames()).containsExactlyInAnyOrder( + TestMemberClassInnerClass.class.getName(), TestMemberClassInnerInterface.class.getName()); } @Test @@ -141,44 +132,37 @@ public abstract class AbstractAnnotationMetadataTests { @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()); + assertThat(get(WithDirectAnnotations.class).getAnnotations().stream()) + .filteredOn(MergedAnnotation::isDirectlyPresent) + .extracting(a -> a.getType().getName()) + .containsExactlyInAnyOrder(DirectAnnotation1.class.getName(), DirectAnnotation2.class.getName()); } @Test public void isAnnotatedWhenMatchesDirectAnnotationReturnsTrue() { - assertThat(get(WithDirectAnnotations.class).isAnnotated( - DirectAnnotation1.class.getName())).isTrue(); + assertThat(get(WithDirectAnnotations.class).isAnnotated(DirectAnnotation1.class.getName())).isTrue(); } @Test public void isAnnotatedWhenMatchesMetaAnnotationReturnsTrue() { - assertThat(get(WithMetaAnnotations.class).isAnnotated( - MetaAnnotation2.class.getName())).isTrue(); + assertThat(get(WithMetaAnnotations.class).isAnnotated(MetaAnnotation2.class.getName())).isTrue(); } @Test public void isAnnotatedWhenDoesNotMatchDirectOrMetaAnnotationReturnsFalse() { - assertThat(get(TestClass.class).isAnnotated( - DirectAnnotation1.class.getName())).isFalse(); + assertThat(get(TestClass.class).isAnnotated(DirectAnnotation1.class.getName())).isFalse(); } @Test public void getAnnotationAttributesReturnsAttributes() { - assertThat(get(WithAnnotationAttributes.class).getAnnotationAttributes( - AnnotationAttributes.class.getName())).containsOnly(entry("name", "test"), - entry("size", 1)); + assertThat(get(WithAnnotationAttributes.class).getAnnotationAttributes(AnnotationAttributes.class.getName())) + .containsOnly(entry("name", "test"), entry("size", 1)); } @Test public void getAllAnnotationAttributesReturnsAllAttributes() { - MultiValueMap attributes = get( - WithMetaAnnotationAttributes.class).getAllAnnotationAttributes( - AnnotationAttributes.class.getName()); + MultiValueMap attributes = + get(WithMetaAnnotationAttributes.class).getAllAnnotationAttributes(AnnotationAttributes.class.getName()); assertThat(attributes).containsOnlyKeys("name", "size"); assertThat(attributes.get("name")).containsExactlyInAnyOrder("m1", "m2"); assertThat(attributes.get("size")).containsExactlyInAnyOrder(1, 2); @@ -194,155 +178,126 @@ public abstract class AbstractAnnotationMetadataTests { @Test public void getMetaAnnotationTypesReturnsMetaAnnotations() { AnnotationMetadata metadata = get(WithMetaAnnotations.class); - assertThat(metadata.getMetaAnnotationTypes( - MetaAnnotationRoot.class.getName())).containsExactlyInAnyOrder( - MetaAnnotation1.class.getName(), MetaAnnotation2.class.getName()); + assertThat(metadata.getMetaAnnotationTypes(MetaAnnotationRoot.class.getName())) + .containsExactlyInAnyOrder(MetaAnnotation1.class.getName(), MetaAnnotation2.class.getName()); } @Test public void hasAnnotationWhenMatchesDirectAnnotationReturnsTrue() { - assertThat(get(WithDirectAnnotations.class).hasAnnotation( - DirectAnnotation1.class.getName())).isTrue(); + assertThat(get(WithDirectAnnotations.class).hasAnnotation(DirectAnnotation1.class.getName())).isTrue(); } @Test public void hasAnnotationWhenMatchesMetaAnnotationReturnsFalse() { - assertThat(get(WithMetaAnnotations.class).hasAnnotation( - MetaAnnotation1.class.getName())).isFalse(); - assertThat(get(WithMetaAnnotations.class).hasAnnotation( - MetaAnnotation2.class.getName())).isFalse(); + assertThat(get(WithMetaAnnotations.class).hasAnnotation(MetaAnnotation1.class.getName())).isFalse(); + assertThat(get(WithMetaAnnotations.class).hasAnnotation(MetaAnnotation2.class.getName())).isFalse(); } @Test public void hasAnnotationWhenDoesNotMatchDirectOrMetaAnnotationReturnsFalse() { - assertThat(get(TestClass.class).hasAnnotation( - DirectAnnotation1.class.getName())).isFalse(); + assertThat(get(TestClass.class).hasAnnotation(DirectAnnotation1.class.getName())).isFalse(); } @Test public void hasMetaAnnotationWhenMatchesDirectReturnsFalse() { - assertThat(get(WithDirectAnnotations.class).hasMetaAnnotation( - DirectAnnotation1.class.getName())).isFalse(); + assertThat(get(WithDirectAnnotations.class).hasMetaAnnotation(DirectAnnotation1.class.getName())).isFalse(); } @Test public void hasMetaAnnotationWhenMatchesMetaAnnotationReturnsTrue() { - assertThat(get(WithMetaAnnotations.class).hasMetaAnnotation( - MetaAnnotation1.class.getName())).isTrue(); - assertThat(get(WithMetaAnnotations.class).hasMetaAnnotation( - MetaAnnotation2.class.getName())).isTrue(); + assertThat(get(WithMetaAnnotations.class).hasMetaAnnotation(MetaAnnotation1.class.getName())).isTrue(); + assertThat(get(WithMetaAnnotations.class).hasMetaAnnotation(MetaAnnotation2.class.getName())).isTrue(); } @Test public void hasMetaAnnotationWhenDoesNotMatchDirectOrMetaAnnotationReturnsFalse() { - assertThat(get(TestClass.class).hasMetaAnnotation( - MetaAnnotation1.class.getName())).isFalse(); + assertThat(get(TestClass.class).hasMetaAnnotation(MetaAnnotation1.class.getName())).isFalse(); } @Test public void hasAnnotatedMethodsWhenMatchesDirectAnnotationReturnsTrue() { - assertThat(get(WithAnnotatedMethod.class).hasAnnotatedMethods( - DirectAnnotation1.class.getName())).isTrue(); + assertThat(get(WithAnnotatedMethod.class).hasAnnotatedMethods(DirectAnnotation1.class.getName())).isTrue(); } @Test public void hasAnnotatedMethodsWhenMatchesMetaAnnotationReturnsTrue() { - assertThat(get(WithMetaAnnotatedMethod.class).hasAnnotatedMethods( - MetaAnnotation2.class.getName())).isTrue(); + assertThat(get(WithMetaAnnotatedMethod.class).hasAnnotatedMethods(MetaAnnotation2.class.getName())).isTrue(); } @Test public void hasAnnotatedMethodsWhenDoesNotMatchAnyAnnotationReturnsFalse() { - assertThat(get(WithAnnotatedMethod.class).hasAnnotatedMethods( - MetaAnnotation2.class.getName())).isFalse(); - assertThat(get(WithNonAnnotatedMethod.class).hasAnnotatedMethods( - DirectAnnotation1.class.getName())).isFalse(); + assertThat(get(WithAnnotatedMethod.class).hasAnnotatedMethods(MetaAnnotation2.class.getName())).isFalse(); + assertThat(get(WithNonAnnotatedMethod.class).hasAnnotatedMethods(DirectAnnotation1.class.getName())).isFalse(); } @Test public void getAnnotatedMethodsReturnsMatchingAnnotatedAndMetaAnnotatedMethods() { - assertThat(get(WithDirectAndMetaAnnotatedMethods.class).getAnnotatedMethods( - MetaAnnotation2.class.getName()).stream().map( - MethodMetadata::getMethodName)).containsExactlyInAnyOrder( - "direct", "meta"); + assertThat(get(WithDirectAndMetaAnnotatedMethods.class).getAnnotatedMethods(MetaAnnotation2.class.getName())) + .extracting(MethodMetadata::getMethodName) + .containsExactlyInAnyOrder("direct", "meta"); } protected abstract AnnotationMetadata get(Class source); - @Retention(RetentionPolicy.RUNTIME) - public static @interface DirectAnnotation1 { + @Retention(RetentionPolicy.RUNTIME) + public @interface DirectAnnotation1 { } @Retention(RetentionPolicy.RUNTIME) - public static @interface DirectAnnotation2 { - + public @interface DirectAnnotation2 { } @Retention(RetentionPolicy.RUNTIME) @MetaAnnotation1 - public static @interface MetaAnnotationRoot { - + public @interface MetaAnnotationRoot { } @Retention(RetentionPolicy.RUNTIME) @MetaAnnotation2 - public static @interface MetaAnnotation1 { - + public @interface MetaAnnotation1 { } @Retention(RetentionPolicy.RUNTIME) - public static @interface MetaAnnotation2 { - + public @interface MetaAnnotation2 { } public static class TestClass { - } public static interface TestInterface { - } public static interface TestSubInterface extends TestInterface { - } public @interface TestAnnotation { - } public static final class TestFinalClass { - } public class TestNonStaticInnerClass { - } public static class TestSubclass extends TestClass implements TestInterface { - } @DirectAnnotation1 @DirectAnnotation2 public static class WithDirectAnnotations { - } @MetaAnnotationRoot public static class WithMetaAnnotations { - } public static class TestMemberClass { public static class TestMemberClassInnerClass { - } interface TestMemberClassInnerInterface { - } } @@ -384,29 +339,25 @@ public abstract class AbstractAnnotationMetadataTests { @AnnotationAttributes(name = "test", size = 1) public static class WithAnnotationAttributes { - } @MetaAnnotationAttributes1 @MetaAnnotationAttributes2 public static class WithMetaAnnotationAttributes { - } @Retention(RetentionPolicy.RUNTIME) @AnnotationAttributes(name = "m1", size = 1) - public static @interface MetaAnnotationAttributes1 { - + public @interface MetaAnnotationAttributes1 { } @Retention(RetentionPolicy.RUNTIME) @AnnotationAttributes(name = "m2", size = 2) - public static @interface MetaAnnotationAttributes2 { - + public @interface MetaAnnotationAttributes2 { } @Retention(RetentionPolicy.RUNTIME) - public static @interface AnnotationAttributes { + public @interface AnnotationAttributes { String name();