Browse Source

removed valueOf convention b/c of unwanted side effects

pull/23217/head
Keith Donald 15 years ago
parent
commit
4d9bf3a45f
  1. 34
      org.springframework.context/src/main/java/org/springframework/ui/format/support/GenericFormatterRegistry.java
  2. 7
      org.springframework.context/src/test/java/org/springframework/ui/format/GenericFormatterRegistryTests.java
  3. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToStringConverter.java

34
org.springframework.context/src/main/java/org/springframework/ui/format/support/GenericFormatterRegistry.java

@ -277,7 +277,7 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC @@ -277,7 +277,7 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
} else {
Formatted formattedAnnotation = annotationType.getAnnotation(Formatted.class);
if (formattedAnnotation != null) {
// annotation has @Formatted meta-annotation
// property annotation has @Formatted meta-annotation
Formatter formatter = createFormatter(formattedAnnotation.value());
addFormatterByAnnotation(annotationType, formatter);
return findFormatterHolderForAnnotation(annotation);
@ -314,48 +314,16 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC @@ -314,48 +314,16 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
Formatter formatter = createFormatter(formatted.value());
addFormatterByType(type, formatter);
return findFormatterHolderForType(type);
} else {
Method valueOfMethod = getValueOfMethod(type);
if (valueOfMethod != null) {
Formatter formatter = createFormatter(valueOfMethod);
addFormatterByType(type, formatter);
return findFormatterHolderForType(type);
} else {
return null;
}
}
}
private Formatter createFormatter(Class<? extends Formatter> formatterClass) {
return (this.applicationContext != null ? this.applicationContext.getAutowireCapableBeanFactory().createBean(
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 Class formattedObjectType;

7
org.springframework.context/src/test/java/org/springframework/ui/format/GenericFormatterRegistryTests.java

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

8
org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToStringConverter.java

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

Loading…
Cancel
Save