Polishing

This commit is contained in:
Sam Brannen 2019-12-05 22:37:00 +01:00
parent c2141e2e93
commit 3b9d1a00b0
3 changed files with 46 additions and 98 deletions

View File

@ -100,8 +100,7 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
public Set<String> getAnnotationTypes() { public Set<String> getAnnotationTypes() {
Set<String> annotationTypes = this.annotationTypes; Set<String> annotationTypes = this.annotationTypes;
if (annotationTypes == null) { if (annotationTypes == null) {
annotationTypes = Collections.unmodifiableSet( annotationTypes = Collections.unmodifiableSet(AnnotationMetadata.super.getAnnotationTypes());
AnnotationMetadata.super.getAnnotationTypes());
this.annotationTypes = annotationTypes; this.annotationTypes = annotationTypes;
} }
return annotationTypes; return annotationTypes;

View File

@ -156,6 +156,4 @@ final class SimpleAnnotationMetadata implements AnnotationMetadata {
return this.annotations; return this.annotations;
} }
} }

View File

@ -38,8 +38,7 @@ public abstract class AbstractAnnotationMetadataTests {
@Test @Test
public void getClassNameReturnsClassName() { public void getClassNameReturnsClassName() {
assertThat(get(TestClass.class).getClassName()).isEqualTo( assertThat(get(TestClass.class).getClassName()).isEqualTo(TestClass.class.getName());
TestClass.class.getName());
} }
@Test @Test
@ -93,16 +92,13 @@ public abstract class AbstractAnnotationMetadataTests {
@Test @Test
public void getEnclosingClassNameWhenHasNoEnclosingClassReturnsNull() { public void getEnclosingClassNameWhenHasNoEnclosingClassReturnsNull() {
assertThat(get( assertThat(get(AbstractAnnotationMetadataTests.class).getEnclosingClassName()).isNull();
AbstractAnnotationMetadataTests.class).getEnclosingClassName()).isNull();
} }
@Test @Test
public void getSuperClassNameWhenHasSuperClassReturnsName() { public void getSuperClassNameWhenHasSuperClassReturnsName() {
assertThat(get(TestSubclass.class).getSuperClassName()).isEqualTo( assertThat(get(TestSubclass.class).getSuperClassName()).isEqualTo(TestClass.class.getName());
TestClass.class.getName()); assertThat(get(TestClass.class).getSuperClassName()).isEqualTo(Object.class.getName());
assertThat(get(TestClass.class).getSuperClassName()).isEqualTo(
Object.class.getName());
} }
@Test @Test
@ -114,11 +110,8 @@ public abstract class AbstractAnnotationMetadataTests {
@Test @Test
public void getInterfaceNamesWhenHasInterfacesReturnsNames() { public void getInterfaceNamesWhenHasInterfacesReturnsNames() {
assertThat(get(TestSubclass.class).getInterfaceNames()).containsExactlyInAnyOrder( assertThat(get(TestSubclass.class).getInterfaceNames()).containsExactlyInAnyOrder(TestInterface.class.getName());
TestInterface.class.getName()); assertThat(get(TestSubInterface.class).getInterfaceNames()).containsExactlyInAnyOrder(TestInterface.class.getName());
assertThat(get(
TestSubInterface.class).getInterfaceNames()).containsExactlyInAnyOrder(
TestInterface.class.getName());
} }
@Test @Test
@ -128,10 +121,8 @@ public abstract class AbstractAnnotationMetadataTests {
@Test @Test
public void getMemberClassNamesWhenHasMemberClassesReturnsNames() { public void getMemberClassNamesWhenHasMemberClassesReturnsNames() {
assertThat(get( assertThat(get(TestMemberClass.class).getMemberClassNames()).containsExactlyInAnyOrder(
TestMemberClass.class).getMemberClassNames()).containsExactlyInAnyOrder( TestMemberClassInnerClass.class.getName(), TestMemberClassInnerInterface.class.getName());
TestMemberClassInnerClass.class.getName(),
TestMemberClassInnerInterface.class.getName());
} }
@Test @Test
@ -141,44 +132,37 @@ public abstract class AbstractAnnotationMetadataTests {
@Test @Test
public void getAnnotationsReturnsDirectAnnotations() { public void getAnnotationsReturnsDirectAnnotations() {
AnnotationMetadata metadata = get(WithDirectAnnotations.class); assertThat(get(WithDirectAnnotations.class).getAnnotations().stream())
assertThat(metadata.getAnnotations().stream().filter( .filteredOn(MergedAnnotation::isDirectlyPresent)
MergedAnnotation::isDirectlyPresent).map( .extracting(a -> a.getType().getName())
a -> a.getType().getName())).containsExactlyInAnyOrder( .containsExactlyInAnyOrder(DirectAnnotation1.class.getName(), DirectAnnotation2.class.getName());
DirectAnnotation1.class.getName(),
DirectAnnotation2.class.getName());
} }
@Test @Test
public void isAnnotatedWhenMatchesDirectAnnotationReturnsTrue() { public void isAnnotatedWhenMatchesDirectAnnotationReturnsTrue() {
assertThat(get(WithDirectAnnotations.class).isAnnotated( assertThat(get(WithDirectAnnotations.class).isAnnotated(DirectAnnotation1.class.getName())).isTrue();
DirectAnnotation1.class.getName())).isTrue();
} }
@Test @Test
public void isAnnotatedWhenMatchesMetaAnnotationReturnsTrue() { public void isAnnotatedWhenMatchesMetaAnnotationReturnsTrue() {
assertThat(get(WithMetaAnnotations.class).isAnnotated( assertThat(get(WithMetaAnnotations.class).isAnnotated(MetaAnnotation2.class.getName())).isTrue();
MetaAnnotation2.class.getName())).isTrue();
} }
@Test @Test
public void isAnnotatedWhenDoesNotMatchDirectOrMetaAnnotationReturnsFalse() { public void isAnnotatedWhenDoesNotMatchDirectOrMetaAnnotationReturnsFalse() {
assertThat(get(TestClass.class).isAnnotated( assertThat(get(TestClass.class).isAnnotated(DirectAnnotation1.class.getName())).isFalse();
DirectAnnotation1.class.getName())).isFalse();
} }
@Test @Test
public void getAnnotationAttributesReturnsAttributes() { public void getAnnotationAttributesReturnsAttributes() {
assertThat(get(WithAnnotationAttributes.class).getAnnotationAttributes( assertThat(get(WithAnnotationAttributes.class).getAnnotationAttributes(AnnotationAttributes.class.getName()))
AnnotationAttributes.class.getName())).containsOnly(entry("name", "test"), .containsOnly(entry("name", "test"), entry("size", 1));
entry("size", 1));
} }
@Test @Test
public void getAllAnnotationAttributesReturnsAllAttributes() { public void getAllAnnotationAttributesReturnsAllAttributes() {
MultiValueMap<String, Object> attributes = get( MultiValueMap<String, Object> attributes =
WithMetaAnnotationAttributes.class).getAllAnnotationAttributes( get(WithMetaAnnotationAttributes.class).getAllAnnotationAttributes(AnnotationAttributes.class.getName());
AnnotationAttributes.class.getName());
assertThat(attributes).containsOnlyKeys("name", "size"); assertThat(attributes).containsOnlyKeys("name", "size");
assertThat(attributes.get("name")).containsExactlyInAnyOrder("m1", "m2"); assertThat(attributes.get("name")).containsExactlyInAnyOrder("m1", "m2");
assertThat(attributes.get("size")).containsExactlyInAnyOrder(1, 2); assertThat(attributes.get("size")).containsExactlyInAnyOrder(1, 2);
@ -194,155 +178,126 @@ public abstract class AbstractAnnotationMetadataTests {
@Test @Test
public void getMetaAnnotationTypesReturnsMetaAnnotations() { public void getMetaAnnotationTypesReturnsMetaAnnotations() {
AnnotationMetadata metadata = get(WithMetaAnnotations.class); AnnotationMetadata metadata = get(WithMetaAnnotations.class);
assertThat(metadata.getMetaAnnotationTypes( assertThat(metadata.getMetaAnnotationTypes(MetaAnnotationRoot.class.getName()))
MetaAnnotationRoot.class.getName())).containsExactlyInAnyOrder( .containsExactlyInAnyOrder(MetaAnnotation1.class.getName(), MetaAnnotation2.class.getName());
MetaAnnotation1.class.getName(), MetaAnnotation2.class.getName());
} }
@Test @Test
public void hasAnnotationWhenMatchesDirectAnnotationReturnsTrue() { public void hasAnnotationWhenMatchesDirectAnnotationReturnsTrue() {
assertThat(get(WithDirectAnnotations.class).hasAnnotation( assertThat(get(WithDirectAnnotations.class).hasAnnotation(DirectAnnotation1.class.getName())).isTrue();
DirectAnnotation1.class.getName())).isTrue();
} }
@Test @Test
public void hasAnnotationWhenMatchesMetaAnnotationReturnsFalse() { public void hasAnnotationWhenMatchesMetaAnnotationReturnsFalse() {
assertThat(get(WithMetaAnnotations.class).hasAnnotation( assertThat(get(WithMetaAnnotations.class).hasAnnotation(MetaAnnotation1.class.getName())).isFalse();
MetaAnnotation1.class.getName())).isFalse(); assertThat(get(WithMetaAnnotations.class).hasAnnotation(MetaAnnotation2.class.getName())).isFalse();
assertThat(get(WithMetaAnnotations.class).hasAnnotation(
MetaAnnotation2.class.getName())).isFalse();
} }
@Test @Test
public void hasAnnotationWhenDoesNotMatchDirectOrMetaAnnotationReturnsFalse() { public void hasAnnotationWhenDoesNotMatchDirectOrMetaAnnotationReturnsFalse() {
assertThat(get(TestClass.class).hasAnnotation( assertThat(get(TestClass.class).hasAnnotation(DirectAnnotation1.class.getName())).isFalse();
DirectAnnotation1.class.getName())).isFalse();
} }
@Test @Test
public void hasMetaAnnotationWhenMatchesDirectReturnsFalse() { public void hasMetaAnnotationWhenMatchesDirectReturnsFalse() {
assertThat(get(WithDirectAnnotations.class).hasMetaAnnotation( assertThat(get(WithDirectAnnotations.class).hasMetaAnnotation(DirectAnnotation1.class.getName())).isFalse();
DirectAnnotation1.class.getName())).isFalse();
} }
@Test @Test
public void hasMetaAnnotationWhenMatchesMetaAnnotationReturnsTrue() { public void hasMetaAnnotationWhenMatchesMetaAnnotationReturnsTrue() {
assertThat(get(WithMetaAnnotations.class).hasMetaAnnotation( assertThat(get(WithMetaAnnotations.class).hasMetaAnnotation(MetaAnnotation1.class.getName())).isTrue();
MetaAnnotation1.class.getName())).isTrue(); assertThat(get(WithMetaAnnotations.class).hasMetaAnnotation(MetaAnnotation2.class.getName())).isTrue();
assertThat(get(WithMetaAnnotations.class).hasMetaAnnotation(
MetaAnnotation2.class.getName())).isTrue();
} }
@Test @Test
public void hasMetaAnnotationWhenDoesNotMatchDirectOrMetaAnnotationReturnsFalse() { public void hasMetaAnnotationWhenDoesNotMatchDirectOrMetaAnnotationReturnsFalse() {
assertThat(get(TestClass.class).hasMetaAnnotation( assertThat(get(TestClass.class).hasMetaAnnotation(MetaAnnotation1.class.getName())).isFalse();
MetaAnnotation1.class.getName())).isFalse();
} }
@Test @Test
public void hasAnnotatedMethodsWhenMatchesDirectAnnotationReturnsTrue() { public void hasAnnotatedMethodsWhenMatchesDirectAnnotationReturnsTrue() {
assertThat(get(WithAnnotatedMethod.class).hasAnnotatedMethods( assertThat(get(WithAnnotatedMethod.class).hasAnnotatedMethods(DirectAnnotation1.class.getName())).isTrue();
DirectAnnotation1.class.getName())).isTrue();
} }
@Test @Test
public void hasAnnotatedMethodsWhenMatchesMetaAnnotationReturnsTrue() { public void hasAnnotatedMethodsWhenMatchesMetaAnnotationReturnsTrue() {
assertThat(get(WithMetaAnnotatedMethod.class).hasAnnotatedMethods( assertThat(get(WithMetaAnnotatedMethod.class).hasAnnotatedMethods(MetaAnnotation2.class.getName())).isTrue();
MetaAnnotation2.class.getName())).isTrue();
} }
@Test @Test
public void hasAnnotatedMethodsWhenDoesNotMatchAnyAnnotationReturnsFalse() { public void hasAnnotatedMethodsWhenDoesNotMatchAnyAnnotationReturnsFalse() {
assertThat(get(WithAnnotatedMethod.class).hasAnnotatedMethods( assertThat(get(WithAnnotatedMethod.class).hasAnnotatedMethods(MetaAnnotation2.class.getName())).isFalse();
MetaAnnotation2.class.getName())).isFalse(); assertThat(get(WithNonAnnotatedMethod.class).hasAnnotatedMethods(DirectAnnotation1.class.getName())).isFalse();
assertThat(get(WithNonAnnotatedMethod.class).hasAnnotatedMethods(
DirectAnnotation1.class.getName())).isFalse();
} }
@Test @Test
public void getAnnotatedMethodsReturnsMatchingAnnotatedAndMetaAnnotatedMethods() { public void getAnnotatedMethodsReturnsMatchingAnnotatedAndMetaAnnotatedMethods() {
assertThat(get(WithDirectAndMetaAnnotatedMethods.class).getAnnotatedMethods( assertThat(get(WithDirectAndMetaAnnotatedMethods.class).getAnnotatedMethods(MetaAnnotation2.class.getName()))
MetaAnnotation2.class.getName()).stream().map( .extracting(MethodMetadata::getMethodName)
MethodMetadata::getMethodName)).containsExactlyInAnyOrder( .containsExactlyInAnyOrder("direct", "meta");
"direct", "meta");
} }
protected abstract AnnotationMetadata get(Class<?> source); protected abstract AnnotationMetadata get(Class<?> source);
@Retention(RetentionPolicy.RUNTIME)
public static @interface DirectAnnotation1 {
@Retention(RetentionPolicy.RUNTIME)
public @interface DirectAnnotation1 {
} }
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public static @interface DirectAnnotation2 { public @interface DirectAnnotation2 {
} }
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@MetaAnnotation1 @MetaAnnotation1
public static @interface MetaAnnotationRoot { public @interface MetaAnnotationRoot {
} }
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@MetaAnnotation2 @MetaAnnotation2
public static @interface MetaAnnotation1 { public @interface MetaAnnotation1 {
} }
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public static @interface MetaAnnotation2 { public @interface MetaAnnotation2 {
} }
public static class TestClass { public static class TestClass {
} }
public static interface TestInterface { public static interface TestInterface {
} }
public static interface TestSubInterface extends TestInterface { public static interface TestSubInterface extends TestInterface {
} }
public @interface TestAnnotation { public @interface TestAnnotation {
} }
public static final class TestFinalClass { public static final class TestFinalClass {
} }
public class TestNonStaticInnerClass { public class TestNonStaticInnerClass {
} }
public static class TestSubclass extends TestClass implements TestInterface { public static class TestSubclass extends TestClass implements TestInterface {
} }
@DirectAnnotation1 @DirectAnnotation1
@DirectAnnotation2 @DirectAnnotation2
public static class WithDirectAnnotations { public static class WithDirectAnnotations {
} }
@MetaAnnotationRoot @MetaAnnotationRoot
public static class WithMetaAnnotations { public static class WithMetaAnnotations {
} }
public static class TestMemberClass { public static class TestMemberClass {
public static class TestMemberClassInnerClass { public static class TestMemberClassInnerClass {
} }
interface TestMemberClassInnerInterface { interface TestMemberClassInnerInterface {
} }
} }
@ -384,29 +339,25 @@ public abstract class AbstractAnnotationMetadataTests {
@AnnotationAttributes(name = "test", size = 1) @AnnotationAttributes(name = "test", size = 1)
public static class WithAnnotationAttributes { public static class WithAnnotationAttributes {
} }
@MetaAnnotationAttributes1 @MetaAnnotationAttributes1
@MetaAnnotationAttributes2 @MetaAnnotationAttributes2
public static class WithMetaAnnotationAttributes { public static class WithMetaAnnotationAttributes {
} }
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@AnnotationAttributes(name = "m1", size = 1) @AnnotationAttributes(name = "m1", size = 1)
public static @interface MetaAnnotationAttributes1 { public @interface MetaAnnotationAttributes1 {
} }
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@AnnotationAttributes(name = "m2", size = 2) @AnnotationAttributes(name = "m2", size = 2)
public static @interface MetaAnnotationAttributes2 { public @interface MetaAnnotationAttributes2 {
} }
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public static @interface AnnotationAttributes { public @interface AnnotationAttributes {
String name(); String name();