Skip to content

Commit 2d7c2d6

Browse files
committed
revised wrapper type handling
1 parent d41344e commit 2d7c2d6

File tree

12 files changed

+111
-119
lines changed

12 files changed

+111
-119
lines changed

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

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,22 @@
3232
*
3333
* @author Keith Donald
3434
* @author Andy Clement
35+
* @author Juergen Hoeller
3536
* @since 3.0
3637
*/
3738
public class TypeDescriptor {
3839

3940
public final static TypeDescriptor NULL = new TypeDescriptor((Class<?>) null);
4041

42+
43+
private Class<?> type;
44+
4145
private MethodParameter methodParameter;
4246

4347
private Field field;
4448

4549
private Annotation[] cachedFieldAnnotations;
4650

47-
private Class<?> type;
48-
4951

5052
/**
5153
* Creates a new descriptor for the given type.
@@ -101,13 +103,13 @@ public Field getField() {
101103
*/
102104
public Class<?> getType() {
103105
if (this.type != null) {
104-
return wrapperType(this.type);
106+
return this.type;
105107
}
106108
else if (this.field != null) {
107-
return wrapperType(this.field.getType());
109+
return this.field.getType();
108110
}
109111
else if (this.methodParameter != null) {
110-
return wrapperType(this.methodParameter.getParameterType());
112+
return this.methodParameter.getParameterType();
111113
}
112114
else {
113115
return null;
@@ -233,9 +235,9 @@ public boolean isAbstractClass() {
233235
/**
234236
* Is the obj an instance of this type?
235237
*/
236-
public boolean isInstance(Object obj) {
238+
public boolean isAssignableValue(Object obj) {
237239
Class<?> type = getType();
238-
return (type != null && getType().isInstance(obj));
240+
return (type != null && ClassUtils.isAssignableValue(getType(), obj));
239241
}
240242

241243
/**
@@ -282,32 +284,6 @@ else if (isMap()) {
282284

283285

284286
// internal helpers
285-
286-
private Class<?> wrapperType(Class<?> type) {
287-
if (type.isPrimitive()) {
288-
if (type.equals(int.class)) {
289-
return Integer.class;
290-
} else if (type.equals(short.class)) {
291-
return Short.class;
292-
} else if (type.equals(long.class)) {
293-
return Long.class;
294-
} else if (type.equals(float.class)) {
295-
return Float.class;
296-
} else if (type.equals(double.class)) {
297-
return Double.class;
298-
} else if (type.equals(byte.class)) {
299-
return Byte.class;
300-
} else if (type.equals(boolean.class)) {
301-
return Boolean.class;
302-
} else if (type.equals(char.class)) {
303-
return Character.class;
304-
} else {
305-
throw new IllegalStateException("Should never happen - primitive type is not a primitive?");
306-
}
307-
} else {
308-
return type;
309-
}
310-
}
311287

312288
private Class<?> getArrayComponentType() {
313289
return getType().getComponentType();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.core.convert.support;
1718

1819
import java.lang.reflect.Array;
@@ -23,7 +24,9 @@
2324
* Special one-way converter that converts from a source array to a target array. Supports type conversion of the
2425
* individual array elements; for example, the ability to convert a String[] to an Integer[]. Mainly used internally by
2526
* {@link org.springframework.core.convert.ConversionService} implementations.
27+
*
2628
* @author Keith Donald
29+
* @author Juergen Hoeller
2730
* @since 3.0
2831
*/
2932
@SuppressWarnings("unchecked")

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* specified. Supports type conversion of array elements when a parameterized target collection type descriptor is provided.
2929
*
3030
* @author Keith Donald
31+
* @author Juergen Hoeller
3132
* @since 3.0
3233
*/
3334
class ArrayToCollection extends AbstractCollectionConverter {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
/**
2626
* Special converter that converts from target collection to a source array.
27+
*
2728
* @author Keith Donald
29+
* @author Juergen Hoeller
2830
* @since 3.0
2931
*/
3032
class CollectionToArray extends AbstractCollectionConverter {
@@ -49,16 +51,15 @@ protected Object doExecute(Object source) throws Exception {
4951
private ConversionExecutor getElementConverter(Collection<?> source) {
5052
ConversionExecutor elementConverter = getElementConverter();
5153
if (elementConverter == NoOpConversionExecutor.INSTANCE) {
52-
Iterator<?> it = source.iterator();
53-
while (it.hasNext()) {
54-
Object value = it.next();
54+
for (Object value : source) {
5555
if (value != null) {
56-
elementConverter = getConversionService().getConversionExecutor(value.getClass(), TypeDescriptor.valueOf(getTargetElementType()));
56+
elementConverter = getConversionService()
57+
.getConversionExecutor(value.getClass(), TypeDescriptor.valueOf(getTargetElementType()));
5758
break;
5859
}
5960
}
6061
}
61-
return elementConverter != null ? elementConverter : NoOpConversionExecutor.INSTANCE;
62+
return (elementConverter != null ? elementConverter : NoOpConversionExecutor.INSTANCE);
6263
}
6364

64-
}
65+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
/**
2525
* A converter that can convert from one collection type to another.
26+
*
2627
* @author Keith Donald
28+
* @author Juergen Hoeller
2729
* @since 3.0
2830
*/
2931
@SuppressWarnings("unchecked")

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.core.convert.converter.ConverterInfo;
3636
import org.springframework.core.convert.converter.ConverterRegistry;
3737
import org.springframework.util.Assert;
38+
import org.springframework.util.ClassUtils;
3839

3940
/**
4041
* Base implementation of a conversion service.
@@ -118,17 +119,7 @@ public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
118119

119120
public boolean canConvert(Class<?> sourceType, TypeDescriptor targetType) {
120121
ConversionExecutor executor = getConversionExecutor(sourceType, targetType);
121-
if (executor != null) {
122-
return true;
123-
}
124-
else {
125-
if (parent != null) {
126-
return parent.canConvert(sourceType, targetType);
127-
}
128-
else {
129-
return false;
130-
}
131-
}
122+
return (executor != null || (this.parent != null && this.parent.canConvert(sourceType, targetType)));
132123
}
133124

134125
@SuppressWarnings("unchecked")
@@ -144,15 +135,13 @@ public Object convert(Object source, TypeDescriptor targetType) {
144135
if (executor != null) {
145136
return executor.execute(source);
146137
}
138+
else if (this.parent != null) {
139+
return this.parent.convert(source, targetType);
140+
}
147141
else {
148-
if (this.parent != null) {
149-
return this.parent.convert(source, targetType);
150-
}
151-
else {
152-
throw new ConverterNotFoundException(source.getClass(), targetType.getType(),
153-
"No converter found that can convert from sourceType [" + source.getClass().getName()
154-
+ "] to targetType [" + targetType.getName() + "]");
155-
}
142+
throw new ConverterNotFoundException(source.getClass(), targetType.getType(),
143+
"No converter found that can convert from sourceType [" + source.getClass().getName()
144+
+ "] to targetType [" + targetType.getName() + "]");
156145
}
157146
}
158147

@@ -279,7 +268,7 @@ else if (targetType.isCollection()) {
279268
if (sourceType.isAssignableTo(targetType)) {
280269
return NoOpConversionExecutor.INSTANCE;
281270
}
282-
Converter converter = findRegisteredConverter(sourceType.getType(), targetType.getType());
271+
Converter converter = findRegisteredConverter(ClassUtils.resolvePrimitiveIfNecessary(sourceType.getType()), ClassUtils.resolvePrimitiveIfNecessary(targetType.getType()));
283272
if (converter != null) {
284273
return new StaticConversionExecutor(sourceType, targetType, converter);
285274
}
@@ -361,7 +350,6 @@ private Converter findRegisteredConverter(Class<?> sourceType, Class<?> targetTy
361350
while (!classQueue.isEmpty()) {
362351
Class currentClass = classQueue.removeLast();
363352
Map<Class, Object> converters = getConvertersForSource(currentClass);
364-
System.out.println("Source:" + currentClass);
365353
Converter converter = getConverter(converters, targetType);
366354
if (converter != null) {
367355
return converter;

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* Default conversion executor implementation for converters.
2525
*
2626
* @author Keith Donald
27+
* @author Juergen Hoeller
2728
* @since 3.0
2829
*/
2930
class StaticConversionExecutor implements ConversionExecutor {
@@ -47,15 +48,15 @@ public Object execute(Object source) throws ConversionFailedException {
4748
if (source == null) {
4849
return null;
4950
}
50-
if (!this.sourceType.isInstance(source)) {
51-
throw new ConversionFailedException(source, sourceType.getType(), targetType.getType(), "Source object "
52-
+ source + " to convert is expected to be an instance of [" + sourceType.getName() + "]");
51+
if (!this.sourceType.isAssignableValue(source)) {
52+
throw new ConversionFailedException(source, this.sourceType.getType(), this.targetType.getType(),
53+
"Source object " + source + " to convert is expected to be an instance of [" + this.sourceType.getName() + "]");
5354
}
5455
try {
5556
return this.converter.convert(source);
5657
}
5758
catch (Exception ex) {
58-
throw new ConversionFailedException(source, sourceType.getType(), targetType.getType(), ex);
59+
throw new ConversionFailedException(source, this.sourceType.getType(), this.targetType.getType(), ex);
5960
}
6061
}
6162

org.springframework.core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009 the original author or authors.
2+
* Copyright 2002-2009 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.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.core.convert;
1718

1819
import static junit.framework.Assert.assertEquals;
@@ -32,12 +33,6 @@ public class TypeDescriptorTests {
3233
int[] intArray;
3334
List<String>[] arrayOfListOfString;
3435

35-
@Test
36-
public void testWrapperType() {
37-
TypeDescriptor desc = TypeDescriptor.valueOf(int.class);
38-
assertEquals(Integer.class, desc.getType());
39-
}
40-
4136
@Test
4237
public void listDescriptors() throws Exception {
4338
TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("listOfString"));

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void convertArrayToArray() {
136136
@Test
137137
public void convertArrayToPrimitiveArray() {
138138
converter.add(new StringToInteger());
139-
int[] result = (int[]) converter.convert(new String[] { "1", "2", "3" }, int[].class);
139+
int[] result = converter.convert(new String[] { "1", "2", "3" }, int[].class);
140140
assertEquals(1, result[0]);
141141
assertEquals(2, result[1]);
142142
assertEquals(3, result[2]);
@@ -184,7 +184,7 @@ public void convertListToArray() {
184184
list.add("1");
185185
list.add("2");
186186
list.add("3");
187-
String[] result = (String[]) converter.convert(list, String[].class);
187+
String[] result = converter.convert(list, String[].class);
188188
assertEquals("1", result[0]);
189189
assertEquals("2", result[1]);
190190
assertEquals("3", result[2]);
@@ -229,15 +229,17 @@ public void convertStringToArray() {
229229
@Test
230230
public void convertStringToArrayWithElementConversion() {
231231
converter.add(new StringToInteger());
232-
Integer[] result = (Integer[]) converter.convert("1,2,3", Integer[].class);
232+
Integer[] result = converter.convert("1,2,3", Integer[].class);
233233
assertEquals(3, result.length);
234234
assertEquals(new Integer(1), result[0]);
235235
assertEquals(new Integer(2), result[1]);
236236
assertEquals(new Integer(3), result[2]);
237237
}
238238

239+
239240
public static enum FooEnum {
241+
240242
BAR, BAZ
241243
}
242244

243-
}
245+
}

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.expression;
1718

1819
import org.springframework.core.convert.TypeDescriptor;
@@ -21,19 +22,22 @@
2122
* Encapsulates an object and a type descriptor that describes it.
2223
* The type descriptor can hold generic information that would
2324
* not be accessible through a simple getClass() call on the object.
24-
*
25+
*
2526
* @author Andy Clement
2627
* @since 3.0
2728
*/
2829
public class TypedValue {
2930

30-
private Object value;
31-
private TypeDescriptor typeDescriptor;
32-
3331
public static final TypedValue NULL_TYPED_VALUE = new TypedValue(null, TypeDescriptor.NULL);
3432

33+
34+
private final Object value;
35+
36+
private final TypeDescriptor typeDescriptor;
37+
38+
3539
/**
36-
* Create a TypedValue for a simple object. The type descriptor is inferred
40+
* Create a TypedValue for a simple object. The type descriptor is inferred
3741
* from the object, so no generic information is preserved.
3842
* @param value the object value
3943
*/
@@ -52,6 +56,7 @@ public TypedValue(Object value, TypeDescriptor typeDescriptor) {
5256
this.typeDescriptor = typeDescriptor;
5357
}
5458

59+
5560
public Object getValue() {
5661
return this.value;
5762
}
@@ -60,10 +65,12 @@ public TypeDescriptor getTypeDescriptor() {
6065
return this.typeDescriptor;
6166
}
6267

68+
6369
@Override
6470
public String toString() {
65-
StringBuffer str = new StringBuffer();
66-
str.append("TypedValue: ").append(value).append(" of type "+typeDescriptor.asString());
71+
StringBuilder str = new StringBuilder();
72+
str.append("TypedValue: ").append(this.value).append(" of type ").append(this.typeDescriptor.asString());
6773
return str.toString();
6874
}
75+
6976
}

0 commit comments

Comments
 (0)