Skip to content

Commit cd584af

Browse files
committed
removed ConversionService/TypeConverter convenience methods in order to restore 3.0's SPI (for backwards compatibility with implementers)
1 parent b7d7fa7 commit cd584af

File tree

13 files changed

+97
-87
lines changed

13 files changed

+97
-87
lines changed

org.springframework.core/src/main/java/org/springframework/core/convert/ConversionService.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -64,10 +64,4 @@ public interface ConversionService {
6464
*/
6565
Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);
6666

67-
// 3.1 additions that encapsulate TypeDescriptor.forObject(source)
68-
69-
boolean canConvert(Object source, TypeDescriptor targetType);
70-
71-
Object convert(Object source, TypeDescriptor targetType);
72-
7367
}

org.springframework.core/src/main/java/org/springframework/core/convert/support/ConvertingPropertyEditorAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -56,7 +56,7 @@ public ConvertingPropertyEditorAdapter(ConversionService conversionService, Type
5656

5757
@Override
5858
public void setAsText(String text) throws IllegalArgumentException {
59-
setValue(this.conversionService.convert(text, this.targetDescriptor));
59+
setValue(this.conversionService.convert(text, TypeDescriptor.valueOf(String.class), this.targetDescriptor));
6060
}
6161

6262
@Override

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -188,13 +188,6 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
188188
return result;
189189
}
190190

191-
public boolean canConvert(Object source, TypeDescriptor targetType) {
192-
return canConvert(TypeDescriptor.forObject(source), targetType);
193-
}
194-
195-
public Object convert(Object source, TypeDescriptor targetType) {
196-
return convert(source, TypeDescriptor.forObject(source), targetType);
197-
}
198191

199192
public String toString() {
200193
List<String> converterStrings = new ArrayList<String>();

org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@
1616

1717
package org.springframework.core.convert.support;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertNull;
22-
import static org.junit.Assert.assertSame;
23-
import static org.junit.Assert.assertTrue;
24-
25-
import java.awt.Color;
19+
import java.awt.*;
2620
import java.math.BigDecimal;
2721
import java.math.BigInteger;
2822
import java.util.AbstractList;
@@ -40,7 +34,9 @@
4034
import java.util.Properties;
4135
import java.util.Set;
4236

37+
import static org.junit.Assert.*;
4338
import org.junit.Test;
39+
4440
import org.springframework.core.MethodParameter;
4541
import org.springframework.core.convert.ConversionFailedException;
4642
import org.springframework.core.convert.ConversionService;
@@ -287,7 +283,8 @@ public void convertArrayToCollectionInterface() {
287283

288284
@Test
289285
public void convertArrayToCollectionGenericTypeConversion() throws Exception {
290-
List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, new TypeDescriptor(getClass().getDeclaredField("genericList")));
286+
List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, TypeDescriptor
287+
.valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList")));
291288
assertEquals(new Integer("1"), result.get(0));
292289
assertEquals(new Integer("2"), result.get(1));
293290
assertEquals(new Integer("3"), result.get(2));
@@ -297,7 +294,7 @@ public void convertArrayToCollectionGenericTypeConversion() throws Exception {
297294
public void testSpr7766() throws Exception {
298295
ConverterRegistry registry = ((ConverterRegistry) conversionService);
299296
registry.addConverter(new ColorConverter());
300-
List<Color> colors = (List<Color>) conversionService.convert(new String[] { "ffffff", "#000000" }, new TypeDescriptor(new MethodParameter(getClass().getMethod("handlerMethod", List.class), 0)));
297+
List<Color> colors = (List<Color>) conversionService.convert(new String[] { "ffffff", "#000000" }, TypeDescriptor.valueOf(String[].class), new TypeDescriptor(new MethodParameter(getClass().getMethod("handlerMethod", List.class), 0)));
301298
assertEquals(2, colors.size());
302299
assertEquals(Color.WHITE, colors.get(0));
303300
assertEquals(Color.BLACK, colors.get(1));
@@ -457,7 +454,8 @@ public void convertStringToCollection() {
457454

458455
@Test
459456
public void convertStringToCollectionWithElementConversion() throws Exception {
460-
List result = (List) conversionService.convert("1,2,3", new TypeDescriptor(getClass().getField("genericList")));
457+
List result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class),
458+
new TypeDescriptor(getClass().getField("genericList")));
461459
assertEquals(3, result.size());
462460
assertEquals(new Integer(1), result.get(0));
463461
assertEquals(new Integer(2), result.get(1));
@@ -493,7 +491,8 @@ public void convertObjectToCollection() {
493491

494492
@Test
495493
public void convertObjectToCollectionWithElementConversion() throws Exception {
496-
List<Integer> result = (List<Integer>) conversionService.convert(3L, new TypeDescriptor(getClass().getField("genericList")));
494+
List<Integer> result = (List<Integer>) conversionService.convert(3L, TypeDescriptor.valueOf(Long.class),
495+
new TypeDescriptor(getClass().getField("genericList")));
497496
assertEquals(1, result.size());
498497
assertEquals(new Integer(3), result.get(0));
499498
}
@@ -528,15 +527,17 @@ public void convertCollectionToCollection() throws Exception {
528527
foo.add("1");
529528
foo.add("2");
530529
foo.add("3");
531-
List<Integer> bar = (List<Integer>) conversionService.convert(foo, new TypeDescriptor(getClass().getField("genericList")));
530+
List<Integer> bar = (List<Integer>) conversionService.convert(foo, TypeDescriptor.forObject(foo),
531+
new TypeDescriptor(getClass().getField("genericList")));
532532
assertEquals(new Integer(1), bar.get(0));
533533
assertEquals(new Integer(2), bar.get(1));
534534
assertEquals(new Integer(3), bar.get(2));
535535
}
536536

537537
@Test
538538
public void convertCollectionToCollectionNull() throws Exception {
539-
List<Integer> bar = (List<Integer>) conversionService.convert(null, new TypeDescriptor(getClass().getField("genericList")));
539+
List<Integer> bar = (List<Integer>) conversionService.convert(null,
540+
TypeDescriptor.valueOf(LinkedHashSet.class), new TypeDescriptor(getClass().getField("genericList")));
540541
assertNull(bar);
541542
}
542543

@@ -546,7 +547,8 @@ public void convertCollectionToCollectionNotGeneric() throws Exception {
546547
foo.add("1");
547548
foo.add("2");
548549
foo.add("3");
549-
List bar = (List) conversionService.convert(foo, TypeDescriptor.valueOf(List.class));
550+
List bar = (List) conversionService.convert(foo, TypeDescriptor.valueOf(LinkedHashSet.class), TypeDescriptor
551+
.valueOf(List.class));
550552
assertEquals("1", bar.get(0));
551553
assertEquals("2", bar.get(1));
552554
assertEquals("3", bar.get(2));
@@ -559,7 +561,8 @@ public void convertCollectionToCollectionSpecialCaseSourceImpl() throws Exceptio
559561
map.put("2", "2");
560562
map.put("3", "3");
561563
Collection values = map.values();
562-
List<Integer> bar = (List<Integer>) conversionService.convert(values, new TypeDescriptor(getClass().getField("genericList")));
564+
List<Integer> bar = (List<Integer>) conversionService.convert(values,
565+
TypeDescriptor.forObject(values), new TypeDescriptor(getClass().getField("genericList")));
563566
assertEquals(3, bar.size());
564567
assertEquals(new Integer(1), bar.get(0));
565568
assertEquals(new Integer(2), bar.get(1));
@@ -573,7 +576,8 @@ public void convertMapToMap() throws Exception {
573576
Map<String, String> foo = new HashMap<String, String>();
574577
foo.put("1", "BAR");
575578
foo.put("2", "BAZ");
576-
Map<String, FooEnum> map = (Map<String, FooEnum>) conversionService.convert(foo, new TypeDescriptor(getClass().getField("genericMap")));
579+
Map<String, FooEnum> map = (Map<String, FooEnum>) conversionService.convert(foo,
580+
TypeDescriptor.forObject(foo), new TypeDescriptor(getClass().getField("genericMap")));
577581
assertEquals(FooEnum.BAR, map.get(1));
578582
assertEquals(FooEnum.BAZ, map.get(2));
579583
}

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@
1616

1717
package org.springframework.core.convert.support;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertNull;
23-
import static org.junit.Assert.assertSame;
24-
import static org.junit.Assert.assertTrue;
25-
import static org.junit.Assert.fail;
26-
2719
import java.util.ArrayList;
2820
import java.util.Collections;
2921
import java.util.HashMap;
@@ -32,7 +24,9 @@
3224
import java.util.List;
3325
import java.util.Map;
3426

27+
import static org.junit.Assert.*;
3528
import org.junit.Test;
29+
3630
import org.springframework.core.convert.ConversionFailedException;
3731
import org.springframework.core.convert.ConverterNotFoundException;
3832
import org.springframework.core.convert.TypeDescriptor;
@@ -111,12 +105,12 @@ public void convertNull() {
111105

112106
public void convertNullTargetClass() {
113107
assertNull(conversionService.convert("3", (Class<?>) null));
114-
assertNull(conversionService.convert("3", TypeDescriptor.NULL));
108+
assertNull(conversionService.convert("3", TypeDescriptor.valueOf(String.class), TypeDescriptor.NULL));
115109
}
116110

117111
@Test
118112
public void convertNullTypeDescriptor() {
119-
assertNull(conversionService.convert("3", TypeDescriptor.NULL));
113+
assertNull(conversionService.convert("3", TypeDescriptor.valueOf(String.class), TypeDescriptor.NULL));
120114
}
121115

122116
@Test
@@ -150,7 +144,7 @@ public void convertObjectToPrimitive() {
150144
Boolean b = conversionService.convert("true", boolean.class);
151145
assertEquals(Boolean.TRUE, b);
152146
assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(boolean.class)));
153-
b = (Boolean) conversionService.convert("true", TypeDescriptor.valueOf(boolean.class));
147+
b = (Boolean) conversionService.convert("true", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(boolean.class));
154148
assertEquals(Boolean.TRUE, b);
155149
}
156150

@@ -260,7 +254,7 @@ public void testWildcardMap() throws Exception {
260254
GenericConversionService conversionService = new DefaultConversionService();
261255
Map<String, String> input = new LinkedHashMap<String, String>();
262256
input.put("key", "value");
263-
Object converted = conversionService.convert(input, new TypeDescriptor(getClass().getField("wildcardMap")));
257+
Object converted = conversionService.convert(input, TypeDescriptor.forObject(input), new TypeDescriptor(getClass().getField("wildcardMap")));
264258
assertEquals(input, converted);
265259
}
266260

@@ -323,7 +317,7 @@ public void testPerformance2() throws Exception {
323317
source.add("3");
324318
TypeDescriptor td = new TypeDescriptor(getClass().getField("list"));
325319
for (int i = 0; i < 1000000; i++) {
326-
conversionService.convert(source, td);
320+
conversionService.convert(source, TypeDescriptor.forObject(source), td);
327321
}
328322
watch.stop();
329323
watch.start("convert 4,000,000 manually");
@@ -350,7 +344,7 @@ public void testPerformance3() throws Exception {
350344
source.put("3", "3");
351345
TypeDescriptor td = new TypeDescriptor(getClass().getField("map"));
352346
for (int i = 0; i < 1000000; i++) {
353-
conversionService.convert(source, td);
347+
conversionService.convert(source, TypeDescriptor.forObject(source), td);
354348
}
355349
watch.stop();
356350
watch.start("convert 4,000,000 manually");

org.springframework.expression/src/main/java/org/springframework/expression/TypeConverter.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -41,7 +41,7 @@ public interface TypeConverter {
4141
/**
4242
* Convert (may coerce) a value from one type to another, for example from a boolean to a string.
4343
* The typeDescriptor parameter enables support for typed collections - if the caller really wishes they
44-
* can have a List<Integer> for example, rather than simply a List.
44+
* can have a List&lt;Integer&gt; for example, rather than simply a List.
4545
* @param value the value to be converted
4646
* @param sourceType a type descriptor that supplies extra information about the source object
4747
* @param targetType a type descriptor that supplies extra information about the requested result type
@@ -50,8 +50,4 @@ public interface TypeConverter {
5050
*/
5151
Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType);
5252

53-
// 3.1 additions for encapsulation of TypeDescriptor.forObject(value);
54-
55-
Object convertValue(Object value, TypeDescriptor targetType);
56-
5753
}

org.springframework.expression/src/main/java/org/springframework/expression/spel/ExpressionState.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -141,7 +141,8 @@ public Object convertValue(Object value, TypeDescriptor targetTypeDescriptor) th
141141
}
142142

143143
public Object convertValue(TypedValue value, TypeDescriptor targetTypeDescriptor) throws EvaluationException {
144-
return this.relatedContext.getTypeConverter().convertValue(value.getValue(), targetTypeDescriptor);
144+
Object val = value.getValue();
145+
return this.relatedContext.getTypeConverter().convertValue(val, TypeDescriptor.forObject(val), targetTypeDescriptor);
145146
}
146147

147148
/*

org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -35,8 +35,9 @@
3535
import org.springframework.expression.spel.SpelNode;
3636

3737
/**
38-
* Represents the invocation of a constructor. Either a constructor on a regular type or construction of an array. When
39-
* an array is constructed, an initializer can be specified.
38+
* Represents the invocation of a constructor. Either a constructor on a regular type or
39+
* construction of an array. When an array is constructed, an initializer can be specified.
40+
*
4041
* <p>
4142
* Examples:<br>
4243
* new String('hello world')<br>
@@ -322,7 +323,7 @@ private void populateReferenceTypeArray(ExpressionState state, Object newArray,
322323
for (int i = 0; i < newObjectArray.length; i++) {
323324
SpelNode elementNode = initializer.getChild(i);
324325
Object arrayEntry = elementNode.getValue(state);
325-
newObjectArray[i] = typeConverter.convertValue(arrayEntry, toTypeDescriptor);
326+
newObjectArray[i] = typeConverter.convertValue(arrayEntry, TypeDescriptor.forObject(arrayEntry), toTypeDescriptor);
326327
}
327328
}
328329

org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -241,7 +241,8 @@ static void convertArguments(TypeConverter converter, Object[] arguments, Object
241241
else {
242242
targetType = new TypeDescriptor(MethodParameter.forMethodOrConstructor(methodOrCtor, argPosition));
243243
}
244-
arguments[argPosition] = converter.convertValue(arguments[argPosition], targetType);
244+
Object argument = arguments[argPosition];
245+
arguments[argPosition] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
245246
}
246247
}
247248

@@ -278,7 +279,7 @@ public static void convertAllArguments(TypeConverter converter, Object[] argumen
278279
if (converter == null) {
279280
throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, argument.getClass().getName(), targetType);
280281
}
281-
arguments[argPosition] = converter.convertValue(argument, targetType);
282+
arguments[argPosition] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
282283
}
283284
}
284285
catch (EvaluationException ex) {

org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyAccessor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -218,7 +218,8 @@ public void write(EvaluationContext context, Object target, String name, Object
218218
TypeDescriptor typeDescriptor = getTypeDescriptor(context, target, name);
219219
if (typeDescriptor != null) {
220220
try {
221-
possiblyConvertedNewValue = context.getTypeConverter().convertValue(newValue, typeDescriptor);
221+
possiblyConvertedNewValue = context.getTypeConverter().convertValue(
222+
newValue, TypeDescriptor.forObject(newValue), typeDescriptor);
222223
}
223224
catch (EvaluationException evaluationException) {
224225
throw new AccessException("Type conversion failure",evaluationException);

0 commit comments

Comments
 (0)