@@ -51,11 +51,11 @@ public class FormattingConversionService extends GenericConversionService
51
51
52
52
private StringValueResolver embeddedValueResolver ;
53
53
54
- private final Map <FieldFormatterKey , GenericConverter > cachedPrinters =
55
- new ConcurrentHashMap <FieldFormatterKey , GenericConverter >();
54
+ private final Map <AnnotationConverterKey , GenericConverter > cachedPrinters =
55
+ new ConcurrentHashMap <AnnotationConverterKey , GenericConverter >();
56
56
57
- private final Map <FieldFormatterKey , GenericConverter > cachedParsers =
58
- new ConcurrentHashMap <FieldFormatterKey , GenericConverter >();
57
+ private final Map <AnnotationConverterKey , GenericConverter > cachedParsers =
58
+ new ConcurrentHashMap <AnnotationConverterKey , GenericConverter >();
59
59
60
60
61
61
public void setEmbeddedValueResolver (StringValueResolver resolver ) {
@@ -93,90 +93,13 @@ public void addFormatterForFieldAnnotation(final AnnotationFormatterFactory anno
93
93
if (this .embeddedValueResolver != null && annotationFormatterFactory instanceof EmbeddedValueResolverAware ) {
94
94
((EmbeddedValueResolverAware ) annotationFormatterFactory ).setEmbeddedValueResolver (this .embeddedValueResolver );
95
95
}
96
-
97
96
Set <Class <?>> fieldTypes = annotationFormatterFactory .getFieldTypes ();
98
97
for (final Class <?> fieldType : fieldTypes ) {
99
- addConverter (new ConditionalGenericConverter () {
100
- public Set <ConvertiblePair > getConvertibleTypes () {
101
- return Collections .singleton (new ConvertiblePair (fieldType , String .class ));
102
- }
103
- public boolean matches (TypeDescriptor sourceType , TypeDescriptor targetType ) {
104
- return (sourceType .getAnnotation (annotationType ) != null );
105
- }
106
- public Object convert (Object source , TypeDescriptor sourceType , TypeDescriptor targetType ) {
107
- FieldFormatterKey key = new FieldFormatterKey (sourceType .getAnnotation (annotationType ), fieldType );
108
- GenericConverter converter = cachedPrinters .get (key );
109
- if (converter == null ) {
110
- Printer <?> printer = annotationFormatterFactory .getPrinter (key .getAnnotation (), key .getFieldType ());
111
- converter = new PrinterConverter (fieldType , printer , FormattingConversionService .this );
112
- cachedPrinters .put (key , converter );
113
- }
114
- return converter .convert (source , sourceType , targetType );
115
- }
116
- public String toString () {
117
- return "@" + annotationType .getName () + " " + fieldType .getName () + " -> " +
118
- String .class .getName () + ": " + annotationFormatterFactory ;
119
- }
120
- });
121
- addConverter (new ConditionalGenericConverter () {
122
- public Set <ConvertiblePair > getConvertibleTypes () {
123
- return Collections .singleton (new ConvertiblePair (String .class , fieldType ));
124
- }
125
- public boolean matches (TypeDescriptor sourceType , TypeDescriptor targetType ) {
126
- return (targetType .getAnnotation (annotationType ) != null );
127
- }
128
- public Object convert (Object source , TypeDescriptor sourceType , TypeDescriptor targetType ) {
129
- FieldFormatterKey key = new FieldFormatterKey (targetType .getAnnotation (annotationType ), fieldType );
130
- GenericConverter converter = cachedParsers .get (key );
131
- if (converter == null ) {
132
- Parser <?> printer = annotationFormatterFactory .getParser (key .getAnnotation (), key .getFieldType ());
133
- converter = new ParserConverter (fieldType , printer , FormattingConversionService .this );
134
- cachedParsers .put (key , converter );
135
- }
136
- return converter .convert (source , sourceType , targetType );
137
- }
138
- public String toString () {
139
- return String .class .getName () + " -> @" + annotationType .getName () + " " +
140
- fieldType .getName () + ": " + annotationFormatterFactory ;
141
- }
142
- });
98
+ addConverter (new AnnotationPrinterConverter (annotationType , annotationFormatterFactory , fieldType ));
99
+ addConverter (new AnnotationParserConverter (annotationType , annotationFormatterFactory , fieldType ));
143
100
}
144
101
}
145
102
146
-
147
- private static final class FieldFormatterKey {
148
-
149
- private final Annotation annotation ;
150
-
151
- private final Class <?> fieldType ;
152
-
153
- public FieldFormatterKey (Annotation annotation , Class <?> fieldType ) {
154
- this .annotation = annotation ;
155
- this .fieldType = fieldType ;
156
- }
157
-
158
- public Annotation getAnnotation () {
159
- return annotation ;
160
- }
161
-
162
- public Class <?> getFieldType () {
163
- return fieldType ;
164
- }
165
-
166
- public boolean equals (Object o ) {
167
- if (!(o instanceof FieldFormatterKey )) {
168
- return false ;
169
- }
170
- FieldFormatterKey key = (FieldFormatterKey ) o ;
171
- return this .annotation .equals (key .annotation ) && this .fieldType .equals (key .fieldType );
172
- }
173
-
174
- public int hashCode () {
175
- return this .annotation .hashCode () + 29 * this .fieldType .hashCode ();
176
- }
177
- }
178
-
179
-
180
103
private static class PrinterConverter implements GenericConverter {
181
104
182
105
private Class <?> fieldType ;
@@ -216,7 +139,6 @@ public String toString() {
216
139
}
217
140
}
218
141
219
-
220
142
private static class ParserConverter implements GenericConverter {
221
143
222
144
private Class <?> fieldType ;
@@ -259,4 +181,114 @@ public String toString() {
259
181
}
260
182
}
261
183
184
+ private final class AnnotationPrinterConverter implements ConditionalGenericConverter {
185
+
186
+ private Class <? extends Annotation > annotationType ;
187
+
188
+ private AnnotationFormatterFactory annotationFormatterFactory ;
189
+
190
+ private Class <?> fieldType ;
191
+
192
+ public AnnotationPrinterConverter (Class <? extends Annotation > annotationType ,
193
+ AnnotationFormatterFactory annotationFormatterFactory , Class <?> fieldType ) {
194
+ this .annotationType = annotationType ;
195
+ this .annotationFormatterFactory = annotationFormatterFactory ;
196
+ this .fieldType = fieldType ;
197
+ }
198
+
199
+ public Set <ConvertiblePair > getConvertibleTypes () {
200
+ return Collections .singleton (new ConvertiblePair (fieldType , String .class ));
201
+ }
202
+
203
+ public boolean matches (TypeDescriptor sourceType , TypeDescriptor targetType ) {
204
+ return sourceType .getAnnotation (annotationType ) != null ;
205
+ }
206
+
207
+ public Object convert (Object source , TypeDescriptor sourceType , TypeDescriptor targetType ) {
208
+ AnnotationConverterKey converterKey = new AnnotationConverterKey (sourceType .getAnnotation (annotationType ), sourceType .getObjectType ());
209
+ GenericConverter converter = cachedPrinters .get (converterKey );
210
+ if (converter == null ) {
211
+ Printer <?> printer = annotationFormatterFactory .getPrinter (converterKey .getAnnotation (), converterKey .getFieldType ());
212
+ converter = new PrinterConverter (fieldType , printer , FormattingConversionService .this );
213
+ cachedPrinters .put (converterKey , converter );
214
+ }
215
+ return converter .convert (source , sourceType , targetType );
216
+ }
217
+
218
+ public String toString () {
219
+ return "@" + annotationType .getName () + " " + fieldType .getName () + " -> " + String .class .getName () + ": " + annotationFormatterFactory ;
220
+ }
221
+ }
222
+
223
+ private final class AnnotationParserConverter implements ConditionalGenericConverter {
224
+
225
+ private Class <? extends Annotation > annotationType ;
226
+
227
+ private AnnotationFormatterFactory annotationFormatterFactory ;
228
+
229
+ private Class <?> fieldType ;
230
+
231
+ public AnnotationParserConverter (Class <? extends Annotation > annotationType ,
232
+ AnnotationFormatterFactory <?> annotationFormatterFactory , Class <?> fieldType ) {
233
+ this .annotationType = annotationType ;
234
+ this .annotationFormatterFactory = annotationFormatterFactory ;
235
+ this .fieldType = fieldType ;
236
+ }
237
+
238
+ public Set <ConvertiblePair > getConvertibleTypes () {
239
+ return Collections .singleton (new ConvertiblePair (String .class , fieldType ));
240
+ }
241
+
242
+ public boolean matches (TypeDescriptor sourceType , TypeDescriptor targetType ) {
243
+ return targetType .getAnnotation (annotationType ) != null ;
244
+ }
245
+
246
+ public Object convert (Object source , TypeDescriptor sourceType , TypeDescriptor targetType ) {
247
+ AnnotationConverterKey converterKey = new AnnotationConverterKey (targetType .getAnnotation (annotationType ), targetType .getObjectType ());
248
+ GenericConverter converter = cachedParsers .get (converterKey );
249
+ if (converter == null ) {
250
+ Parser <?> parser = annotationFormatterFactory .getParser (converterKey .getAnnotation (), converterKey .getFieldType ());
251
+ converter = new ParserConverter (fieldType , parser , FormattingConversionService .this );
252
+ cachedParsers .put (converterKey , converter );
253
+ }
254
+ return converter .convert (source , sourceType , targetType );
255
+ }
256
+
257
+ public String toString () {
258
+ return String .class .getName () + " -> @" + annotationType .getName () + " " + fieldType .getName () + ": " + annotationFormatterFactory ;
259
+ }
260
+ }
261
+
262
+ private static final class AnnotationConverterKey {
263
+
264
+ private final Annotation annotation ;
265
+
266
+ private final Class <?> fieldType ;
267
+
268
+ public AnnotationConverterKey (Annotation annotation , Class <?> fieldType ) {
269
+ this .annotation = annotation ;
270
+ this .fieldType = fieldType ;
271
+ }
272
+
273
+ public Annotation getAnnotation () {
274
+ return annotation ;
275
+ }
276
+
277
+ public Class <?> getFieldType () {
278
+ return fieldType ;
279
+ }
280
+
281
+ public boolean equals (Object o ) {
282
+ if (!(o instanceof AnnotationConverterKey )) {
283
+ return false ;
284
+ }
285
+ AnnotationConverterKey key = (AnnotationConverterKey ) o ;
286
+ return this .annotation .equals (key .annotation ) && this .fieldType .equals (key .fieldType );
287
+ }
288
+
289
+ public int hashCode () {
290
+ return this .annotation .hashCode () + 29 * this .fieldType .hashCode ();
291
+ }
292
+ }
293
+
262
294
}
0 commit comments