Skip to content

Commit 7be149b

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

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ default Method getRequiredGetter() {
9797
Method getter = getGetter();
9898

9999
if (getter == null) {
100-
throw new IllegalArgumentException("No getter available for this persistent property!");
100+
throw new IllegalArgumentException(String.format("No getter available for persistent property %s!", this));
101101
}
102102

103103
return getter;
@@ -117,7 +117,7 @@ default Method getRequiredSetter() {
117117
Method setter = getSetter();
118118

119119
if (setter == null) {
120-
throw new IllegalArgumentException("No setter available for this persistent property!");
120+
throw new IllegalArgumentException(String.format("No setter available for persistent property %s!", this));
121121
}
122122

123123
return setter;
@@ -131,7 +131,7 @@ default Field getRequiredField() {
131131
Field field = getField();
132132

133133
if (field == null) {
134-
throw new IllegalArgumentException("No field backing this persistent property!");
134+
throw new IllegalArgumentException(String.format("No field backing persistent property %s!", this));
135135
}
136136

137137
return field;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ private Property(TypeInformation<?> type, Optional<Field> field, Optional<Proper
6464
);
6565
this.hashCode = Lazy.of(() -> withFieldOrDescriptor(Object::hashCode));
6666
this.name = Lazy.of(() -> withFieldOrDescriptor(Field::getName, FeatureDescriptor::getName));
67-
this.toString = Lazy.of(() -> withFieldOrDescriptor(Object::toString));
67+
this.toString = Lazy.of(() -> withFieldOrDescriptor(Object::toString,
68+
it -> String.format("%s.%s", type.getType().getName(), it.getDisplayName())));
6869

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

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

+38
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,39 @@ public void getRequiredAnnotationThrowsException() {
239239
assertThatThrownBy(() -> property.getRequiredAnnotation(Transient.class)).isInstanceOf(IllegalStateException.class);
240240
}
241241

242+
@Test // DATACMNS-1359
243+
public void missingRequiredGetterThrowsException() {
244+
245+
SamplePersistentProperty property = getProperty(Sample.class, "field");
246+
247+
assertThatExceptionOfType(IllegalArgumentException.class) //
248+
.isThrownBy(() -> property.getRequiredGetter()) //
249+
.withMessageContaining("field") //
250+
.withMessageContaining(Sample.class.getName());
251+
}
252+
253+
@Test // DATACMNS-1359
254+
public void missingRequiredSetterThrowsException() {
255+
256+
SamplePersistentProperty property = getProperty(Sample.class, "field");
257+
258+
assertThatExceptionOfType(IllegalArgumentException.class) //
259+
.isThrownBy(() -> property.getRequiredSetter()) //
260+
.withMessageContaining("field") //
261+
.withMessageContaining(Sample.class.getName());
262+
}
263+
264+
@Test
265+
public void missingRequiredFieldThrowsException() {
266+
267+
SamplePersistentProperty property = getProperty(NoField.class, "firstname");
268+
269+
assertThatExceptionOfType(IllegalArgumentException.class) //
270+
.isThrownBy(() -> property.getRequiredField()) //
271+
.withMessageContaining("firstname") //
272+
.withMessageContaining(NoField.class.getName());
273+
}
274+
242275
@SuppressWarnings("unchecked")
243276
private Map<Class<? extends Annotation>, Annotation> getAnnotationCache(SamplePersistentProperty property) {
244277
return (Map<Class<? extends Annotation>, Annotation>) ReflectionTestUtils.getField(property, "annotationCache");
@@ -410,4 +443,9 @@ public String getField() {
410443
@interface CustomReadOnly {
411444

412445
}
446+
447+
interface NoField {
448+
449+
String getFirstname();
450+
}
413451
}

0 commit comments

Comments
 (0)