diff --git a/src/main/java/org/springframework/data/mapping/PersistentProperty.java b/src/main/java/org/springframework/data/mapping/PersistentProperty.java index 5ffd7c4d0..b3b52015e 100644 --- a/src/main/java/org/springframework/data/mapping/PersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/PersistentProperty.java @@ -15,11 +15,13 @@ */ package org.springframework.data.mapping; +import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Collection; import java.util.Map; +import org.springframework.core.annotation.AnnotationUtils; import org.springframework.data.util.TypeInformation; /** @@ -179,4 +181,22 @@ public interface PersistentProperty

> { * @return */ Class getActualType(); + + /** + * Looks up the annotation of the given type on the {@link PersistentProperty}. Will inspect accessors and the + * potentially backing field and traverse accessor methods to potentially available super types. + * + * @param annotationType the annotation to look up, must not be {@literal null}. + * @return the annotation of the given type if present or {@literal null} otherwise. + * @see AnnotationUtils#findAnnotation(Method, Class) + */ + A findAnnotation(Class annotationType); + + /** + * Returns whether the {@link PersistentProperty} has an annotation of the given type. + * + * @param annotationType the annotation to lookup, must not be {@literal null}. + * @return whether the {@link PersistentProperty} has an annotation of the given type. + */ + boolean isAnnotationPresent(Class annotationType); } 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 f5cc526f2..e2233380b 100644 --- a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java @@ -15,8 +15,6 @@ */ package org.springframework.data.mapping.model; -import static org.springframework.core.annotation.AnnotationUtils.*; - import java.beans.PropertyDescriptor; import java.lang.annotation.Annotation; import java.lang.reflect.Field; @@ -164,7 +162,7 @@ public abstract class AnnotationBasedPersistentProperty

A findAnnotation(Class annotationType) { + public A findAnnotation(Class annotationType) { Assert.notNull(annotationType, "Annotation type must not be null!"); @@ -185,7 +183,7 @@ public abstract class AnnotationBasedPersistentProperty

annotationType) { + public boolean isAnnotationPresent(Class annotationType) { return findAnnotation(annotationType) != null; } diff --git a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java index 543bba92b..ebaf53d44 100644 --- a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java @@ -22,6 +22,7 @@ import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; +import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.Collection; import java.util.Map; @@ -326,6 +327,16 @@ public class AbstractPersistentPropertyUnitTests { protected Association createAssociation() { return null; } + + @Override + public A findAnnotation(Class annotationType) { + return null; + } + + @Override + public boolean isAnnotationPresent(Class annotationType) { + return false; + } } static class Sample {