removed valueOf convention b/c of unwanted side effects

This commit is contained in:
Keith Donald 2009-10-14 21:37:57 +00:00
parent dc076ee6fe
commit 4d9bf3a45f
3 changed files with 5 additions and 46 deletions

View File

@ -277,7 +277,7 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
} else { } else {
Formatted formattedAnnotation = annotationType.getAnnotation(Formatted.class); Formatted formattedAnnotation = annotationType.getAnnotation(Formatted.class);
if (formattedAnnotation != null) { if (formattedAnnotation != null) {
// annotation has @Formatted meta-annotation // property annotation has @Formatted meta-annotation
Formatter formatter = createFormatter(formattedAnnotation.value()); Formatter formatter = createFormatter(formattedAnnotation.value());
addFormatterByAnnotation(annotationType, formatter); addFormatterByAnnotation(annotationType, formatter);
return findFormatterHolderForAnnotation(annotation); return findFormatterHolderForAnnotation(annotation);
@ -315,14 +315,7 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
addFormatterByType(type, formatter); addFormatterByType(type, formatter);
return findFormatterHolderForType(type); return findFormatterHolderForType(type);
} else { } else {
Method valueOfMethod = getValueOfMethod(type); return null;
if (valueOfMethod != null) {
Formatter formatter = createFormatter(valueOfMethod);
addFormatterByType(type, formatter);
return findFormatterHolderForType(type);
} else {
return null;
}
} }
} }
@ -331,31 +324,6 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
formatterClass) : BeanUtils.instantiate(formatterClass)); formatterClass) : BeanUtils.instantiate(formatterClass));
} }
private Method getValueOfMethod(Class type) {
Method[] methods = type.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if ("valueOf".equals(method.getName()) && acceptsSingleStringParameterType(method)
&& Modifier.isPublic(method.getModifiers()) && Modifier.isStatic(method.getModifiers())) {
return method;
}
}
return null;
}
private boolean acceptsSingleStringParameterType(Method method) {
Class[] paramTypes = method.getParameterTypes();
if (paramTypes == null) {
return false;
} else {
return paramTypes.length == 1 && paramTypes[0] == String.class;
}
}
private Formatter createFormatter(Method valueOfMethod) {
return new ValueOfMethodFormatter(valueOfMethod);
}
private abstract static class AbstractFormatterHolder { private abstract static class AbstractFormatterHolder {
private Class formattedObjectType; private Class formattedObjectType;

View File

@ -17,6 +17,7 @@
package org.springframework.ui.format; package org.springframework.ui.format;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
@ -104,10 +105,8 @@ public class GenericFormatterRegistryTests {
} }
@Test @Test
public void testGetDefaultFormatterForTypeValueOfMethod() throws ParseException { public void testGetDefaultFormatterNull() throws ParseException {
Formatter formatter = registry.getFormatter(TypeDescriptor.valueOf(Integer.class)); assertNull(registry.getFormatter(TypeDescriptor.valueOf(Integer.class)));
assertEquals("3", formatter.format(new Integer(3), null));
assertEquals(new Integer(3), formatter.parse("3", null));
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)

View File

@ -28,14 +28,6 @@ import org.springframework.core.convert.converter.Converter;
*/ */
final class ObjectToStringConverter implements Converter<Object, String> { final class ObjectToStringConverter implements Converter<Object, String> {
public Class<?> getSourceType() {
return Object.class;
}
public Class<?> getTargetType() {
return String.class;
}
public String convert(Object source) { public String convert(Object source) {
return source.toString(); return source.toString();
} }