Skip to content

Commit 0792b74

Browse files
committed
Polishing.
Improve null-safety. Refine exception signature to avoid exception catching. See #2590 Original pull request: #2591.
1 parent b9eb4d1 commit 0792b74

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/main/java/org/springframework/data/convert/PropertyValueConversions.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,11 @@ static <P extends PersistentProperty<P>> PropertyValueConversions simple(
6161

6262
SimplePropertyValueConversions conversions = new SimplePropertyValueConversions();
6363
PropertyValueConverterRegistrar registrar = new PropertyValueConverterRegistrar();
64+
6465
config.accept(registrar);
6566
conversions.setValueConverterRegistry(registrar.buildRegistry());
66-
try {
67-
conversions.afterPropertiesSet();
68-
} catch (Exception e) {
69-
throw new IllegalStateException("Could not initialize value conversions.");
70-
}
67+
conversions.afterPropertiesSet();
68+
7169
return conversions;
7270
}
7371
}

src/main/java/org/springframework/data/convert/SimplePropertyValueConversions.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* {@link PropertyValueConverter converter} retrieval.
3333
*
3434
* @author Christoph Strobl
35+
* @author Mark Paluch
3536
* @since 2.7
3637
*/
3738
public class SimplePropertyValueConversions implements PropertyValueConversions, InitializingBean {
@@ -55,6 +56,18 @@ public PropertyValueConverterFactory getConverterFactory() {
5556
return converterFactory;
5657
}
5758

59+
private PropertyValueConverterFactory obtainConverterFactory() {
60+
61+
PropertyValueConverterFactory factory = getConverterFactory();
62+
63+
if (factory == null) {
64+
throw new IllegalStateException(
65+
"PropertyValueConverterFactory is not set. Make sure to either set the converter factory or call afterPropertiesSet() to initialize the object.");
66+
}
67+
68+
return factory;
69+
}
70+
5871
/**
5972
* Set the {@link ValueConverterRegistry converter registry} for path configured converters. This is short for adding
6073
* a
@@ -78,7 +91,7 @@ public ValueConverterRegistry<?> getValueConverterRegistry() {
7891
}
7992

8093
/**
81-
* Dis-/Enable caching. Enabled by default.
94+
* Configure whether to use converter cache. Enabled by default.
8295
*
8396
* @param converterCacheEnabled set to {@literal true} to enable caching of {@link PropertyValueConverter converter}
8497
* instances.
@@ -89,14 +102,14 @@ public void setConverterCacheEnabled(boolean converterCacheEnabled) {
89102

90103
@Override
91104
public boolean hasValueConverter(PersistentProperty<?> property) {
92-
return this.converterFactory.getConverter(property) != null;
105+
return obtainConverterFactory().getConverter(property) != null;
93106
}
94107

95108
@Nullable
96109
@Override
97110
public <DV, SV, C extends PersistentProperty<C>, D extends ValueConversionContext<C>> PropertyValueConverter<DV, SV, D> getValueConverter(
98111
C property) {
99-
return this.converterFactory.getConverter(property);
112+
return obtainConverterFactory().getConverter(property);
100113
}
101114

102115
/**
@@ -125,7 +138,7 @@ public void init() {
125138
}
126139

127140
@Override
128-
public void afterPropertiesSet() throws Exception {
141+
public void afterPropertiesSet() {
129142
init();
130143
}
131144
}

0 commit comments

Comments
 (0)