-
Notifications
You must be signed in to change notification settings - Fork 682
/
Copy pathCustomConversions.java
994 lines (815 loc) · 36.2 KB
/
CustomConversions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/*
* Copyright 2011-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.convert;
import java.lang.annotation.Annotation;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.ConverterBuilder.ConverterAware;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.CustomCollections;
import org.springframework.data.util.Predicates;
import org.springframework.data.util.Streamable;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* Value object to capture custom conversion. That is essentially a {@link List} of converters and some additional logic
* around them. The converters build up two sets of types which store-specific basic types can be converted into and
* from. These types will be considered simple ones (which means they neither need deeper inspection nor nested
* conversion. Thus, the {@link CustomConversions} also act as factory for {@link SimpleTypeHolder} .
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
* @author Xeno Amess
* @author Johannes Englmeier
* @since 2.0
*/
public class CustomConversions {
private static final Log logger = LogFactory.getLog(CustomConversions.class);
private static final String READ_CONVERTER_NOT_SIMPLE = "Registering converter from %s to %s as reading converter although it doesn't convert from a store-supported type; You might want to check your annotation setup at the converter implementation";
private static final String WRITE_CONVERTER_NOT_SIMPLE = "Registering converter from %s to %s as writing converter although it doesn't convert to a store-supported type; You might want to check your annotation setup at the converter implementation";
private static final String NOT_A_CONVERTER = "Converter %s is neither a Spring Converter, GenericConverter or ConverterFactory";
private static final String CONVERTER_FILTER = "converter from %s to %s as %s converter";
private static final String ADD_CONVERTER = "Adding %s" + CONVERTER_FILTER;
private static final String SKIP_CONVERTER = "Skipping " + CONVERTER_FILTER
+ " %s is not a store supported simple type";
private static final List<Object> DEFAULT_CONVERTERS;
static {
List<Object> defaults = new ArrayList<>();
defaults.addAll(Jsr310Converters.getConvertersToRegister());
defaults.addAll(JMoleculesConverters.getConvertersToRegister());
DEFAULT_CONVERTERS = Collections.unmodifiableList(defaults);
}
private final SimpleTypeHolder simpleTypeHolder;
private final List<Object> converters;
private final Set<ConvertiblePair> readingPairs = new LinkedHashSet<>();
private final Set<ConvertiblePair> writingPairs = new LinkedHashSet<>();
private final Set<Class<?>> customSimpleTypes = new HashSet<>();
private final ConversionTargetsCache customReadTargetTypes = new ConversionTargetsCache();
private final ConversionTargetsCache customWriteTargetTypes = new ConversionTargetsCache();
private final ConverterConfiguration converterConfiguration;
private final Function<ConvertiblePair, Class<?>> getReadTarget = convertiblePair -> getCustomTarget(
convertiblePair.getSourceType(), convertiblePair.getTargetType(), readingPairs);
private final Function<ConvertiblePair, Class<?>> getWriteTarget = convertiblePair -> getCustomTarget(
convertiblePair.getSourceType(), convertiblePair.getTargetType(), writingPairs);
private final Function<ConvertiblePair, Class<?>> getRawWriteTarget = convertiblePair -> getCustomTarget(
convertiblePair.getSourceType(), null, writingPairs);
@Nullable
private final PropertyValueConversions propertyValueConversions;
/**
* @param converterConfiguration the {@link ConverterConfiguration} to apply.
* @since 2.3
*/
public CustomConversions(@NonNull ConverterConfiguration converterConfiguration) {
this.converterConfiguration = converterConfiguration;
List<Object> registeredConverters = collectPotentialConverterRegistrations(
converterConfiguration.getStoreConversions(), converterConfiguration.getUserConverters()).stream()
.filter(this::isSupportedConverter)
.filter(this::shouldRegister)
.map(ConverterRegistrationIntent::getConverterRegistration)
.map(this::register)
.distinct()
.collect(Collectors.toList());
Collections.reverse(registeredConverters);
this.converters = Collections.unmodifiableList(registeredConverters);
this.simpleTypeHolder = new SimpleTypeHolder(customSimpleTypes,
converterConfiguration.getStoreConversions().getStoreTypeHolder());
this.propertyValueConversions = converterConfiguration.getPropertyValueConversions();
}
/**
* Creates a new {@link CustomConversions} instance registering all given user defined converters and selecting
* {@link Converter converters} from {@link StoreConversions} depending on
* {@link StoreConversions#getSimpleTypeHolder() store simple types} only considering those that either convert
* to/from a store supported type.
*
* @param storeConversions must not be {@literal null}.
* @param converters must not be {@literal null}.
*/
public CustomConversions(@NonNull StoreConversions storeConversions, @NonNull Collection<?> converters) {
this(new ConverterConfiguration(storeConversions, new ArrayList<>(converters)));
}
/**
* Returns the underlying {@link SimpleTypeHolder}.
*
* @return the underlying {@link SimpleTypeHolder}.
* @see SimpleTypeHolder
*/
public @NonNull SimpleTypeHolder getSimpleTypeHolder() {
return simpleTypeHolder;
}
/**
* Determines whether the given, required {@link PersistentProperty property} has a value-specific converter
* registered. Returns {@literal false} if no {@link PropertyValueConversions} have been configured for the
* underlying store.
* <p>
* This method protects against {@literal null} when not {@link PropertyValueConversions} have been configured for
* the underlying data store, and is a shortcut for:
*
* <code>
* customConversions.getPropertyValueConversions().hasValueConverter(property);
* </code>
*
* @param property {@link PersistentProperty} to evaluate; must not be {@literal null}.
* @return a boolean value indicating whether {@link PropertyValueConverter} has been configured and registered
* for the {@link PersistentProperty property}.
* @see PropertyValueConversions#hasValueConverter(PersistentProperty)
* @see #getPropertyValueConversions()
* @see PersistentProperty
*/
public boolean hasValueConverter(@NonNull PersistentProperty<?> property) {
PropertyValueConversions propertyValueConversions = getPropertyValueConversions();
return propertyValueConversions != null && propertyValueConversions.hasValueConverter(property);
}
/**
* Returns whether the given type is considered to be simple. That means it's either a general simple type or we have
* a writing {@link Converter} registered for a particular type.
*
* @see SimpleTypeHolder#isSimpleType(Class)
* @param type {@link Class} to evaluate as a simple type, such as a primitive type.
* @return a boolean value indicating whether the given, required {@link Class type} is simple.
*/
// TODO: Technically, an 'isXyz(..)' method (returning a boolean to answer a user's question should not throw an Exception).
// Rather, a null Class type argument should simply return false to indicate it is clearly not a "simple type".
// How much data store specific code relies on the existing behavior?
public boolean isSimpleType(@NonNull Class<?> type) {
Assert.notNull(type, "Type must not be null");
return simpleTypeHolder.isSimpleType(type);
}
/**
* Populates the given {@link GenericConversionService} with the converters registered.
*
* @param conversionService {@link ConverterRegistry} to populate; must not be {@literal null}.
* @see ConverterRegistry
*/
public void registerConvertersIn(@NonNull ConverterRegistry conversionService) {
Assert.notNull(conversionService, "ConversionService must not be null");
converters.forEach(it -> registerConverterIn(it, conversionService));
CustomCollections.registerConvertersIn(conversionService);
}
/**
* Gets a reference to the configured {@link PropertyValueConversions} if property value conversions
* are supported by the underlying data store.
*
* @return a reference to the configured {@link PropertyValueConversions}; may be {@literal null}
* if the underlying data store does not support property value conversions.
* @see PropertyValueConversions
*/
@Nullable
public PropertyValueConversions getPropertyValueConversions() {
return propertyValueConversions;
}
/**
* Get all converters and add origin information
*
* @param storeConversions collection of store-base conversions; must not be {@literal null}.
* @param converters collections of custom, user-based converters; must not be {@literal null}.
* @return a {@link List} of intended {@link ConverterRegistration ConverterRegistrations}.
* @see ConverterRegistration
* @since 2.3
*/
private List<ConverterRegistrationIntent> collectPotentialConverterRegistrations(@
NonNull StoreConversions storeConversions, @NonNull Collection<?> converters) {
List<ConverterRegistrationIntent> converterRegistrations = new ArrayList<>();
converters.stream()
.map(storeConversions::getRegistrationsFor)
.flatMap(Streamable::stream)
.map(ConverterRegistrationIntent::userConverters)
.forEach(converterRegistrations::add);
storeConversions.getStoreConverters().stream()
.map(storeConversions::getRegistrationsFor)
.flatMap(Streamable::stream)
.map(ConverterRegistrationIntent::storeConverters)
.forEach(converterRegistrations::add);
DEFAULT_CONVERTERS.stream()
.map(storeConversions::getRegistrationsFor)
.flatMap(Streamable::stream)
.map(ConverterRegistrationIntent::defaultConverters)
.forEach(converterRegistrations::add);
return converterRegistrations;
}
/**
* Registers the given converter in the given {@link GenericConversionService}.
*
* @param candidate must not be {@literal null}.
* @param conversionService must not be {@literal null}.
*/
private void registerConverterIn(Object candidate, ConverterRegistry conversionService) {
if (candidate instanceof Converter converter) {
conversionService.addConverter(converter);
} else if (candidate instanceof ConverterFactory converterFactory) {
conversionService.addConverterFactory(converterFactory);
} else if (candidate instanceof GenericConverter genericConverter) {
conversionService.addConverter(genericConverter);
} else if (candidate instanceof ConverterAware converterAware) {
converterAware.getConverters().forEach(it -> registerConverterIn(it, conversionService));
} else {
throw new IllegalArgumentException(String.format(NOT_A_CONVERTER, candidate));
}
}
/**
* Registers the given {@link ConvertiblePair} as reading or writing pair depending on the type sides being basic
* types.
*
* @param converterRegistration {@link ConverterRegistration} to register; must not be {@literal null}.
* @see ConverterRegistration
*/
private Object register(@NonNull ConverterRegistration converterRegistration) {
Assert.notNull(converterRegistration, "Converter registration must not be null");
ConvertiblePair pair = converterRegistration.getConvertiblePair();
if (converterRegistration.isReading()) {
readingPairs.add(pair);
if (logger.isWarnEnabled() && !converterRegistration.isSimpleSourceType()) {
logger.warn(String.format(READ_CONVERTER_NOT_SIMPLE, pair.getSourceType(), pair.getTargetType()));
}
}
if (converterRegistration.isWriting()) {
writingPairs.add(pair);
customSimpleTypes.add(pair.getSourceType());
if (logger.isWarnEnabled() && !converterRegistration.isSimpleTargetType()) {
logger.warn(String.format(WRITE_CONVERTER_NOT_SIMPLE, pair.getSourceType(), pair.getTargetType()));
}
}
return converterRegistration.getConverter();
}
/**
* Validate a given {@link ConverterRegistration} in a specific setup.<br/>
* Non {@link ReadingConverter reading} and user defined {@link Converter converters} are only considered supported if
* the {@link ConverterRegistrationIntent#isSimpleTargetType() target type} is considered to be a store simple type.
*
* @param registrationIntent {@link ConverterRegistrationIntent} to validate; must not be {@literal null}.
* @return {@literal true} if supported.
* @since 2.3
*/
private boolean isSupportedConverter(@NonNull ConverterRegistrationIntent registrationIntent) {
boolean register = registrationIntent.isUserConverter() || registrationIntent.isStoreConverter()
|| (registrationIntent.isReading() && registrationIntent.isSimpleSourceType())
|| (registrationIntent.isWriting() && registrationIntent.isSimpleTargetType());
if (logger.isDebugEnabled()) {
if (register) {
logger.debug(String.format(ADD_CONVERTER, registrationIntent.isUserConverter() ? "user defined " : "",
registrationIntent.getSourceType(), registrationIntent.getTargetType(),
registrationIntent.isReading() ? "reading" : "writing"));
} else {
logger.debug(String.format(SKIP_CONVERTER, registrationIntent.getSourceType(),
registrationIntent.getTargetType(), registrationIntent.isReading() ? "reading" : "writing",
registrationIntent.isReading() ? registrationIntent.getSourceType() : registrationIntent.getTargetType()));
}
}
return register;
}
/**
* @param intent must not be {@literal null}.
* @return {@literal false} if the given {@link ConverterRegistration} shall be skipped.
* @since 2.3
*/
private boolean shouldRegister(@NonNull ConverterRegistrationIntent intent) {
return !intent.isDefaultConverter()
|| converterConfiguration.shouldRegister(intent.getConverterRegistration().getConvertiblePair());
}
/**
* Returns the target type to convert to in case we have a custom conversion registered to convert the given source
* type into a store native one.
*
* @param sourceType must not be {@literal null}
* @return the target type to convert to in case we have a custom conversion registered to convert the given source
* type into a store native one.
*/
public Optional<Class<?>> getCustomWriteTarget(@NonNull Class<?> sourceType) {
Assert.notNull(sourceType, "Source type must not be null");
Class<?> target = customWriteTargetTypes.computeIfAbsent(sourceType, getRawWriteTarget);
return Void.class.equals(target) || target == null ? Optional.empty() : Optional.of(target);
}
/**
* Returns the target type we can read an inject of the given source type to. The returned type might be a subclass of
* the given expected type though. If {@code requestedTargetType} is {@literal null} we will simply return the first
* target type matching or {@literal null} if no conversion can be found.
*
* @param sourceType must not be {@literal null}
* @param requestedTargetType must not be {@literal null}.
* @return the target type we can read an inject of the given source type to.
*/
public Optional<Class<?>> getCustomWriteTarget(@NonNull Class<?> sourceType, @NonNull Class<?> requestedTargetType) {
Assert.notNull(sourceType, "Source type must not be null");
Assert.notNull(requestedTargetType, "Target type must not be null");
Class<?> target = customWriteTargetTypes.computeIfAbsent(sourceType, requestedTargetType, getWriteTarget);
return Void.class.equals(target) || target == null ? Optional.empty() : Optional.of(target);
}
/**
* Returns whether we have a custom conversion registered to read {@code sourceType} into a native type. The returned
* type might be a subclass of the given expected type though.
*
* @param sourceType must not be {@literal null}
* @return whether we have a custom conversion registered to read {@code sourceType} into a native type.
*/
public boolean hasCustomWriteTarget(@NonNull Class<?> sourceType) {
Assert.notNull(sourceType, "Source type must not be null");
return getCustomWriteTarget(sourceType).isPresent();
}
/**
* Returns whether we have a custom conversion registered to read an object of the given source type into an object of
* the given native target type.
*
* @param sourceType must not be {@literal null}.
* @param targetType must not be {@literal null}.
* @return whether we have a custom conversion registered to read an object of the given source type into an object
* of the given native target type.
*/
public boolean hasCustomWriteTarget(@NonNull Class<?> sourceType, @NonNull Class<?> targetType) {
Assert.notNull(sourceType, "Source type must not be null");
Assert.notNull(targetType, "Target type must not be null");
return getCustomWriteTarget(sourceType, targetType).isPresent();
}
/**
* Returns whether we have a custom conversion registered to read the given source into the given target type.
*
* @param sourceType must not be {@literal null}
* @param targetType must not be {@literal null}
* @return whether we have a custom conversion registered to read the given source into the given target type.
*/
public boolean hasCustomReadTarget(@NonNull Class<?> sourceType, @NonNull Class<?> targetType) {
Assert.notNull(sourceType, "Source type must not be null");
Assert.notNull(targetType, "Target type must not be null");
return getCustomReadTarget(sourceType, targetType) != null;
}
/**
* Returns the actual target type for the given {@code sourceType} and {@code targetType}. Note that the returned
* {@link Class} could be an assignable type to the given {@code targetType}.
*
* @param sourceType must not be {@literal null}.
* @param targetType must not be {@literal null}.
* @return the actual target type for the given {@code sourceType} and {@code targetType}.
*/
@Nullable
private Class<?> getCustomReadTarget(@NonNull Class<?> sourceType, @NonNull Class<?> targetType) {
return customReadTargetTypes.computeIfAbsent(sourceType, targetType, getReadTarget);
}
/**
* Inspects the given {@link ConvertiblePair ConvertiblePairs} for ones that have a source compatible type as source.
* Additionally, checks assignability of the target type if one is given.
*
* @param sourceType must not be {@literal null}.
* @param targetType can be {@literal null}.
* @param pairs must not be {@literal null}.
* @return the base {@link Class type} for the (requested) {@link Class target type} if present.
*/
@Nullable
private Class<?> getCustomTarget(@NonNull Class<?> sourceType, @Nullable Class<?> targetType,
Collection<ConvertiblePair> pairs) {
if (targetType != null && pairs.contains(new ConvertiblePair(sourceType, targetType))) {
return targetType;
}
for (ConvertiblePair pair : pairs) {
if (!hasAssignableSourceType(pair, sourceType)) {
continue;
}
Class<?> candidate = pair.getTargetType();
if (!requestedTargetTypeIsAssignable(targetType, candidate)) {
continue;
}
return candidate;
}
return null;
}
private static boolean hasAssignableSourceType(@NonNull ConvertiblePair pair, @NonNull Class<?> sourceType) {
return pair.getSourceType().isAssignableFrom(sourceType);
}
private static boolean requestedTargetTypeIsAssignable(@Nullable Class<?> requestedTargetType,
@NonNull Class<?> targetType) {
return requestedTargetType == null || targetType.isAssignableFrom(requestedTargetType);
}
/**
* Value object to cache custom conversion targets.
*
* @author Mark Paluch
*/
static class ConversionTargetsCache {
private final Map<Class<?>, TargetTypes> customReadTargetTypes = new ConcurrentHashMap<>();
/**
* Get or compute a target type given its {@code sourceType}. Returns a cached {@link Optional} if the value
* (present/absent target) was computed once. Otherwise, uses a {@link Function mappingFunction} to determine a
* possibly existing target type.
*
* @param sourceType must not be {@literal null}.
* @param mappingFunction must not be {@literal null}.
* @return the optional target type.
*/
@Nullable
public Class<?> computeIfAbsent(Class<?> sourceType, Function<ConvertiblePair, Class<?>> mappingFunction) {
return computeIfAbsent(sourceType, AbsentTargetTypeMarker.class, mappingFunction);
}
/**
* Get or compute a target type given its {@code sourceType} and {@code targetType}. Returns a cached
* {@link Optional} if the value (present/absent target) was computed once. Otherwise, uses a {@link Function
* mappingFunction} to determine a possibly existing target type.
*
* @param sourceType must not be {@literal null}.
* @param targetType must not be {@literal null}.
* @param mappingFunction must not be {@literal null}.
* @return the optional target type.
*/
@Nullable
public Class<?> computeIfAbsent(Class<?> sourceType, Class<?> targetType,
Function<ConvertiblePair, Class<?>> mappingFunction) {
TargetTypes targetTypes = customReadTargetTypes.get(sourceType);
if (targetTypes == null) {
targetTypes = customReadTargetTypes.computeIfAbsent(sourceType, TargetTypes::new);
}
return targetTypes.computeIfAbsent(targetType, mappingFunction);
}
/**
* Marker type for absent target type caching.
*/
interface AbsentTargetTypeMarker {}
}
/**
* Value object for a specific {@code Class source type} to determine possible target conversion types.
*
* @author Mark Paluch
*/
static class TargetTypes {
private final Class<?> sourceType;
private final Map<Class<?>, Class<?>> conversionTargets = new ConcurrentHashMap<>();
TargetTypes(Class<?> sourceType) {
this.sourceType = sourceType;
}
/**
* Get or compute a target type given its {@code targetType}. Returns a cached {@link Optional} if the value
* (present/absent target) was computed once. Otherwise, uses a {@link Function mappingFunction} to determine a
* possibly existing target type.
*
* @param targetType must not be {@literal null}.
* @param mappingFunction must not be {@literal null}.
* @return the optional target type.
*/
@Nullable
public Class<?> computeIfAbsent(Class<?> targetType, Function<ConvertiblePair, Class<?>> mappingFunction) {
Class<?> optionalTarget = conversionTargets.get(targetType);
if (optionalTarget == null) {
optionalTarget = mappingFunction.apply(new ConvertiblePair(sourceType, targetType));
conversionTargets.put(targetType, optionalTarget == null ? Void.class : optionalTarget);
}
return Void.class.equals(optionalTarget) ? null : optionalTarget;
}
}
/**
* Value class tying together a {@link ConverterRegistration} and its {@link ConverterOrigin origin} to allow fine
* grained registration based on store supported types.
*
* @since 2.3
* @author Christoph Strobl
*/
protected static class ConverterRegistrationIntent {
private final ConverterRegistration delegate;
private final ConverterOrigin origin;
ConverterRegistrationIntent(ConverterRegistration delegate, ConverterOrigin origin) {
this.delegate = delegate;
this.origin = origin;
}
static ConverterRegistrationIntent userConverters(ConverterRegistration delegate) {
return new ConverterRegistrationIntent(delegate, ConverterOrigin.USER_DEFINED);
}
static ConverterRegistrationIntent storeConverters(ConverterRegistration delegate) {
return new ConverterRegistrationIntent(delegate, ConverterOrigin.STORE);
}
static ConverterRegistrationIntent defaultConverters(ConverterRegistration delegate) {
return new ConverterRegistrationIntent(delegate, ConverterOrigin.DEFAULT);
}
Class<?> getSourceType() {
return delegate.getConvertiblePair().getSourceType();
}
Class<?> getTargetType() {
return delegate.getConvertiblePair().getTargetType();
}
public boolean isWriting() {
return delegate.isWriting();
}
public boolean isReading() {
return delegate.isReading();
}
public boolean isSimpleSourceType() {
return delegate.isSimpleSourceType();
}
public boolean isSimpleTargetType() {
return delegate.isSimpleTargetType();
}
public boolean isUserConverter() {
return isConverterOfSource(ConverterOrigin.USER_DEFINED);
}
public boolean isStoreConverter() {
return isConverterOfSource(ConverterOrigin.STORE);
}
public boolean isDefaultConverter() {
return isConverterOfSource(ConverterOrigin.DEFAULT);
}
public ConverterRegistration getConverterRegistration() {
return delegate;
}
private boolean isConverterOfSource(ConverterOrigin source) {
return origin.equals(source);
}
protected enum ConverterOrigin {
DEFAULT, USER_DEFINED, STORE
}
}
/**
* Conversion registration information.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
private static class ConverterRegistration {
private final Object converter;
private final ConvertiblePair convertiblePair;
private final StoreConversions storeConversions;
private final boolean reading;
private final boolean writing;
private ConverterRegistration(Object converter, ConvertiblePair convertiblePair, StoreConversions storeConversions,
boolean reading, boolean writing) {
this.converter = converter;
this.convertiblePair = convertiblePair;
this.storeConversions = storeConversions;
this.reading = reading;
this.writing = writing;
}
/**
* Returns whether the converter shall be used for writing.
*
* @return
*/
public boolean isWriting() {
return writing || (!reading && isSimpleTargetType());
}
/**
* Returns whether the converter shall be used for reading.
*
* @return
*/
public boolean isReading() {
return reading || (!writing && isSimpleSourceType());
}
/**
* Returns the actual conversion pair.
*
* @return
*/
public ConvertiblePair getConvertiblePair() {
return convertiblePair;
}
/**
* Returns whether the source type is a simple one.
*
* @return
*/
public boolean isSimpleSourceType() {
return storeConversions.isStoreSimpleType(convertiblePair.getSourceType());
}
/**
* Returns whether the target type is a simple one.
*
* @return
*/
public boolean isSimpleTargetType() {
return storeConversions.isStoreSimpleType(convertiblePair.getTargetType());
}
/**
* @return
*/
Object getConverter() {
return converter;
}
}
/**
* Value type to capture store-specific extensions to the {@link CustomConversions}. Allows to forward store specific
* default conversions and a set of types that are supposed to be considered simple.
*
* @author Oliver Gierke
*/
public static class StoreConversions {
public static final StoreConversions NONE = StoreConversions.of(SimpleTypeHolder.DEFAULT, Collections.emptyList());
private final SimpleTypeHolder storeTypeHolder;
private final Collection<?> storeConverters;
private StoreConversions(SimpleTypeHolder storeTypeHolder, Collection<?> storeConverters) {
this.storeTypeHolder = storeTypeHolder;
this.storeConverters = storeConverters;
}
/**
* Creates a new {@link StoreConversions} for the given store-specific {@link SimpleTypeHolder} and the given
* converters.
*
* @param storeTypeHolder must not be {@literal null}.
* @param converters must not be {@literal null}.
* @return
*/
public static StoreConversions of(SimpleTypeHolder storeTypeHolder, Object... converters) {
Assert.notNull(storeTypeHolder, "SimpleTypeHolder must not be null");
Assert.notNull(converters, "Converters must not be null");
return new StoreConversions(storeTypeHolder, Arrays.asList(converters));
}
/**
* Creates a new {@link StoreConversions} for the given store-specific {@link SimpleTypeHolder} and the given
* converters.
*
* @param storeTypeHolder must not be {@literal null}.
* @param converters must not be {@literal null}.
* @return
*/
public static StoreConversions of(SimpleTypeHolder storeTypeHolder, Collection<?> converters) {
Assert.notNull(storeTypeHolder, "SimpleTypeHolder must not be null");
Assert.notNull(converters, "Converters must not be null");
return new StoreConversions(storeTypeHolder, converters);
}
/**
* Returns {@link ConverterRegistration}s for the given converter.
*
* @param converter must not be {@literal null}.
* @return
*/
public Streamable<ConverterRegistration> getRegistrationsFor(Object converter) {
Assert.notNull(converter, "Converter must not be null");
Class<?> type = converter.getClass();
boolean isWriting = isAnnotatedWith(type, WritingConverter.class);
boolean isReading = isAnnotatedWith(type, ReadingConverter.class);
if (converter instanceof ConverterAware converterAware) {
return Streamable.of(() -> converterAware.getConverters().stream()
.flatMap(it -> getRegistrationsFor(it).stream()));
} else if (converter instanceof GenericConverter genericConverter) {
Set<ConvertiblePair> convertibleTypes = genericConverter.getConvertibleTypes();
return convertibleTypes == null
? Streamable.empty()
: Streamable.of(convertibleTypes).map(it -> register(converter, it, isReading, isWriting));
} else if (converter instanceof ConverterFactory) {
return getRegistrationFor(converter, ConverterFactory.class, isReading, isWriting);
} else if (converter instanceof Converter) {
return getRegistrationFor(converter, Converter.class, isReading, isWriting);
} else {
throw new IllegalArgumentException(String.format("Unsupported converter type %s", converter));
}
}
private static boolean isAnnotatedWith(Class<?> type, Class<? extends Annotation> annotationType) {
return AnnotationUtils.findAnnotation(type, annotationType) != null;
}
private Streamable<ConverterRegistration> getRegistrationFor(Object converter, Class<?> type, boolean isReading,
boolean isWriting) {
Class<?> converterType = converter.getClass();
Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converterType, type);
if (arguments == null) {
throw new IllegalStateException(String.format("Couldn't resolve type arguments for %s", converterType));
}
return Streamable.of(register(converter, arguments[0], arguments[1], isReading, isWriting));
}
private ConverterRegistration register(Object converter, Class<?> source, Class<?> target, boolean isReading,
boolean isWriting) {
return register(converter, new ConvertiblePair(source, target), isReading, isWriting);
}
private ConverterRegistration register(Object converter, ConvertiblePair pair, boolean isReading,
boolean isWriting) {
return new ConverterRegistration(converter, pair, this, isReading, isWriting);
}
private boolean isStoreSimpleType(Class<?> type) {
return storeTypeHolder.isSimpleType(type);
}
SimpleTypeHolder getStoreTypeHolder() {
return this.storeTypeHolder;
}
Collection<?> getStoreConverters() {
return this.storeConverters;
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (!(o instanceof StoreConversions that)) {
return false;
}
if (!ObjectUtils.nullSafeEquals(storeTypeHolder, that.storeTypeHolder)) {
return false;
}
return ObjectUtils.nullSafeEquals(storeConverters, that.storeConverters);
}
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(storeTypeHolder);
result = 31 * result + ObjectUtils.nullSafeHashCode(storeConverters);
return result;
}
@Override
public String toString() {
return "StoreConversions{" + "storeTypeHolder=" + storeTypeHolder + ", storeConverters=" + storeConverters + '}';
}
}
/**
* Value object holding the actual {@link StoreConversions} and custom {@link Converter converters} configured for
* registration.
*
* @author Christoph Strobl
* @since 2.3
*/
protected static class ConverterConfiguration {
private final StoreConversions storeConversions;
private final List<?> userConverters;
private final Predicate<ConvertiblePair> converterRegistrationFilter;
private final PropertyValueConversions propertyValueConversions;
/**
* Create a new ConverterConfiguration holding the given {@link StoreConversions} and user defined converters.
*
* @param storeConversions must not be {@literal null}.
* @param userConverters must not be {@literal null} use {@link Collections#emptyList()} instead.
*/
public ConverterConfiguration(StoreConversions storeConversions, List<?> userConverters) {
this(storeConversions, userConverters, Predicates.isTrue());
}
/**
* Create a new ConverterConfiguration holding the given {@link StoreConversions} and user defined converters as
* well as a {@link Collection} of {@link ConvertiblePair} for which to skip the registration of default converters.
* <br />
* This allows store implementations to modify default converter registration based on specific needs and
* configurations. User defined converters will are never subject of filtering.
*
* @param storeConversions must not be {@literal null}.
* @param userConverters must not be {@literal null} use {@link Collections#emptyList()} instead.
* @param converterRegistrationFilter must not be {@literal null}..
*/
public ConverterConfiguration(StoreConversions storeConversions, List<?> userConverters,
Predicate<ConvertiblePair> converterRegistrationFilter) {
this(storeConversions, userConverters, converterRegistrationFilter, PropertyValueConversions.simple(it -> {}));
}
/**
* Create a new ConverterConfiguration holding the given {@link StoreConversions} and user defined converters as
* well as a {@link Collection} of {@link ConvertiblePair} for which to skip the registration of default converters.
* <br />
* This allows store implementations to modify default converter registration based on specific needs and
* configurations. User defined converters will are never subject of filtering.
*
* @param storeConversions must not be {@literal null}.
* @param userConverters must not be {@literal null} use {@link Collections#emptyList()} instead.
* @param converterRegistrationFilter must not be {@literal null}.
* @param propertyValueConversions can be {@literal null}.
* @since 2.7
*/
public ConverterConfiguration(@NonNull StoreConversions storeConversions, @NonNull List<?> userConverters,
@NonNull Predicate<ConvertiblePair> converterRegistrationFilter,
@Nullable PropertyValueConversions propertyValueConversions) {
this.storeConversions = storeConversions;
this.userConverters = new ArrayList<>(userConverters);
this.converterRegistrationFilter = converterRegistrationFilter;
this.propertyValueConversions = propertyValueConversions;
}
/**
* @return never {@literal null}
*/
StoreConversions getStoreConversions() {
return storeConversions;
}
/**
* @return never {@literal null}.
*/
List<?> getUserConverters() {
return userConverters;
}
/**
* @return never {@literal null}.
*/
boolean shouldRegister(ConvertiblePair candidate) {
return this.converterRegistrationFilter.test(candidate);
}
/**
* @return the configured {@link PropertyValueConversions} if set, {@literal null} otherwise.
* @since 2.7
*/
@Nullable
public PropertyValueConversions getPropertyValueConversions() {
return this.propertyValueConversions;
}
}
}