Polishing.

Improve null-safety. Refine exception signature to avoid exception catching.

See #2590
Original pull request: #2591.
This commit is contained in:
Mark Paluch
2022-04-05 09:14:36 +02:00
parent b9eb4d1416
commit 0792b74549
2 changed files with 20 additions and 9 deletions

View File

@@ -61,13 +61,11 @@ public interface PropertyValueConversions {
SimplePropertyValueConversions conversions = new SimplePropertyValueConversions();
PropertyValueConverterRegistrar registrar = new PropertyValueConverterRegistrar();
config.accept(registrar);
conversions.setValueConverterRegistry(registrar.buildRegistry());
try {
conversions.afterPropertiesSet();
} catch (Exception e) {
throw new IllegalStateException("Could not initialize value conversions.");
}
conversions.afterPropertiesSet();
return conversions;
}
}

View File

@@ -32,6 +32,7 @@ import org.springframework.lang.Nullable;
* {@link PropertyValueConverter converter} retrieval.
*
* @author Christoph Strobl
* @author Mark Paluch
* @since 2.7
*/
public class SimplePropertyValueConversions implements PropertyValueConversions, InitializingBean {
@@ -55,6 +56,18 @@ public class SimplePropertyValueConversions implements PropertyValueConversions,
return converterFactory;
}
private PropertyValueConverterFactory obtainConverterFactory() {
PropertyValueConverterFactory factory = getConverterFactory();
if (factory == null) {
throw new IllegalStateException(
"PropertyValueConverterFactory is not set. Make sure to either set the converter factory or call afterPropertiesSet() to initialize the object.");
}
return factory;
}
/**
* Set the {@link ValueConverterRegistry converter registry} for path configured converters. This is short for adding
* a
@@ -78,7 +91,7 @@ public class SimplePropertyValueConversions implements PropertyValueConversions,
}
/**
* Dis-/Enable caching. Enabled by default.
* Configure whether to use converter cache. Enabled by default.
*
* @param converterCacheEnabled set to {@literal true} to enable caching of {@link PropertyValueConverter converter}
* instances.
@@ -89,14 +102,14 @@ public class SimplePropertyValueConversions implements PropertyValueConversions,
@Override
public boolean hasValueConverter(PersistentProperty<?> property) {
return this.converterFactory.getConverter(property) != null;
return obtainConverterFactory().getConverter(property) != null;
}
@Nullable
@Override
public <DV, SV, C extends PersistentProperty<C>, D extends ValueConversionContext<C>> PropertyValueConverter<DV, SV, D> getValueConverter(
C property) {
return this.converterFactory.getConverter(property);
return obtainConverterFactory().getConverter(property);
}
/**
@@ -125,7 +138,7 @@ public class SimplePropertyValueConversions implements PropertyValueConversions,
}
@Override
public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {
init();
}
}