Skip to content

Commit 57d0b32

Browse files
committed
DATACMNS-1359 - Improved exception message for missing accessors and getters.
The exception messages used in the PersistentProperty.getRequired(Getter|Setter|Wither|Field)(…) now mention the name of the property that's offending.
1 parent 26ade36 commit 57d0b32

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ default Method getRequiredGetter() {
8787
Method getter = getGetter();
8888

8989
if (getter == null) {
90-
throw new IllegalArgumentException("No getter available for this persistent property!");
90+
throw new IllegalArgumentException(String.format("No getter available for persistent property %s!", this));
9191
}
9292

9393
return getter;
@@ -107,7 +107,7 @@ default Method getRequiredSetter() {
107107
Method setter = getSetter();
108108

109109
if (setter == null) {
110-
throw new IllegalArgumentException("No setter available for this persistent property!");
110+
throw new IllegalArgumentException(String.format("No setter available for persistent property %s!", this));
111111
}
112112

113113
return setter;
@@ -145,7 +145,7 @@ default Method getRequiredWither() {
145145
Method wither = getWither();
146146

147147
if (wither == null) {
148-
throw new IllegalArgumentException("No wither available for this persistent property!");
148+
throw new IllegalArgumentException(String.format("No wither available for persistent property %s!", this));
149149
}
150150

151151
return wither;
@@ -159,7 +159,7 @@ default Field getRequiredField() {
159159
Field field = getField();
160160

161161
if (field == null) {
162-
throw new IllegalArgumentException("No field backing this persistent property!");
162+
throw new IllegalArgumentException(String.format("No field backing persistent property %s!", this));
163163
}
164164

165165
return field;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ private Property(TypeInformation<?> type, Optional<Field> field, Optional<Proper
6767
);
6868
this.hashCode = Lazy.of(() -> withFieldOrDescriptor(Object::hashCode));
6969
this.name = Lazy.of(() -> withFieldOrDescriptor(Field::getName, FeatureDescriptor::getName));
70-
this.toString = Lazy.of(() -> withFieldOrDescriptor(Object::toString));
70+
this.toString = Lazy.of(() -> withFieldOrDescriptor(Object::toString,
71+
it -> String.format("%s.%s", type.getType().getName(), it.getDisplayName())));
7172

7273
this.getter = descriptor.map(PropertyDescriptor::getReadMethod)//
7374
.filter(it -> getType() != null)//

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,50 @@ public void detectsUltimateAssociationTargetClass() {
249249
});
250250
}
251251

252+
@Test // DATACMNS-1359
253+
public void missingRequiredGetterThrowsException() {
254+
255+
SamplePersistentProperty property = getProperty(Sample.class, "field");
256+
257+
assertThatExceptionOfType(IllegalArgumentException.class) //
258+
.isThrownBy(() -> property.getRequiredGetter()) //
259+
.withMessageContaining("field") //
260+
.withMessageContaining(Sample.class.getName());
261+
}
262+
263+
@Test // DATACMNS-1359
264+
public void missingRequiredSetterThrowsException() {
265+
266+
SamplePersistentProperty property = getProperty(Sample.class, "field");
267+
268+
assertThatExceptionOfType(IllegalArgumentException.class) //
269+
.isThrownBy(() -> property.getRequiredSetter()) //
270+
.withMessageContaining("field") //
271+
.withMessageContaining(Sample.class.getName());
272+
}
273+
274+
@Test // DATACMNS-1359
275+
public void missingRequiredWitherThrowsException() {
276+
277+
SamplePersistentProperty property = getProperty(Sample.class, "field");
278+
279+
assertThatExceptionOfType(IllegalArgumentException.class) //
280+
.isThrownBy(() -> property.getRequiredWither()) //
281+
.withMessageContaining("field") //
282+
.withMessageContaining(Sample.class.getName());
283+
}
284+
285+
@Test
286+
public void missingRequiredFieldThrowsException() {
287+
288+
SamplePersistentProperty property = getProperty(NoField.class, "firstname");
289+
290+
assertThatExceptionOfType(IllegalArgumentException.class) //
291+
.isThrownBy(() -> property.getRequiredField()) //
292+
.withMessageContaining("firstname") //
293+
.withMessageContaining(NoField.class.getName());
294+
}
295+
252296
@SuppressWarnings("unchecked")
253297
private Map<Class<? extends Annotation>, Annotation> getAnnotationCache(SamplePersistentProperty property) {
254298
return (Map<Class<? extends Annotation>, Annotation>) ReflectionTestUtils.getField(property, "annotationCache");
@@ -428,4 +472,9 @@ static class WithReferences {
428472
@Reference Sample sample;
429473
Sample withoutAnnotation;
430474
}
475+
476+
interface NoField {
477+
478+
String getFirstname();
479+
}
431480
}

0 commit comments

Comments
 (0)