1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -92,7 +92,7 @@ public class GenericConversionService implements ConfigurableConversionService {
92
92
new ConcurrentHashMap <ConverterCacheKey , GenericConverter >(64 );
93
93
94
94
95
- // implementing ConverterRegistry
95
+ // ConverterRegistry implementation
96
96
97
97
@ Override
98
98
public void addConverter (Converter <?, ?> converter ) {
@@ -104,7 +104,8 @@ public void addConverter(Converter<?, ?> converter) {
104
104
105
105
@ Override
106
106
public void addConverter (Class <?> sourceType , Class <?> targetType , Converter <?, ?> converter ) {
107
- addConverter (new ConverterAdapter (converter , ResolvableType .forClass (sourceType ), ResolvableType .forClass (targetType )));
107
+ addConverter (new ConverterAdapter (
108
+ converter , ResolvableType .forClass (sourceType ), ResolvableType .forClass (targetType )));
108
109
}
109
110
110
111
@ Override
@@ -116,7 +117,7 @@ public void addConverter(GenericConverter converter) {
116
117
@ Override
117
118
public void addConverterFactory (ConverterFactory <?, ?> converterFactory ) {
118
119
ResolvableType [] typeInfo = getRequiredTypeInfo (converterFactory , ConverterFactory .class );
119
- Assert .notNull ("Unable to the determine sourceType <S> and targetRangeType R which your " +
120
+ Assert .notNull (typeInfo , "Unable to the determine source type <S> and target range type R which your " +
120
121
"ConverterFactory<S, R> converts between; declare these generic types." );
121
122
addConverter (new ConverterFactoryAdapter (converterFactory ,
122
123
new ConvertiblePair (typeInfo [0 ].resolve (), typeInfo [1 ].resolve ())));
@@ -128,7 +129,8 @@ public void removeConvertible(Class<?> sourceType, Class<?> targetType) {
128
129
invalidateCache ();
129
130
}
130
131
131
- // implementing ConversionService
132
+
133
+ // ConversionService implementation
132
134
133
135
@ Override
134
136
public boolean canConvert (Class <?> sourceType , Class <?> targetType ) {
@@ -148,17 +150,18 @@ public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType)
148
150
}
149
151
150
152
/**
151
- * Returns true if conversion between the sourceType and targetType can be bypassed.
152
- * More precisely this method will return true if objects of sourceType can be
153
+ * Return whether conversion between the sourceType and targetType can be bypassed.
154
+ * <p> More precisely, this method will return true if objects of sourceType can be
153
155
* converted to the targetType by returning the source object unchanged.
154
- * @param sourceType context about the source type to convert from (may be null if source is null)
156
+ * @param sourceType context about the source type to convert from
157
+ * (may be {@code null} if source is {@code null})
155
158
* @param targetType context about the target type to convert to (required)
156
- * @return true if conversion can be bypassed
157
- * @throws IllegalArgumentException if targetType is null
159
+ * @return {@code true} if conversion can be bypassed; {@code false} otherwise
160
+ * @throws IllegalArgumentException if targetType is {@code null}
158
161
* @since 3.2
159
162
*/
160
163
public boolean canBypassConvert (TypeDescriptor sourceType , TypeDescriptor targetType ) {
161
- Assert .notNull (targetType , "The targetType to convert to cannot be null" );
164
+ Assert .notNull (targetType , "targetType to convert to cannot be null" );
162
165
if (sourceType == null ) {
163
166
return true ;
164
167
}
@@ -169,19 +172,19 @@ public boolean canBypassConvert(TypeDescriptor sourceType, TypeDescriptor target
169
172
@ Override
170
173
@ SuppressWarnings ("unchecked" )
171
174
public <T > T convert (Object source , Class <T > targetType ) {
172
- Assert .notNull (targetType ,"The targetType to convert to cannot be null" );
175
+ Assert .notNull (targetType , " targetType to convert to cannot be null" );
173
176
return (T ) convert (source , TypeDescriptor .forObject (source ), TypeDescriptor .valueOf (targetType ));
174
177
}
175
178
176
179
@ Override
177
180
public Object convert (Object source , TypeDescriptor sourceType , TypeDescriptor targetType ) {
178
- Assert .notNull (targetType ,"The targetType to convert to cannot be null" );
181
+ Assert .notNull (targetType , " targetType to convert to cannot be null" );
179
182
if (sourceType == null ) {
180
- Assert .isTrue (source == null , "The source must be [null] if sourceType == [null]" );
181
- return handleResult (sourceType , targetType , convertNullSource (sourceType , targetType ));
183
+ Assert .isTrue (source == null , "source must be [null] if sourceType == [null]" );
184
+ return handleResult (null , targetType , convertNullSource (null , targetType ));
182
185
}
183
186
if (source != null && !sourceType .getObjectType ().isInstance (source )) {
184
- throw new IllegalArgumentException ("The source to convert from must be an instance of " +
187
+ throw new IllegalArgumentException ("source to convert from must be an instance of " +
185
188
sourceType + "; instead it was a " + source .getClass ().getName ());
186
189
}
187
190
GenericConverter converter = getConverter (sourceType , targetType );
@@ -202,8 +205,8 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
202
205
* @param targetType the target type
203
206
* @return the converted value
204
207
* @throws ConversionException if a conversion exception occurred
205
- * @throws IllegalArgumentException if targetType is null,
206
- * or sourceType is null but source is not null
208
+ * @throws IllegalArgumentException if targetType is {@code null} ,
209
+ * or sourceType is {@code null} but source is not {@code null}
207
210
*/
208
211
public Object convert (Object source , TypeDescriptor targetType ) {
209
212
return convert (source , TypeDescriptor .forObject (source ), targetType );
@@ -218,11 +221,11 @@ public String toString() {
218
221
// Protected template methods
219
222
220
223
/**
221
- * Template method to convert a null source.
222
- * <p>Default implementation returns {@code null} or the Java 8
224
+ * Template method to convert a {@code null} source.
225
+ * <p>The default implementation returns {@code null} or the Java 8
223
226
* {@link java.util.Optional#empty()} instance if the target type is
224
- * {@code java.uti .Optional}.
225
- * Subclasses may override to return custom null objects for specific target types.
227
+ * {@code java.util .Optional}. Subclasses may override this to return
228
+ * custom {@code null} objects for specific target types.
226
229
* @param sourceType the sourceType to convert from
227
230
* @param targetType the targetType to convert to
228
231
* @return the converted null object
@@ -239,11 +242,10 @@ protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor tar
239
242
* First queries this ConversionService's converter cache.
240
243
* On a cache miss, then performs an exhaustive search for a matching converter.
241
244
* If no converter matches, returns the default converter.
242
- * Subclasses may override.
243
245
* @param sourceType the source type to convert from
244
246
* @param targetType the target type to convert to
245
- * @return the generic converter that will perform the conversion, or {@code null} if
246
- * no suitable converter was found
247
+ * @return the generic converter that will perform the conversion,
248
+ * or {@code null} if no suitable converter was found
247
249
* @see #getDefaultConverter(TypeDescriptor, TypeDescriptor)
248
250
*/
249
251
protected GenericConverter getConverter (TypeDescriptor sourceType , TypeDescriptor targetType ) {
@@ -269,9 +271,8 @@ protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescripto
269
271
270
272
/**
271
273
* Return the default converter if no converter is found for the given sourceType/targetType pair.
272
- * Returns a NO_OP Converter if the sourceType is assignable to the targetType.
274
+ * <p> Returns a NO_OP Converter if the sourceType is assignable to the targetType.
273
275
* Returns {@code null} otherwise, indicating no suitable converter could be found.
274
- * Subclasses may override.
275
276
* @param sourceType the source type to convert from
276
277
* @param targetType the target type to convert to
277
278
* @return the default generic converter that will perform the conversion
@@ -280,7 +281,8 @@ protected GenericConverter getDefaultConverter(TypeDescriptor sourceType, TypeDe
280
281
return (sourceType .isAssignableTo (targetType ) ? NO_OP_CONVERTER : null );
281
282
}
282
283
283
- // internal helpers
284
+
285
+ // Internal helpers
284
286
285
287
private ResolvableType [] getRequiredTypeInfo (Object converter , Class <?> genericIfc ) {
286
288
ResolvableType resolvableType = ResolvableType .forClass (converter .getClass ()).as (genericIfc );
@@ -303,7 +305,7 @@ private void invalidateCache() {
303
305
private Object handleConverterNotFound (Object source , TypeDescriptor sourceType , TypeDescriptor targetType ) {
304
306
if (source == null ) {
305
307
assertNotPrimitiveTargetType (sourceType , targetType );
306
- return source ;
308
+ return null ;
307
309
}
308
310
if (sourceType .isAssignableTo (targetType ) && targetType .getObjectType ().isInstance (source )) {
309
311
return source ;
@@ -375,7 +377,7 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
375
377
376
378
@ Override
377
379
public String toString () {
378
- return this .typeInfo + " : " + this .converter ;
380
+ return ( this .typeInfo + " : " + this .converter ) ;
379
381
}
380
382
}
381
383
@@ -425,7 +427,7 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
425
427
426
428
@ Override
427
429
public String toString () {
428
- return this .typeInfo + " : " + this .converterFactory ;
430
+ return ( this .typeInfo + " : " + this .converterFactory ) ;
429
431
}
430
432
}
431
433
@@ -453,20 +455,20 @@ public boolean equals(Object other) {
453
455
return false ;
454
456
}
455
457
ConverterCacheKey otherKey = (ConverterCacheKey ) other ;
456
- return ObjectUtils .nullSafeEquals (this .sourceType , otherKey .sourceType ) &&
457
- ObjectUtils .nullSafeEquals (this .targetType , otherKey .targetType );
458
+ return ( ObjectUtils .nullSafeEquals (this .sourceType , otherKey .sourceType ) &&
459
+ ObjectUtils .nullSafeEquals (this .targetType , otherKey .targetType )) ;
458
460
}
459
461
460
462
@ Override
461
463
public int hashCode () {
462
- return ObjectUtils .nullSafeHashCode (this .sourceType ) * 29 +
463
- ObjectUtils .nullSafeHashCode (this .targetType );
464
+ return ( ObjectUtils .nullSafeHashCode (this .sourceType ) * 29 +
465
+ ObjectUtils .nullSafeHashCode (this .targetType )) ;
464
466
}
465
467
466
468
@ Override
467
469
public String toString () {
468
- return "ConverterCacheKey [sourceType = " + this .sourceType +
469
- ", targetType = " + this .targetType + "]" ;
470
+ return ( "ConverterCacheKey [sourceType = " + this .sourceType +
471
+ ", targetType = " + this .targetType + "]" ) ;
470
472
}
471
473
}
472
474
@@ -544,9 +546,9 @@ private GenericConverter getRegisteredConverter(TypeDescriptor sourceType,
544
546
return converter ;
545
547
}
546
548
}
547
- // Check ConditionalGenericConverter that match all types
549
+ // Check ConditionalConverters for a dynamic match
548
550
for (GenericConverter globalConverter : this .globalConverters ) {
549
- if (((ConditionalConverter )globalConverter ).matches (sourceType , targetType )) {
551
+ if (((ConditionalConverter ) globalConverter ).matches (sourceType , targetType )) {
550
552
return globalConverter ;
551
553
}
552
554
}
@@ -563,6 +565,7 @@ private List<Class<?>> getClassHierarchy(Class<?> type) {
563
565
Set <Class <?>> visited = new HashSet <Class <?>>(20 );
564
566
addToClassHierarchy (0 , ClassUtils .resolvePrimitiveIfNecessary (type ), false , hierarchy , visited );
565
567
boolean array = type .isArray ();
568
+
566
569
int i = 0 ;
567
570
while (i < hierarchy .size ()) {
568
571
Class <?> candidate = hierarchy .get (i );
0 commit comments