From 2da5acc55354a246e3c08fc93b8be0ccc80854ec Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 26 Jan 2017 15:00:05 +0100 Subject: [PATCH] DATACMNS-981 - Consider merged annotations when populating for annotation cache. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now consider property annotations as merged annotations when initially scanning for annotations in a property declaration. Composed annotations such as declared outside our code are captured correctly because they use an own type that is not queried by users of PersistentProperty. Own, revised annotations, using @AliasFor providing an alias for annotation values require merged annotation processing. Previously, annotations were cached as-is without resolving @AliasFor. This caused a later lookup via findAnnotation(…) to return the cached annotation without aliasing. Because the annotation was cached, no further lookup via AnnotatedElementUtils.findMergedAnnotation(…) was attempted. @Retention(RetentionPolicy.RUNTIME) @Target(value = { FIELD, METHOD }) public @interface RevisedAnnnotationWithAliasFor { @AliasFor("value") String name() default ""; @AliasFor("name") String value() default ""; } public class Person { @RevisedAnnnotationWithAliasFor(value = "spring") String firstname; } PersistentProperty firstname = … property.findAnnotation(…) returned previously @RevisedAnnnotationWithAliasFor(value = "spring", name = "") now we return @RevisedAnnnotationWithAliasFor(value = "spring", name = "spring") --- .../AnnotationBasedPersistentProperty.java | 44 ++++++++++--------- ...ationBasedPersistentPropertyUnitTests.java | 37 +++++++++++++--- 2 files changed, 53 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java index 6777ee5cd..bc5fd8ee8 100644 --- a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2016 the original author or authors. + * Copyright 2011-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,9 +42,10 @@ import org.springframework.util.Assert; /** * Special {@link PersistentProperty} that takes annotations at a property into account. - * + * * @author Oliver Gierke * @author Christoph Strobl + * @author Mark Paluch */ public abstract class AnnotationBasedPersistentProperty

> extends AbstractPersistentProperty

{ @@ -59,9 +60,8 @@ public abstract class AnnotationBasedPersistentProperty

owner, @@ -79,8 +79,8 @@ public abstract class AnnotationBasedPersistentProperty

> getAccessors() { - return Arrays.>asList(getGetter(), getSetter(), getField()).stream(); + return Arrays.> asList(getGetter(), getSetter(), getField()).stream(); } } diff --git a/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java index cb13345ec..a350f6af0 100755 --- a/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java @@ -29,6 +29,7 @@ import javax.annotation.Nullable; import org.junit.Before; import org.junit.Test; + import org.springframework.core.annotation.AliasFor; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.data.annotation.AccessType; @@ -42,7 +43,7 @@ import org.springframework.test.util.ReflectionTestUtils; /** * Unit tests for {@link AnnotationBasedPersistentProperty}. - * + * * @author Oliver Gierke * @author Christoph Strobl */ @@ -223,6 +224,17 @@ public class AnnotationBasedPersistentPropertyUnitTests

{ + assertThat(property.findAnnotation(RevisedAnnnotationWithAliasFor.class)).hasValueSatisfying(annotation -> { + assertThat(annotation.name()).isEqualTo("my-value"); + assertThat(annotation.value()).isEqualTo("my-value"); + }); + }); + } + @SuppressWarnings("unchecked") private Map, Annotation> getAnnotationCache(SamplePersistentProperty property) { return (Map, Annotation>) ReflectionTestUtils.getField(property, "annotationCache"); @@ -248,7 +260,7 @@ public class AnnotationBasedPersistentPropertyUnitTests