Browse Source

DATACMNS-400 - PersistentProperty now exposes findAnnotation(…).

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.
pull/57/merge
Oliver Gierke 13 years ago
parent
commit
fed61720b0
  1. 20
      src/main/java/org/springframework/data/mapping/PersistentProperty.java
  2. 8
      src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java
  3. 11
      src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java

20
src/main/java/org/springframework/data/mapping/PersistentProperty.java

@ -15,11 +15,13 @@ @@ -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<P extends PersistentProperty<P>> { @@ -179,4 +181,22 @@ public interface PersistentProperty<P extends PersistentProperty<P>> {
* @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 extends Annotation> A findAnnotation(Class<A> 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<? extends Annotation> annotationType);
}

8
src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java

@ -15,8 +15,6 @@ @@ -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<P extends PersistentProp @@ -164,7 +162,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
* @return
*/
@SuppressWarnings("unchecked")
public <A extends Annotation> A findAnnotation(Class<? extends A> annotationType) {
public <A extends Annotation> A findAnnotation(Class<A> annotationType) {
Assert.notNull(annotationType, "Annotation type must not be null!");
@ -185,7 +183,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp @@ -185,7 +183,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
}
}
return cacheAndReturn(annotationType, getAnnotation(field, annotationType));
return cacheAndReturn(annotationType, AnnotationUtils.getAnnotation(field, annotationType));
}
/**
@ -209,7 +207,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp @@ -209,7 +207,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
* @param annotationType the annotation type to look up.
* @return
*/
protected boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
return findAnnotation(annotationType) != null;
}

11
src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java

@ -22,6 +22,7 @@ import java.beans.BeanInfo; @@ -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 { @@ -326,6 +327,16 @@ public class AbstractPersistentPropertyUnitTests {
protected Association<SamplePersistentProperty> createAssociation() {
return null;
}
@Override
public <A extends Annotation> A findAnnotation(Class<A> annotationType) {
return null;
}
@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
return false;
}
}
static class Sample {

Loading…
Cancel
Save