13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
+
16
17
package org .springframework .format .support ;
17
18
18
19
import java .lang .annotation .Annotation ;
21
22
22
23
import org .springframework .context .i18n .LocaleContextHolder ;
23
24
import org .springframework .core .GenericTypeResolver ;
25
+ import org .springframework .core .convert .ConversionException ;
24
26
import org .springframework .core .convert .ConversionFailedException ;
25
27
import org .springframework .core .convert .ConversionService ;
26
28
import org .springframework .core .convert .TypeDescriptor ;
27
29
import org .springframework .core .convert .converter .ConditionalGenericConverter ;
28
- import org .springframework .core .convert .converter .ConverterRegistry ;
29
30
import org .springframework .core .convert .converter .GenericConverter ;
30
- import org .springframework .core .convert .support .ConversionServiceFactory ;
31
+ import org .springframework .core .convert .support .GenericConversionService ;
31
32
import org .springframework .format .AnnotationFormatterFactory ;
32
33
import org .springframework .format .Formatter ;
33
34
import org .springframework .format .FormatterRegistry ;
34
35
import org .springframework .format .Parser ;
35
36
import org .springframework .format .Printer ;
36
37
37
38
/**
38
- * A ConversionService implementation designed to be configured as a {@link FormatterRegistry}..
39
+ * A {@link org.springframework.core.convert.ConversionService} implementation
40
+ * designed to be configured as a {@link FormatterRegistry}.
41
+ *
39
42
* @author Keith Donald
43
+ * @author Juergen Hoeller
40
44
* @since 3.0
41
45
*/
42
- public class FormattingConversionService implements FormatterRegistry , ConversionService {
46
+ public class FormattingConversionService extends GenericConversionService
47
+ implements FormatterRegistry {
43
48
44
- private ConversionService conversionService = ConversionServiceFactory .createDefaultConversionService ();
45
-
46
- // implementing FormattingRegistry
47
-
48
49
public void addFormatterForFieldType (Class <?> fieldType , Printer <?> printer , Parser <?> parser ) {
49
- getConverterRegistry (). addGenericConverter (new PrinterConverter (fieldType , printer , this . conversionService ));
50
- getConverterRegistry (). addGenericConverter (new ParserConverter (fieldType , parser , this . conversionService ));
50
+ addGenericConverter (new PrinterConverter (fieldType , printer , this ));
51
+ addGenericConverter (new ParserConverter (fieldType , parser , this ));
51
52
}
52
53
53
54
public void addFormatterForFieldType (Class <?> fieldType , Formatter <?> formatter ) {
54
- getConverterRegistry (). addGenericConverter (new PrinterConverter (fieldType , formatter , this . conversionService ));
55
- getConverterRegistry (). addGenericConverter (new ParserConverter (fieldType , formatter , this . conversionService ));
55
+ addGenericConverter (new PrinterConverter (fieldType , formatter , this ));
56
+ addGenericConverter (new ParserConverter (fieldType , formatter , this ));
56
57
}
57
58
58
59
@ SuppressWarnings ("unchecked" )
59
60
public void addFormatterForFieldAnnotation (final AnnotationFormatterFactory annotationFormatterFactory ) {
60
- final Class <? extends Annotation > annotationType = resolveAnnotationType (annotationFormatterFactory );
61
+ final Class <? extends Annotation > annotationType = (Class <? extends Annotation >)
62
+ GenericTypeResolver .resolveTypeArgument (annotationFormatterFactory .getClass (), AnnotationFormatterFactory .class );
61
63
if (annotationType == null ) {
62
64
throw new IllegalArgumentException (
63
65
"Unable to extract parameterized Annotation type argument from AnnotationFormatterFactory ["
64
66
+ annotationFormatterFactory .getClass ().getName ()
65
67
+ "]; does the factory parameterize the <A extends Annotation> generic type?" );
66
68
}
67
69
Set <Class <?>> fieldTypes = annotationFormatterFactory .getFieldTypes ();
70
+
68
71
for (final Class <?> fieldType : fieldTypes ) {
69
- getConverterRegistry (). addGenericConverter (new ConditionalGenericConverter () {
72
+ addGenericConverter (new ConditionalGenericConverter () {
70
73
public Class <?>[][] getConvertibleTypes () {
71
- return new Class <?>[][] { { fieldType , String .class } };
74
+ return new Class <?>[][] {{ fieldType , String .class } };
72
75
}
73
76
public boolean matches (TypeDescriptor sourceFieldType , TypeDescriptor targetFieldType ) {
74
- return sourceFieldType .getAnnotation (annotationType ) != null ;
77
+ return ( sourceFieldType .getAnnotation (annotationType ) != null ) ;
75
78
}
76
79
public Object convert (Object source , TypeDescriptor sourceType , TypeDescriptor targetType ) {
77
80
Printer <?> printer = annotationFormatterFactory .getPrinter (sourceType .getAnnotation (annotationType ), sourceType .getType ());
78
- return new PrinterConverter (fieldType , printer , conversionService ).convert (source , sourceType , targetType );
81
+ return new PrinterConverter (fieldType , printer , FormattingConversionService . this ).convert (source , sourceType , targetType );
79
82
}
80
83
public String toString () {
81
- return "@" + annotationType .getName () + " " + fieldType .getName () + " -> " + String .class .getName () + " : " + annotationFormatterFactory ;
84
+ return "@" + annotationType .getName () + " " + fieldType .getName () + " -> " +
85
+ String .class .getName () + ": " + annotationFormatterFactory ;
82
86
}
83
87
});
84
- getConverterRegistry (). addGenericConverter (new ConditionalGenericConverter () {
88
+ addGenericConverter (new ConditionalGenericConverter () {
85
89
public Class <?>[][] getConvertibleTypes () {
86
- return new Class <?>[][] { { String .class , fieldType } };
90
+ return new Class <?>[][] {{ String .class , fieldType } };
87
91
}
88
92
public boolean matches (TypeDescriptor sourceFieldType , TypeDescriptor targetFieldType ) {
89
- return targetFieldType .getAnnotation (annotationType ) != null ;
93
+ return ( targetFieldType .getAnnotation (annotationType ) != null ) ;
90
94
}
91
95
public Object convert (Object source , TypeDescriptor sourceType , TypeDescriptor targetType ) {
92
96
Parser <?> parser = annotationFormatterFactory .getParser (targetType .getAnnotation (annotationType ), targetType .getType ());
93
- return new ParserConverter (fieldType , parser , conversionService ).convert (source , sourceType , targetType );
97
+ return new ParserConverter (fieldType , parser , FormattingConversionService . this ).convert (source , sourceType , targetType );
94
98
}
95
99
public String toString () {
96
- return String .class .getName () + " -> @" + annotationType .getName () + " " + fieldType .getName () + " : " + annotationFormatterFactory ;
100
+ return String .class .getName () + " -> @" + annotationType .getName () + " " +
101
+ fieldType .getName () + ": " + annotationFormatterFactory ;
97
102
}
98
103
});
99
104
}
100
105
}
101
106
102
- public ConverterRegistry getConverterRegistry () {
103
- return (ConverterRegistry ) this .conversionService ;
104
- }
105
-
106
- // implementing ConverisonService
107
107
108
- public boolean canConvert (Class <?> sourceType , Class <?> targetType ) {
109
- return canConvert (TypeDescriptor .valueOf (sourceType ), TypeDescriptor .valueOf (targetType ));
110
- }
111
-
112
- @ SuppressWarnings ("unchecked" )
113
- public <T > T convert (Object source , Class <T > targetType ) {
114
- return (T ) convert (source , TypeDescriptor .forObject (source ), TypeDescriptor .valueOf (targetType ));
115
- }
116
-
117
- public boolean canConvert (TypeDescriptor sourceType , TypeDescriptor targetType ) {
118
- return this .conversionService .canConvert (sourceType , targetType );
119
- }
120
-
121
- public Object convert (Object source , TypeDescriptor sourceType , TypeDescriptor targetType ) {
122
- return this .conversionService .convert (source , sourceType , targetType );
123
- }
124
-
125
- public String toString () {
126
- return this .conversionService .toString ();
127
- }
128
-
129
- // internal helpers
130
-
131
- @ SuppressWarnings ("unchecked" )
132
- private Class <? extends Annotation > resolveAnnotationType (AnnotationFormatterFactory <?> annotationFormatterFactory ) {
133
- return (Class <? extends Annotation >) GenericTypeResolver .resolveTypeArgument (annotationFormatterFactory .getClass (), AnnotationFormatterFactory .class );
134
- }
135
-
136
108
private static class PrinterConverter implements GenericConverter {
137
109
138
110
private Class <?> fieldType ;
@@ -172,6 +144,7 @@ public String toString() {
172
144
}
173
145
}
174
146
147
+
175
148
private static class ParserConverter implements GenericConverter {
176
149
177
150
private Class <?> fieldType ;
@@ -198,22 +171,24 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
198
171
Object parsedValue ;
199
172
try {
200
173
parsedValue = this .parser .parse (submittedValue , LocaleContextHolder .getLocale ());
201
- } catch (ParseException e ) {
202
- throw new ConversionFailedException (sourceType , targetType , source , e );
174
+ }
175
+ catch (ParseException ex ) {
176
+ throw new ConversionFailedException (sourceType , targetType , source , ex );
203
177
}
204
178
TypeDescriptor parsedObjectType = TypeDescriptor .valueOf (parsedValue .getClass ());
205
179
if (!parsedObjectType .isAssignableTo (targetType )) {
206
180
try {
207
181
parsedValue = this .conversionService .convert (parsedValue , parsedObjectType , targetType );
208
- } catch (ConversionFailedException e ) {
209
- throw new ConversionFailedException (sourceType , targetType , source , e );
182
+ }
183
+ catch (ConversionException ex ) {
184
+ throw new ConversionFailedException (sourceType , targetType , source , ex );
210
185
}
211
186
}
212
187
return parsedValue ;
213
188
}
214
189
215
190
public String toString () {
216
- return String .class .getName () + " -> " + this .fieldType .getName () + " : " + this .parser ;
191
+ return String .class .getName () + " -> " + this .fieldType .getName () + ": " + this .parser ;
217
192
}
218
193
219
194
}
0 commit comments