diff --git a/spring-binding/src/main/java/org/springframework/binding/format/FormatterRegistry.java b/spring-binding/src/main/java/org/springframework/binding/format/FormatterRegistry.java index 33ed92c4..a7bfa72e 100644 --- a/spring-binding/src/main/java/org/springframework/binding/format/FormatterRegistry.java +++ b/spring-binding/src/main/java/org/springframework/binding/format/FormatterRegistry.java @@ -32,9 +32,10 @@ public interface FormatterRegistry { /** * Returns the formatter for the given class of object with the given id. Use this method to query a custom * formatter instance for a given class of object. + * @param clazz the type of object that will be formatted * @param id the id of the custom formatter instance; typically descriptive like "localDate" * @return the formatter */ - public Formatter getFormatter(String id); + public Formatter getFormatter(Class clazz, String id); } \ No newline at end of file diff --git a/spring-binding/src/main/java/org/springframework/binding/format/registry/GenericFormatterRegistry.java b/spring-binding/src/main/java/org/springframework/binding/format/registry/GenericFormatterRegistry.java index d717c778..3b0372a2 100644 --- a/spring-binding/src/main/java/org/springframework/binding/format/registry/GenericFormatterRegistry.java +++ b/spring-binding/src/main/java/org/springframework/binding/format/registry/GenericFormatterRegistry.java @@ -42,9 +42,13 @@ public class GenericFormatterRegistry implements FormatterRegistry { return (Formatter) formattersByClass.get(clazz); } - public Formatter getFormatter(String id) { + public Formatter getFormatter(Class clazz, String id) { + Assert.notNull(clazz, "The formatted class argument is required"); Assert.hasText(id, "The id of the custom formatter is required"); - return (Formatter) formattersById.get(id); + Formatter formatter = (Formatter) formattersById.get(id); + if (formatter != null && formatter.getObjectType().equals(clazz)) { + } + return formatter; } // impl diff --git a/spring-binding/src/test/java/org/springframework/binding/format/impl/GenericFormatterRegistryTests.java b/spring-binding/src/test/java/org/springframework/binding/format/impl/GenericFormatterRegistryTests.java index 4a779148..277f8c64 100644 --- a/spring-binding/src/test/java/org/springframework/binding/format/impl/GenericFormatterRegistryTests.java +++ b/spring-binding/src/test/java/org/springframework/binding/format/impl/GenericFormatterRegistryTests.java @@ -45,7 +45,7 @@ public class GenericFormatterRegistryTests extends TestCase { NumberFormatter percentFormatter = new NumberFormatter(BigDecimal.class); percentFormatter.setPattern("00%"); registry.registerFormatter("percent", percentFormatter); - Formatter formatter = registry.getFormatter("percent"); + Formatter formatter = registry.getFormatter(Integer.class, "percent"); assertEquals("35%", formatter.format(new BigDecimal(".35"))); BigDecimal value = (BigDecimal) formatter.parse("35%"); assertEquals(new BigDecimal(".35"), value); @@ -54,9 +54,9 @@ public class GenericFormatterRegistryTests extends TestCase { public void testRegisterCustomFormatterBogusLookupId() { registry.registerFormatter(new NumberFormatter(Integer.class)); registry.registerFormatter("double", new NumberFormatter(Double.class)); - Formatter formatter = registry.getFormatter("bogusFormat"); + Formatter formatter = registry.getFormatter(Integer.class, "bogusFormat"); assertNull(formatter); - formatter = registry.getFormatter("double"); + formatter = registry.getFormatter(Double.class, "double"); assertNotNull(formatter); } diff --git a/spring-faces/src/test/java/org/springframework/faces/config/FacesFlowBuilderServicesBeanDefinitionParserTests.java b/spring-faces/src/test/java/org/springframework/faces/config/FacesFlowBuilderServicesBeanDefinitionParserTests.java index 2e811cdc..85d2fffb 100644 --- a/spring-faces/src/test/java/org/springframework/faces/config/FacesFlowBuilderServicesBeanDefinitionParserTests.java +++ b/spring-faces/src/test/java/org/springframework/faces/config/FacesFlowBuilderServicesBeanDefinitionParserTests.java @@ -88,7 +88,8 @@ public class FacesFlowBuilderServicesBeanDefinitionParserTests extends TestCase throw new UnsupportedOperationException("Auto-generated method stub"); } - public ConversionExecutor[] getConversionExecutorsForSource(Class sourceClass) throws ConversionExecutionException { + public ConversionExecutor[] getConversionExecutorsForSource(Class sourceClass) + throws ConversionExecutionException { throw new UnsupportedOperationException("Auto-generated method stub"); } @@ -100,7 +101,7 @@ public class FacesFlowBuilderServicesBeanDefinitionParserTests extends TestCase throw new UnsupportedOperationException("Auto-generated method stub"); } - public Formatter getFormatter(String id) { + public Formatter getFormatter(Class clazz, String id) { throw new UnsupportedOperationException("Auto-generated method stub"); } diff --git a/spring-webflow/src/main/java/org/springframework/webflow/mvc/view/AbstractMvcView.java b/spring-webflow/src/main/java/org/springframework/webflow/mvc/view/AbstractMvcView.java index ac75fbb2..5663c960 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/mvc/view/AbstractMvcView.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/mvc/view/AbstractMvcView.java @@ -513,7 +513,7 @@ public abstract class AbstractMvcView implements View { private Formatter getFormatter(Expression target, Class targetClass) { if (formatterRegistry != null) { - Formatter formatter = formatterRegistry.getFormatter(target.getExpressionString()); + Formatter formatter = formatterRegistry.getFormatter(targetClass, target.getExpressionString()); if (formatter != null) { return formatter; } else { diff --git a/spring-webflow/src/test/java/org/springframework/webflow/config/FlowBuilderServicesBeanDefinitionParserTests.java b/spring-webflow/src/test/java/org/springframework/webflow/config/FlowBuilderServicesBeanDefinitionParserTests.java index 3f8c81dc..85cfcf28 100644 --- a/spring-webflow/src/test/java/org/springframework/webflow/config/FlowBuilderServicesBeanDefinitionParserTests.java +++ b/spring-webflow/src/test/java/org/springframework/webflow/config/FlowBuilderServicesBeanDefinitionParserTests.java @@ -71,7 +71,8 @@ public class FlowBuilderServicesBeanDefinitionParserTests extends TestCase { throw new UnsupportedOperationException("Auto-generated method stub"); } - public ConversionExecutor[] getConversionExecutorsForSource(Class sourceClass) throws ConversionExecutionException { + public ConversionExecutor[] getConversionExecutorsForSource(Class sourceClass) + throws ConversionExecutionException { throw new UnsupportedOperationException("Auto-generated method stub"); } @@ -83,7 +84,7 @@ public class FlowBuilderServicesBeanDefinitionParserTests extends TestCase { throw new UnsupportedOperationException("Auto-generated method stub"); } - public Formatter getFormatter(String id) { + public Formatter getFormatter(Class clazz, String id) { throw new UnsupportedOperationException("Auto-generated method stub"); }