Skip to content

Commit fe33feb

Browse files
committed
Support the meta annotation at @numberformat
Issue: SPR-12743
1 parent 5bdc8d2 commit fe33feb

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

spring-context/src/main/java/org/springframework/format/annotation/NumberFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
*/
4242
@Documented
4343
@Retention(RetentionPolicy.RUNTIME)
44-
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
44+
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
4545
public @interface NumberFormat {
4646

4747
/**

spring-context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,6 +48,7 @@
4848
import org.springframework.core.convert.support.DefaultConversionService;
4949
import org.springframework.format.Formatter;
5050
import org.springframework.format.Printer;
51+
import org.springframework.format.annotation.NumberFormat;
5152
import org.springframework.format.datetime.joda.DateTimeParser;
5253
import org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory;
5354
import org.springframework.format.datetime.joda.ReadablePartialPrinter;
@@ -120,12 +121,15 @@ public void testFormatFieldForValueInjectionUsingMetaAnnotations() {
120121
ac.registerBeanDefinition("ppc", new RootBeanDefinition(PropertyPlaceholderConfigurer.class));
121122
ac.refresh();
122123
System.setProperty("myDate", "10-31-09");
124+
System.setProperty("myNumber", "99.99%");
123125
try {
124126
MetaValueBean valueBean = ac.getBean(MetaValueBean.class);
125127
assertEquals(new LocalDate(2009, 10, 31), new LocalDate(valueBean.date));
128+
assertEquals(Double.valueOf(0.9999), valueBean.number);
126129
}
127130
finally {
128131
System.clearProperty("myDate");
132+
System.clearProperty("myNumber");
129133
}
130134
}
131135

@@ -341,6 +345,10 @@ public static class MetaValueBean {
341345

342346
@MyDateAnn
343347
public Date date;
348+
349+
@MyNumberAnn
350+
public Double number;
351+
344352
}
345353

346354

@@ -350,6 +358,11 @@ public static class MetaValueBean {
350358
public static @interface MyDateAnn {
351359
}
352360

361+
@Value("${myNumber}")
362+
@org.springframework.format.annotation.NumberFormat(style = NumberFormat.Style.PERCENT)
363+
@Retention(RetentionPolicy.RUNTIME)
364+
public static @interface MyNumberAnn {
365+
}
353366

354367
public static class Model {
355368

spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@
3737
import com.fasterxml.jackson.databind.ObjectMapper;
3838
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
3939
import org.hamcrest.Matchers;
40+
import org.joda.time.LocalDate;
4041
import org.junit.Before;
4142
import org.junit.Test;
4243

@@ -52,6 +53,7 @@
5253
import org.springframework.core.io.ClassPathResource;
5354
import org.springframework.format.annotation.DateTimeFormat;
5455
import org.springframework.format.annotation.DateTimeFormat.ISO;
56+
import org.springframework.format.annotation.NumberFormat;
5557
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
5658
import org.springframework.http.MediaType;
5759
import org.springframework.http.converter.HttpMessageConverter;
@@ -165,7 +167,7 @@ public void setUp() throws Exception {
165167
appContext.getServletContext().setAttribute(attributeName, appContext);
166168

167169
handler = new TestController();
168-
Method method = TestController.class.getMethod("testBind", Date.class, TestBean.class, BindingResult.class);
170+
Method method = TestController.class.getMethod("testBind", Date.class, Double.class, TestBean.class, BindingResult.class);
169171
handlerMethod = new InvocableHandlerMethod(handler, method);
170172
}
171173

@@ -211,6 +213,7 @@ public void testDefaultConfig() throws Exception {
211213
// default web binding initializer behavior test
212214
request = new MockHttpServletRequest("GET", "/");
213215
request.addParameter("date", "2009-10-31");
216+
request.addParameter("percent", "99.99%");
214217
MockHttpServletResponse response = new MockHttpServletResponse();
215218

216219
HandlerExecutionChain chain = mapping.getHandler(request);
@@ -222,6 +225,8 @@ public void testDefaultConfig() throws Exception {
222225

223226
adapter.handle(request, response, handlerMethod);
224227
assertTrue(handler.recordedValidationError);
228+
assertEquals(LocalDate.parse("2009-10-31").toDate(), handler.date);
229+
assertEquals(Double.valueOf(0.9999),handler.percent);
225230

226231
CompositeUriComponentsContributor uriComponentsContributor = this.appContext.getBean(
227232
MvcUriComponentsBuilder.MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME,
@@ -841,6 +846,12 @@ private void loadBeanDefinitions(String fileName, int expectedBeanCount) {
841846
public @interface IsoDate {
842847
}
843848

849+
@NumberFormat(style = NumberFormat.Style.PERCENT)
850+
@Target({ElementType.PARAMETER})
851+
@Retention(RetentionPolicy.RUNTIME)
852+
public @interface PercentNumber {
853+
}
854+
844855
@Validated(MyGroup.class)
845856
@Target({ElementType.PARAMETER})
846857
@Retention(RetentionPolicy.RUNTIME)
@@ -850,10 +861,14 @@ private void loadBeanDefinitions(String fileName, int expectedBeanCount) {
850861
@Controller
851862
public static class TestController {
852863

864+
private Date date;
865+
private Double percent;
853866
private boolean recordedValidationError;
854867

855868
@RequestMapping
856-
public void testBind(@RequestParam @IsoDate Date date, @MyValid TestBean bean, BindingResult result) {
869+
public void testBind(@RequestParam @IsoDate Date date, @RequestParam(required = false) @PercentNumber Double percent, @MyValid TestBean bean, BindingResult result) {
870+
this.date = date;
871+
this.percent = percent;
857872
this.recordedValidationError = (result.getErrorCount() == 1);
858873
}
859874
}

0 commit comments

Comments
 (0)