TypeDescriptor.valueOf usage in favor of constants; TypedValue usage simplification
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.core.convert;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.GenericCollectionTypeResolver;
|
||||
@@ -39,13 +40,20 @@ public class TypeDescriptor {
|
||||
/** Constant defining an 'unknown' TypeDescriptor */
|
||||
public static final TypeDescriptor NULL = new TypeDescriptor();
|
||||
|
||||
/** Constant defining a TypeDescriptor for <code>java.lang.Object</code> */
|
||||
public static final TypeDescriptor OBJECT = new TypeDescriptor(Object.class);
|
||||
|
||||
/** Constant defining a TypeDescriptor for <code>java.lang.String</code> */
|
||||
public static final TypeDescriptor STRING = new TypeDescriptor(String.class);
|
||||
|
||||
private static final Map<Class<?>, TypeDescriptor> typeDescriptorCache = new HashMap<Class<?>, TypeDescriptor>();
|
||||
|
||||
static {
|
||||
typeDescriptorCache.put(String.class, new TypeDescriptor(String.class));
|
||||
typeDescriptorCache.put(Byte.class, new TypeDescriptor(Byte.class));
|
||||
typeDescriptorCache.put(Character.class, new TypeDescriptor(Character.class));
|
||||
typeDescriptorCache.put(Boolean.class, new TypeDescriptor(Boolean.class));
|
||||
typeDescriptorCache.put(Short.class, new TypeDescriptor(Short.class));
|
||||
typeDescriptorCache.put(Integer.class, new TypeDescriptor(Integer.class));
|
||||
typeDescriptorCache.put(Long.class, new TypeDescriptor(Long.class));
|
||||
typeDescriptorCache.put(Float.class, new TypeDescriptor(Float.class));
|
||||
typeDescriptorCache.put(Double.class, new TypeDescriptor(Double.class));
|
||||
}
|
||||
|
||||
private Object value;
|
||||
|
||||
private Class<?> type;
|
||||
@@ -413,11 +421,9 @@ public class TypeDescriptor {
|
||||
public static TypeDescriptor valueOf(Class<?> type) {
|
||||
if (type == null) {
|
||||
return TypeDescriptor.NULL;
|
||||
} else if (type.equals(String.class)) {
|
||||
return TypeDescriptor.STRING;
|
||||
} else {
|
||||
return new TypeDescriptor(type);
|
||||
}
|
||||
TypeDescriptor desc = typeDescriptorCache.get(type);
|
||||
return desc != null ? desc : new TypeDescriptor(type);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -50,19 +50,19 @@ public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport {
|
||||
Assert.notNull(targetDescriptor, "TypeDescriptor must not be null");
|
||||
this.conversionService = conversionService;
|
||||
this.targetDescriptor = targetDescriptor;
|
||||
this.canConvertToString = conversionService.canConvert(this.targetDescriptor, TypeDescriptor.STRING);
|
||||
this.canConvertToString = conversionService.canConvert(this.targetDescriptor, TypeDescriptor.valueOf(String.class));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setAsText(String text) throws IllegalArgumentException {
|
||||
setValue(this.conversionService.convert(text, TypeDescriptor.STRING, this.targetDescriptor));
|
||||
setValue(this.conversionService.convert(text, TypeDescriptor.valueOf(String.class), this.targetDescriptor));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsText() {
|
||||
if (this.canConvertToString) {
|
||||
return (String) this.conversionService.convert(getValue(), this.targetDescriptor, TypeDescriptor.STRING);
|
||||
return (String) this.conversionService.convert(getValue(), this.targetDescriptor, TypeDescriptor.valueOf(String.class));
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
|
||||
@@ -414,7 +414,7 @@ public class DefaultConversionTests {
|
||||
public void convertCollectionToStringWithElementConversion() throws Exception {
|
||||
List<Integer> list = Arrays.asList(new Integer[] { 3, 5 });
|
||||
String result = (String) conversionService.convert(list,
|
||||
new TypeDescriptor(getClass().getField("genericList")), TypeDescriptor.STRING);
|
||||
new TypeDescriptor(getClass().getField("genericList")), TypeDescriptor.valueOf(String.class));
|
||||
assertEquals("3,5", result);
|
||||
}
|
||||
|
||||
@@ -429,7 +429,7 @@ public class DefaultConversionTests {
|
||||
|
||||
@Test
|
||||
public void convertStringToCollectionWithElementConversion() throws Exception {
|
||||
List result = (List) conversionService.convert("1,2,3", TypeDescriptor.STRING,
|
||||
List result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class),
|
||||
new TypeDescriptor(getClass().getField("genericList")));
|
||||
assertEquals(3, result.size());
|
||||
assertEquals(new Integer(1), result.get(0));
|
||||
@@ -679,7 +679,7 @@ public class DefaultConversionTests {
|
||||
|
||||
@Test
|
||||
public void convertObjectToObjectFinderMethodWithNull() {
|
||||
TestEntity e = (TestEntity) conversionService.convert(null, TypeDescriptor.STRING, TypeDescriptor.valueOf(TestEntity.class));
|
||||
TestEntity e = (TestEntity) conversionService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(TestEntity.class));
|
||||
assertNull(e);
|
||||
}
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void convertNullTypeDescriptor() {
|
||||
assertNull(conversionService.convert("3", TypeDescriptor.STRING, TypeDescriptor.NULL));
|
||||
assertNull(conversionService.convert("3", TypeDescriptor.valueOf(String.class), TypeDescriptor.NULL));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -121,9 +121,9 @@ public class GenericConversionServiceTests {
|
||||
assertTrue(conversionService.canConvert(String.class, boolean.class));
|
||||
Boolean b = conversionService.convert("true", boolean.class);
|
||||
assertEquals(Boolean.TRUE, b);
|
||||
assertTrue(conversionService.canConvert(TypeDescriptor.STRING, TypeDescriptor
|
||||
assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), TypeDescriptor
|
||||
.valueOf(boolean.class)));
|
||||
b = (Boolean) conversionService.convert("true", TypeDescriptor.STRING, TypeDescriptor
|
||||
b = (Boolean) conversionService.convert("true", TypeDescriptor.valueOf(String.class), TypeDescriptor
|
||||
.valueOf(boolean.class));
|
||||
assertEquals(Boolean.TRUE, b);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user