Skip to content

Commit d67c438

Browse files
committed
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.
1 parent 8bf02ac commit d67c438

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,9 @@ public boolean isWritable() {
201201
}
202202

203203
/**
204-
* Returns the annotation found for the current {@link AnnotationBasedPersistentProperty}. Will prefer field
205-
* annotations over ones found at getters or setters.
204+
* Returns the annotation found for the current {@link AnnotationBasedPersistentProperty}. Will prefer getters or
205+
* setters annotations over ones found at the backing field as the former can be used to reconfigure the metadata in
206+
* subclasses.
206207
*
207208
* @param annotationType must not be {@literal null}.
208209
* @return
@@ -229,7 +230,7 @@ public <A extends Annotation> A findAnnotation(Class<A> annotationType) {
229230
}
230231
}
231232

232-
return field == null ? null : cacheAndReturn(annotationType, AnnotationUtils.getAnnotation(field, annotationType));
233+
return cacheAndReturn(annotationType, field == null ? null : AnnotationUtils.getAnnotation(field, annotationType));
233234
}
234235

235236
/*

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

+24
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,23 @@ public void doesNotRejectNonSpringDataAnnotationsUsedOnBothFieldAndAccessor() {
203203
getProperty(TypeWithCustomAnnotationsOnBothFieldAndAccessor.class, "field");
204204
}
205205

206+
/**
207+
* @see DATACMNS-677
208+
*/
209+
@Test
210+
@SuppressWarnings("unchecked")
211+
public void cachesNonPresenceOfAnnotationOnField() {
212+
213+
SamplePersistentProperty property = getProperty(Sample.class, "getterWithoutField");
214+
215+
assertThat(property.findAnnotation(MyAnnotation.class), is(nullValue()));
216+
217+
Map<Class<?>, ?> field = (Map<Class<?>, ?>) ReflectionTestUtils.getField(property, "annotationCache");
218+
219+
assertThat(field.containsKey(MyAnnotation.class), is(true));
220+
assertThat(field.get(MyAnnotation.class), is(nullValue()));
221+
}
222+
206223
@SuppressWarnings("unchecked")
207224
private Map<Class<? extends Annotation>, Annotation> getAnnotationCache(SamplePersistentProperty property) {
208225
return (Map<Class<? extends Annotation>, Annotation>) ReflectionTestUtils.getField(property, "annotationCache");
@@ -250,6 +267,13 @@ public String getDoubleMapping() {
250267
public void setDoubleMapping(String doubleMapping) {
251268
this.doubleMapping = doubleMapping;
252269
}
270+
271+
@AccessType(Type.PROPERTY)
272+
public Object getGetterWithoutField() {
273+
return null;
274+
}
275+
276+
public void setGetterWithoutField(Object object) {}
253277
}
254278

255279
static class InvalidSample {

0 commit comments

Comments
 (0)