37
37
import org .springframework .core .GenericTypeResolver ;
38
38
import org .springframework .core .convert .converter .Converter ;
39
39
import org .springframework .core .convert .converter .ConverterFactory ;
40
+ import org .springframework .core .convert .converter .ConverterRegistry ;
40
41
import org .springframework .core .convert .converter .GenericConverter ;
41
42
import org .springframework .core .convert .converter .GenericConverter .ConvertiblePair ;
42
43
import org .springframework .core .convert .support .GenericConversionService ;
44
+ import org .springframework .data .convert .ConverterBuilder .ConverterAware ;
43
45
import org .springframework .data .mapping .model .SimpleTypeHolder ;
44
46
import org .springframework .data .util .Optionals ;
45
47
import org .springframework .data .util .Streamable ;
@@ -90,10 +92,12 @@ public class CustomConversions {
90
92
/**
91
93
* Creates a new {@link CustomConversions} instance registering the given converters.
92
94
*
93
- * @param converters
95
+ * @param storeConversions must not be {@literal null}.
96
+ * @param converters must not be {@literal null}.
94
97
*/
95
- public CustomConversions (StoreConversions storeConversions , List <?> converters ) {
98
+ public CustomConversions (StoreConversions storeConversions , Collection <?> converters ) {
96
99
100
+ Assert .notNull (storeConversions , "StoreConversions must not be null!" );
97
101
Assert .notNull (converters , "List of converters must not be null!" );
98
102
99
103
this .readingPairs = new LinkedHashSet <>();
@@ -149,33 +153,46 @@ public boolean isSimpleType(Class<?> type) {
149
153
*
150
154
* @param conversionService
151
155
*/
152
- public void registerConvertersIn (GenericConversionService conversionService ) {
156
+ public void registerConvertersIn (ConverterRegistry conversionService ) {
153
157
154
158
Assert .notNull (conversionService , "ConversionService must not be null!" );
155
159
156
- converters .forEach (it -> {
160
+ converters .forEach (it -> registerConverterIn (it , conversionService ));
161
+ }
157
162
158
- boolean added = false ;
163
+ /**
164
+ * Registers the given converter in the given {@link GenericConversionService}.
165
+ *
166
+ * @param candidate must not be {@literal null}.
167
+ * @param conversionService must not be {@literal null}.
168
+ */
169
+ private void registerConverterIn (Object candidate , ConverterRegistry conversionService ) {
159
170
160
- if (it instanceof Converter ) {
161
- conversionService .addConverter (Converter .class .cast (it ));
162
- added = true ;
163
- }
171
+ boolean added = false ;
164
172
165
- if (it instanceof ConverterFactory ) {
166
- conversionService .addConverterFactory ( ConverterFactory .class .cast (it ));
167
- added = true ;
168
- }
173
+ if (candidate instanceof Converter ) {
174
+ conversionService .addConverter ( Converter .class .cast (candidate ));
175
+ added = true ;
176
+ }
169
177
170
- if (it instanceof GenericConverter ) {
171
- conversionService .addConverter ( GenericConverter .class .cast (it ));
172
- added = true ;
173
- }
178
+ if (candidate instanceof ConverterFactory ) {
179
+ conversionService .addConverterFactory ( ConverterFactory .class .cast (candidate ));
180
+ added = true ;
181
+ }
174
182
175
- if (!added ) {
176
- throw new IllegalArgumentException (String .format (NOT_A_CONVERTER , it ));
177
- }
178
- });
183
+ if (candidate instanceof GenericConverter ) {
184
+ conversionService .addConverter (GenericConverter .class .cast (candidate ));
185
+ added = true ;
186
+ }
187
+
188
+ if (candidate instanceof ConverterAware ) {
189
+ ConverterAware .class .cast (candidate ).getConverters ().forEach (it -> registerConverterIn (it , conversionService ));
190
+ added = true ;
191
+ }
192
+
193
+ if (!added ) {
194
+ throw new IllegalArgumentException (String .format (NOT_A_CONVERTER , candidate ));
195
+ }
179
196
}
180
197
181
198
/**
@@ -460,10 +477,15 @@ public Streamable<ConverterRegistration> getRegistrationsFor(Object converter) {
460
477
boolean isWriting = type .isAnnotationPresent (WritingConverter .class );
461
478
boolean isReading = type .isAnnotationPresent (ReadingConverter .class );
462
479
463
- if (converter instanceof GenericConverter ) {
480
+ if (converter instanceof ConverterAware ) {
481
+
482
+ return Streamable .of (() -> ConverterAware .class .cast (converter ).getConverters ().stream ()//
483
+ .flatMap (it -> getRegistrationsFor (it ).stream ()));
484
+
485
+ } else if (converter instanceof GenericConverter ) {
464
486
465
- GenericConverter genericConverter = (GenericConverter ) converter ;
466
- return Streamable . of ( genericConverter . getConvertibleTypes ()) .map (it -> register (it , isReading , isWriting ));
487
+ return Streamable . of (GenericConverter . class . cast ( converter ). getConvertibleTypes ()) //
488
+ .map (it -> register (it , isReading , isWriting ));
467
489
468
490
} else if (converter instanceof ConverterFactory ) {
469
491
@@ -474,7 +496,7 @@ public Streamable<ConverterRegistration> getRegistrationsFor(Object converter) {
474
496
return getRegistrationFor (converter , Converter .class , isReading , isWriting );
475
497
476
498
} else {
477
- throw new IllegalArgumentException ("Unsupported converter type!" );
499
+ throw new IllegalArgumentException (String . format ( "Unsupported converter type %s!" , converter ) );
478
500
}
479
501
}
480
502
0 commit comments