removed valueOf convention b/c of unwanted side effects
This commit is contained in:
parent
dc076ee6fe
commit
4d9bf3a45f
|
|
@ -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);
|
||||
|
|
@ -315,14 +315,7 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
|
|||
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;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -331,31 +324,6 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -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 {
|
|||
}
|
||||
|
||||
@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)
|
||||
|
|
|
|||
|
|
@ -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…
Reference in New Issue