Skip to content

Commit

Permalink
TypeDescriptor.valueOf usage in favor of constants; TypedValue usage …
Browse files Browse the repository at this point in the history
…simplification
  • Loading branch information
Keith Donald committed Dec 15, 2009
1 parent f37d708 commit 2fef141
Show file tree
Hide file tree
Showing 31 changed files with 120 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.Map;

import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.PropertyAccessor;
Expand All @@ -39,7 +38,7 @@ public boolean canRead(EvaluationContext context, Object target, String name) th
}

public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
return new TypedValue(((Map) target).get(name), TypeDescriptor.OBJECT);
return new TypedValue(((Map) target).get(name));
}

public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
Expand All @@ -52,7 +51,7 @@ public void write(EvaluationContext context, Object target, String name, Object
}

public Class[] getSpecificTargetClasses() {
return new Class[] {Map.class};
return new Class[] { Map.class };
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ protected Object formatFieldValue(String field, Object value) {
}
}
if (this.conversionService != null) {
// Try custom formatter...
// Try custom converter...
TypeDescriptor fieldDesc = getPropertyAccessor().getPropertyTypeDescriptor(fixedField);
if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, TypeDescriptor.STRING)) {
return this.conversionService.convert(value, fieldDesc, TypeDescriptor.STRING);
TypeDescriptor strDesc = TypeDescriptor.valueOf(String.class);
if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, strDesc)) {
return this.conversionService.convert(value, fieldDesc, strDesc);
}
}
return value;
Expand Down Expand Up @@ -152,7 +153,7 @@ public PropertyEditor findEditor(String field, Class valueType) {
TypeDescriptor td = (field != null ?
getPropertyAccessor().getPropertyTypeDescriptor(fixedField(field)) :
TypeDescriptor.valueOf(valueType));
if (this.conversionService.canConvert(TypeDescriptor.STRING, td)) {
if (this.conversionService.canConvert(TypeDescriptor.valueOf(String.class), td)) {
editor = new ConvertingPropertyEditorAdapter(this.conversionService, td);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,44 +95,44 @@ public Date convert(DateTime source) {
});
formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
String formatted = (String) formattingService.convert(new LocalDate(2009, 10, 31).toDateTimeAtCurrentTime()
.toDate(), new TypeDescriptor(Model.class.getField("date")), TypeDescriptor.STRING);
.toDate(), new TypeDescriptor(Model.class.getField("date")), TypeDescriptor.valueOf(String.class));
assertEquals("10/31/09", formatted);
LocalDate date = new LocalDate(formattingService.convert("10/31/09", TypeDescriptor.STRING,
LocalDate date = new LocalDate(formattingService.convert("10/31/09", TypeDescriptor.valueOf(String.class),
new TypeDescriptor(Model.class.getField("date"))));
assertEquals(new LocalDate(2009, 10, 31), date);
}

@Test
public void testPrintNull() throws ParseException {
formattingService.addFormatterForFieldType(Number.class, new NumberFormatter());
assertEquals("", formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.STRING));
assertEquals("", formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(String.class)));
}

@Test
public void testParseNull() throws ParseException {
formattingService.addFormatterForFieldType(Number.class, new NumberFormatter());
assertNull(formattingService.convert(null, TypeDescriptor.STRING, TypeDescriptor.valueOf(Integer.class)));
assertNull(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
}

@Test
public void testParseEmptyString() throws ParseException {
formattingService.addFormatterForFieldType(Number.class, new NumberFormatter());
assertNull(formattingService.convert("", TypeDescriptor.STRING, TypeDescriptor.valueOf(Integer.class)));
assertNull(formattingService.convert("", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
}

@Test
public void testPrintNullDefault() throws ParseException {
assertEquals(null, formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.STRING));
assertEquals(null, formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(String.class)));
}

@Test
public void testParseNullDefault() throws ParseException {
assertNull(formattingService.convert(null, TypeDescriptor.STRING, TypeDescriptor.valueOf(Integer.class)));
assertNull(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
}

@Test
public void testParseEmptyStringDefault() throws ParseException {
assertNull(formattingService.convert("", TypeDescriptor.STRING, TypeDescriptor.valueOf(Integer.class)));
assertNull(formattingService.convert("", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
}

private static class Model {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand All @@ -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;
Expand Down Expand Up @@ -413,11 +421,9 @@ public String toString() {
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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ public ConvertingPropertyEditorAdapter(ConversionService conversionService, Type
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public void convertCollectionToString() {
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);
}

Expand All @@ -429,7 +429,7 @@ public void convertStringToCollection() {

@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));
Expand Down Expand Up @@ -679,7 +679,7 @@ public void convertObjectToObjectFinderMethod() {

@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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void convertNullTargetClass() {

@Test
public void convertNullTypeDescriptor() {
assertNull(conversionService.convert("3", TypeDescriptor.STRING, TypeDescriptor.NULL));
assertNull(conversionService.convert("3", TypeDescriptor.valueOf(String.class), TypeDescriptor.NULL));
}

@Test
Expand Down Expand Up @@ -121,9 +121,9 @@ public void convertObjectToPrimitive() {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ public Class getValueType() {
}

public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}

public TypeDescriptor getValueTypeDescriptor() {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}

public void setValue(EvaluationContext context, Object value) throws EvaluationException {
Expand Down Expand Up @@ -157,11 +157,11 @@ public Class getValueType(EvaluationContext context, Object rootObject) throws E
}

public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}

public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject) throws EvaluationException {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}

public boolean isWritable(EvaluationContext context, Object rootObject) throws EvaluationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ public Class getValueType(EvaluationContext context) {
}

public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}

public TypeDescriptor getValueTypeDescriptor() {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}

public void setValue(EvaluationContext context, Object value) throws EvaluationException {
Expand All @@ -91,49 +91,40 @@ public Class getValueType() {
return String.class;
}


public <T> T getValue(Object rootObject, Class<T> desiredResultType) throws EvaluationException {
Object value = getValue(rootObject);
return ExpressionUtils.convert(null, value, desiredResultType);
}


public String getValue(EvaluationContext context, Object rootObject) throws EvaluationException {
return this.literalValue;
}


public <T> T getValue(EvaluationContext context, Object rootObject, Class<T> desiredResultType) throws EvaluationException {
Object value = getValue(context, rootObject);
return ExpressionUtils.convert(null, value, desiredResultType);
}


public Class getValueType(Object rootObject) throws EvaluationException {
return String.class;
}


public Class getValueType(EvaluationContext context, Object rootObject) throws EvaluationException {
return String.class;
}


public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}


public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject) throws EvaluationException {
return TypeDescriptor.STRING;
return TypeDescriptor.valueOf(String.class);
}


public boolean isWritable(EvaluationContext context, Object rootObject) throws EvaluationException {
return false;
}


public void setValue(EvaluationContext context, Object rootObject, Object value) throws EvaluationException {
throw new EvaluationException(literalValue, "Cannot call setValue() on a LiteralExpression");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Identifier extends SpelNodeImpl {

public Identifier(String payload,int pos) {
super(pos);
this.id = new TypedValue(payload, STRING_TYPE_DESCRIPTOR);
this.id = new TypedValue(payload);
}

@Override
Expand Down
Loading

0 comments on commit 2fef141

Please sign in to comment.