Skip to content

Commit 8bb937a

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 adb7284 commit 8bb937a

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
@@ -190,8 +190,9 @@ public boolean isAssociation() {
190190
}
191191

192192
/**
193-
* Returns the annotation found for the current {@link AnnotationBasedPersistentProperty}. Will prefer field
194-
* annotations over ones found at getters or setters.
193+
* Returns the annotation found for the current {@link AnnotationBasedPersistentProperty}. Will prefer getters or
194+
* setters annotations over ones found at the backing field as the former can be used to reconfigure the metadata in
195+
* subclasses.
195196
*
196197
* @param annotationType must not be {@literal null}.
197198
* @return
@@ -218,7 +219,7 @@ public <A extends Annotation> A findAnnotation(Class<A> annotationType) {
218219
}
219220
}
220221

221-
return field == null ? null : cacheAndReturn(annotationType, AnnotationUtils.getAnnotation(field, annotationType));
222+
return cacheAndReturn(annotationType, field == null ? null : AnnotationUtils.getAnnotation(field, annotationType));
222223
}
223224

224225
/*

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

+24
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,23 @@ public void doesNotRejectNonSpringDataAnnotationsUsedOnBothFieldAndAccessor() {
168168
getProperty(TypeWithCustomAnnotationsOnBothFieldAndAccessor.class, "field");
169169
}
170170

171+
/**
172+
* @see DATACMNS-677
173+
*/
174+
@Test
175+
@SuppressWarnings("unchecked")
176+
public void cachesNonPresenceOfAnnotationOnField() {
177+
178+
SamplePersistentProperty property = getProperty(Sample.class, "getterWithoutField");
179+
180+
assertThat(property.findAnnotation(MyAnnotation.class), is(nullValue()));
181+
182+
Map<Class<?>, ?> field = (Map<Class<?>, ?>) ReflectionTestUtils.getField(property, "annotationCache");
183+
184+
assertThat(field.containsKey(MyAnnotation.class), is(true));
185+
assertThat(field.get(MyAnnotation.class), is(nullValue()));
186+
}
187+
171188
@SuppressWarnings("unchecked")
172189
private Map<Class<? extends Annotation>, Annotation> getAnnotationCache(SamplePersistentProperty property) {
173190
return (Map<Class<? extends Annotation>, Annotation>) ReflectionTestUtils.getField(property, "annotationCache");
@@ -215,6 +232,13 @@ public String getDoubleMapping() {
215232
public void setDoubleMapping(String doubleMapping) {
216233
this.doubleMapping = doubleMapping;
217234
}
235+
236+
@AccessType(Type.PROPERTY)
237+
public Object getGetterWithoutField() {
238+
return null;
239+
}
240+
241+
public void setGetterWithoutField(Object object) {}
218242
}
219243

220244
static class InvalidSample {

0 commit comments

Comments
 (0)