From fed61720b005bd7d5e56f16feb320bc259eb2690 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 13 Nov 2013 16:48:44 +0000 Subject: [PATCH] =?UTF-8?q?DATACMNS-400=20-=20PersistentProperty=20now=20e?= =?UTF-8?q?xposes=20findAnnotation(=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pulled up the method declaration from AnnotationBasedProperty as it's useful to be able to inspect a property for annotations being configured on it independently from whether the persistent mapping of it has been defined via annotations or not. Expose isAnnotationPresent(…) as well to ease quick checks for an annotation. --- .../data/mapping/PersistentProperty.java | 20 +++++++++++++++++++ .../AnnotationBasedPersistentProperty.java | 8 +++----- .../AbstractPersistentPropertyUnitTests.java | 11 ++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) 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 {