Skip to content

Commit ca09eb2

Browse files
committed
Fixed addConverterFactory assertion
(cherry picked from commit 22670b7)
1 parent 13a8745 commit ca09eb2

File tree

2 files changed

+54
-52
lines changed

2 files changed

+54
-52
lines changed

spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -92,7 +92,7 @@ public class GenericConversionService implements ConfigurableConversionService {
9292
new ConcurrentHashMap<ConverterCacheKey, GenericConverter>(64);
9393

9494

95-
// implementing ConverterRegistry
95+
// ConverterRegistry implementation
9696

9797
@Override
9898
public void addConverter(Converter<?, ?> converter) {
@@ -104,7 +104,8 @@ public void addConverter(Converter<?, ?> converter) {
104104

105105
@Override
106106
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)));
108109
}
109110

110111
@Override
@@ -116,7 +117,7 @@ public void addConverter(GenericConverter converter) {
116117
@Override
117118
public void addConverterFactory(ConverterFactory<?, ?> converterFactory) {
118119
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 " +
120121
"ConverterFactory<S, R> converts between; declare these generic types.");
121122
addConverter(new ConverterFactoryAdapter(converterFactory,
122123
new ConvertiblePair(typeInfo[0].resolve(), typeInfo[1].resolve())));
@@ -128,7 +129,8 @@ public void removeConvertible(Class<?> sourceType, Class<?> targetType) {
128129
invalidateCache();
129130
}
130131

131-
// implementing ConversionService
132+
133+
// ConversionService implementation
132134

133135
@Override
134136
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
@@ -148,17 +150,18 @@ public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType)
148150
}
149151

150152
/**
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
153155
* 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})
155158
* @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}
158161
* @since 3.2
159162
*/
160163
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");
162165
if (sourceType == null) {
163166
return true;
164167
}
@@ -169,19 +172,19 @@ public boolean canBypassConvert(TypeDescriptor sourceType, TypeDescriptor target
169172
@Override
170173
@SuppressWarnings("unchecked")
171174
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");
173176
return (T) convert(source, TypeDescriptor.forObject(source), TypeDescriptor.valueOf(targetType));
174177
}
175178

176179
@Override
177180
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");
179182
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));
182185
}
183186
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 " +
185188
sourceType + "; instead it was a " + source.getClass().getName());
186189
}
187190
GenericConverter converter = getConverter(sourceType, targetType);
@@ -202,8 +205,8 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
202205
* @param targetType the target type
203206
* @return the converted value
204207
* @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}
207210
*/
208211
public Object convert(Object source, TypeDescriptor targetType) {
209212
return convert(source, TypeDescriptor.forObject(source), targetType);
@@ -218,11 +221,11 @@ public String toString() {
218221
// Protected template methods
219222

220223
/**
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
223226
* {@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.
226229
* @param sourceType the sourceType to convert from
227230
* @param targetType the targetType to convert to
228231
* @return the converted null object
@@ -239,11 +242,10 @@ protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor tar
239242
* First queries this ConversionService's converter cache.
240243
* On a cache miss, then performs an exhaustive search for a matching converter.
241244
* If no converter matches, returns the default converter.
242-
* Subclasses may override.
243245
* @param sourceType the source type to convert from
244246
* @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
247249
* @see #getDefaultConverter(TypeDescriptor, TypeDescriptor)
248250
*/
249251
protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
@@ -269,9 +271,8 @@ protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescripto
269271

270272
/**
271273
* 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.
273275
* Returns {@code null} otherwise, indicating no suitable converter could be found.
274-
* Subclasses may override.
275276
* @param sourceType the source type to convert from
276277
* @param targetType the target type to convert to
277278
* @return the default generic converter that will perform the conversion
@@ -280,7 +281,8 @@ protected GenericConverter getDefaultConverter(TypeDescriptor sourceType, TypeDe
280281
return (sourceType.isAssignableTo(targetType) ? NO_OP_CONVERTER : null);
281282
}
282283

283-
// internal helpers
284+
285+
// Internal helpers
284286

285287
private ResolvableType[] getRequiredTypeInfo(Object converter, Class<?> genericIfc) {
286288
ResolvableType resolvableType = ResolvableType.forClass(converter.getClass()).as(genericIfc);
@@ -303,7 +305,7 @@ private void invalidateCache() {
303305
private Object handleConverterNotFound(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
304306
if (source == null) {
305307
assertNotPrimitiveTargetType(sourceType, targetType);
306-
return source;
308+
return null;
307309
}
308310
if (sourceType.isAssignableTo(targetType) && targetType.getObjectType().isInstance(source)) {
309311
return source;
@@ -375,7 +377,7 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
375377

376378
@Override
377379
public String toString() {
378-
return this.typeInfo + " : " + this.converter;
380+
return (this.typeInfo + " : " + this.converter);
379381
}
380382
}
381383

@@ -425,7 +427,7 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
425427

426428
@Override
427429
public String toString() {
428-
return this.typeInfo + " : " + this.converterFactory;
430+
return (this.typeInfo + " : " + this.converterFactory);
429431
}
430432
}
431433

@@ -453,20 +455,20 @@ public boolean equals(Object other) {
453455
return false;
454456
}
455457
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));
458460
}
459461

460462
@Override
461463
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));
464466
}
465467

466468
@Override
467469
public String toString() {
468-
return "ConverterCacheKey [sourceType = " + this.sourceType +
469-
", targetType = " + this.targetType + "]";
470+
return ("ConverterCacheKey [sourceType = " + this.sourceType +
471+
", targetType = " + this.targetType + "]");
470472
}
471473
}
472474

@@ -544,9 +546,9 @@ private GenericConverter getRegisteredConverter(TypeDescriptor sourceType,
544546
return converter;
545547
}
546548
}
547-
// Check ConditionalGenericConverter that match all types
549+
// Check ConditionalConverters for a dynamic match
548550
for (GenericConverter globalConverter : this.globalConverters) {
549-
if (((ConditionalConverter)globalConverter).matches(sourceType, targetType)) {
551+
if (((ConditionalConverter) globalConverter).matches(sourceType, targetType)) {
550552
return globalConverter;
551553
}
552554
}
@@ -563,6 +565,7 @@ private List<Class<?>> getClassHierarchy(Class<?> type) {
563565
Set<Class<?>> visited = new HashSet<Class<?>>(20);
564566
addToClassHierarchy(0, ClassUtils.resolvePrimitiveIfNecessary(type), false, hierarchy, visited);
565567
boolean array = type.isArray();
568+
566569
int i = 0;
567570
while (i < hierarchy.size()) {
568571
Class<?> candidate = hierarchy.get(i);

spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -113,17 +113,17 @@ public void convertNullSource() {
113113
assertEquals(null, conversionService.convert(null, Integer.class));
114114
}
115115

116-
@Test(expected=ConversionFailedException.class)
116+
@Test(expected = ConversionFailedException.class)
117117
public void convertNullSourcePrimitiveTarget() {
118118
assertEquals(null, conversionService.convert(null, int.class));
119119
}
120120

121-
@Test(expected=ConversionFailedException.class)
121+
@Test(expected = ConversionFailedException.class)
122122
public void convertNullSourcePrimitiveTargetTypeDescriptor() {
123123
conversionService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(int.class));
124124
}
125125

126-
@Test(expected=IllegalArgumentException.class)
126+
@Test(expected = IllegalArgumentException.class)
127127
public void convertNotNullSourceNullSourceTypeDescriptor() {
128128
conversionService.convert("3", null, TypeDescriptor.valueOf(int.class));
129129
}
@@ -177,18 +177,18 @@ public void convertNull() {
177177
assertNull(conversionService.convert(null, Integer.class));
178178
}
179179

180-
@Test(expected=IllegalArgumentException.class)
180+
@Test(expected = IllegalArgumentException.class)
181181
public void convertNullTargetClass() {
182182
assertNull(conversionService.convert("3", (Class<?>) null));
183183
assertNull(conversionService.convert("3", TypeDescriptor.valueOf(String.class), null));
184184
}
185185

186-
@Test(expected=IllegalArgumentException.class)
186+
@Test(expected = IllegalArgumentException.class)
187187
public void convertNullTypeDescriptor() {
188188
assertNull(conversionService.convert("3", TypeDescriptor.valueOf(String.class), null));
189189
}
190190

191-
@Test(expected=IllegalArgumentException.class)
191+
@Test(expected = IllegalArgumentException.class)
192192
public void convertWrongSourceTypeDescriptor() {
193193
conversionService.convert("3", TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(Long.class));
194194
}
@@ -218,8 +218,7 @@ public Integer convert(CharSequence source) {
218218
}
219219

220220
// SPR-8718
221-
222-
@Test(expected=ConverterNotFoundException.class)
221+
@Test(expected = ConverterNotFoundException.class)
223222
public void convertSuperTarget() {
224223
conversionService.addConverter(new ColorConverter());
225224
conversionService.convert("#000000", SystemColor.class);
@@ -295,7 +294,7 @@ public void testMapToObjectConversion() {
295294
@Test
296295
public void testInterfaceToString() {
297296
GenericConversionService conversionService = new GenericConversionService();
298-
conversionService.addConverter(new MyBaseInterfaceConverter());
297+
conversionService.addConverter(new MyBaseInterfaceToStringConverter());
299298
conversionService.addConverter(new ObjectToStringConverter());
300299
Object converted = conversionService.convert(new MyInterfaceImplementer(), String.class);
301300
assertEquals("RESULT", converted);
@@ -304,7 +303,7 @@ public void testInterfaceToString() {
304303
@Test
305304
public void testInterfaceArrayToStringArray() {
306305
GenericConversionService conversionService = new GenericConversionService();
307-
conversionService.addConverter(new MyBaseInterfaceConverter());
306+
conversionService.addConverter(new MyBaseInterfaceToStringConverter());
308307
conversionService.addConverter(new ArrayToArrayConverter(conversionService));
309308
String[] converted = conversionService.convert(new MyInterface[] {new MyInterfaceImplementer()}, String[].class);
310309
assertEquals("RESULT", converted[0]);
@@ -313,7 +312,7 @@ public void testInterfaceArrayToStringArray() {
313312
@Test
314313
public void testObjectArrayToStringArray() {
315314
GenericConversionService conversionService = new GenericConversionService();
316-
conversionService.addConverter(new MyBaseInterfaceConverter());
315+
conversionService.addConverter(new MyBaseInterfaceToStringConverter());
317316
conversionService.addConverter(new ArrayToArrayConverter(conversionService));
318317
String[] converted = conversionService.convert(new MyInterfaceImplementer[] {new MyInterfaceImplementer()}, String[].class);
319318
assertEquals("RESULT", converted[0]);
@@ -813,7 +812,7 @@ private static class MyInterfaceImplementer implements MyInterface {
813812
}
814813

815814

816-
private static class MyBaseInterfaceConverter implements Converter<MyBaseInterface, String> {
815+
private static class MyBaseInterfaceToStringConverter implements Converter<MyBaseInterface, String> {
817816

818817
@Override
819818
public String convert(MyBaseInterface source) {

0 commit comments

Comments
 (0)