Browse Source

DATACMNS-677 - AnnotationBasedPersistentProperty now caches absence of annotations on accessor-only properties.

AnnotationBasedPersistentProperty now also caches the absence of properties that are expressed through accessors only. Previously the absence of a field caused us to skip the registration of the absence in the cache.
pull/121/head
Oliver Gierke 11 years ago
parent
commit
b0211911b5
  1. 7
      src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java
  2. 24
      src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java

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

@ -201,8 +201,9 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
} }
/** /**
* Returns the annotation found for the current {@link AnnotationBasedPersistentProperty}. Will prefer field * Returns the annotation found for the current {@link AnnotationBasedPersistentProperty}. Will prefer getters or
* annotations over ones found at getters or setters. * setters annotations over ones found at the backing field as the former can be used to reconfigure the metadata in
* subclasses.
* *
* @param annotationType must not be {@literal null}. * @param annotationType must not be {@literal null}.
* @return * @return
@ -229,7 +230,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
} }
} }
return field == null ? null : cacheAndReturn(annotationType, AnnotationUtils.getAnnotation(field, annotationType)); return cacheAndReturn(annotationType, field == null ? null : AnnotationUtils.getAnnotation(field, annotationType));
} }
/* /*

24
src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java

@ -203,6 +203,23 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
getProperty(TypeWithCustomAnnotationsOnBothFieldAndAccessor.class, "field"); getProperty(TypeWithCustomAnnotationsOnBothFieldAndAccessor.class, "field");
} }
/**
* @see DATACMNS-677
*/
@Test
@SuppressWarnings("unchecked")
public void cachesNonPresenceOfAnnotationOnField() {
SamplePersistentProperty property = getProperty(Sample.class, "getterWithoutField");
assertThat(property.findAnnotation(MyAnnotation.class), is(nullValue()));
Map<Class<?>, ?> field = (Map<Class<?>, ?>) ReflectionTestUtils.getField(property, "annotationCache");
assertThat(field.containsKey(MyAnnotation.class), is(true));
assertThat(field.get(MyAnnotation.class), is(nullValue()));
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Map<Class<? extends Annotation>, Annotation> getAnnotationCache(SamplePersistentProperty property) { private Map<Class<? extends Annotation>, Annotation> getAnnotationCache(SamplePersistentProperty property) {
return (Map<Class<? extends Annotation>, Annotation>) ReflectionTestUtils.getField(property, "annotationCache"); return (Map<Class<? extends Annotation>, Annotation>) ReflectionTestUtils.getField(property, "annotationCache");
@ -250,6 +267,13 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
public void setDoubleMapping(String doubleMapping) { public void setDoubleMapping(String doubleMapping) {
this.doubleMapping = doubleMapping; this.doubleMapping = doubleMapping;
} }
@AccessType(Type.PROPERTY)
public Object getGetterWithoutField() {
return null;
}
public void setGetterWithoutField(Object object) {}
} }
static class InvalidSample { static class InvalidSample {

Loading…
Cancel
Save