Skip to content

Commit 8ab3bf5

Browse files
committedMar 14, 2025
Introduce CustomConversions.getRequiredValueConverter(…).
Provide convenience method to simplify flow in calling code. See #3170
1 parent aec2c98 commit 8ab3bf5

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed
 

‎src/main/java/org/springframework/data/convert/CustomConversions.java

+29-2
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,36 @@ public SimpleTypeHolder getSimpleTypeHolder() {
185185
*/
186186
public boolean hasValueConverter(PersistentProperty<?> property) {
187187

188-
PropertyValueConversions propertyValueConversions = getPropertyValueConversions();
188+
PropertyValueConversions pvc = getPropertyValueConversions();
189189

190-
return propertyValueConversions != null && propertyValueConversions.hasValueConverter(property);
190+
return pvc != null && pvc.hasValueConverter(property);
191+
}
192+
193+
/**
194+
* Returns the required {@link PropertyValueConverter} for the given {@link PersistentProperty} or throws
195+
* {@link IllegalStateException} if no converter is available. This is a convenience method for
196+
* {@code getPropertyValueConversions().getValueConverter(…)} enforcing non-null constraints.
197+
* <p>
198+
* Prior to calling this method you should verify a converter is available using
199+
* {@link #hasValueConverter(PersistentProperty)}.
200+
*
201+
* @param property {@link PersistentProperty} to evaluate; must not be {@literal null}.
202+
* @return the required {@link PropertyValueConverter}
203+
* @throws IllegalStateException if no converter is available.
204+
* @since 4.0
205+
* @see #hasValueConverter(PersistentProperty)
206+
*/
207+
public <DV, SV, P extends PersistentProperty<P>, VCC extends ValueConversionContext<P>> PropertyValueConverter<DV, SV, VCC> getRequiredValueConverter(
208+
P property) {
209+
210+
PropertyValueConversions pvc = getPropertyValueConversions();
211+
PropertyValueConverter<DV, SV, VCC> converter = pvc != null ? pvc.getValueConverter(property) : null;
212+
213+
if (converter == null) {
214+
throw new IllegalStateException("No value converter registered for property %s".formatted(property.getName()));
215+
}
216+
217+
return converter;
191218
}
192219

193220
/**

0 commit comments

Comments
 (0)
Please sign in to comment.