1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils2.converters;
18
19 import java.lang.reflect.Array;
20 import java.util.Collection;
21 import java.util.Locale;
22 import java.util.Objects;
23
24 import org.apache.commons.beanutils2.ConversionException;
25 import org.apache.commons.beanutils2.ConvertUtils;
26 import org.apache.commons.beanutils2.Converter;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public abstract class AbstractConverter<D> implements Converter<D> {
53
54
55 private static final String DEFAULT_CONFIG_MSG = "(N.B. Converters can be configured to use default values to avoid throwing exceptions)";
56
57
58
59
60 private static final String PACKAGE = "org.apache.commons.beanutils2.converters.";
61
62
63
64
65
66
67
68 protected static String toLowerCase(final Object value) {
69 return toString(value).toLowerCase(Locale.ROOT);
70 }
71
72
73
74
75
76
77
78 protected static String toString(final Object value) {
79 return Objects.requireNonNull(value, "value").toString();
80 }
81
82
83
84
85
86
87
88 protected static String toTrim(final Object value) {
89 return toString(value).trim();
90 }
91
92
93
94
95 private transient Log log;
96
97
98
99
100 private boolean useDefault;
101
102
103
104
105 private D defaultValue;
106
107
108
109
110 public AbstractConverter() {
111 }
112
113
114
115
116
117
118 public AbstractConverter(final D defaultValue) {
119 setDefaultValue(defaultValue);
120 }
121
122
123
124
125
126
127
128
129
130 protected ConversionException conversionException(final Class<?> type, final Object value) {
131 return ConversionException.format("Can't convert value '%s' to type %s", value, type);
132 }
133
134
135
136
137
138
139
140
141
142 @Override
143 public <R> R convert(final Class<R> type, Object value) {
144 if (type == null) {
145 return convertToDefaultType(value);
146 }
147
148 Class<?> sourceType = value == null ? null : value.getClass();
149 final Class<R> targetType = ConvertUtils.primitiveToWrapper(type);
150
151 if (log().isDebugEnabled()) {
152 log().debug(
153 "Converting" + (value == null ? "" : " '" + toString(sourceType) + "'") + " value '" + value + "' to type '" + toString(targetType) + "'");
154 }
155
156 value = convertArray(value);
157
158
159 if (value == null) {
160 return handleMissing(targetType);
161 }
162
163 sourceType = value.getClass();
164
165 try {
166
167 if (targetType.equals(String.class)) {
168 return targetType.cast(convertToString(value));
169
170
171 }
172 if (targetType.equals(sourceType)) {
173 if (log().isDebugEnabled()) {
174 log().debug(" No conversion required, value is already a " + toString(targetType));
175 }
176 return targetType.cast(value);
177
178
179 }
180 final Object result = convertToType(targetType, value);
181 if (log().isDebugEnabled()) {
182 log().debug(" Converted to " + toString(targetType) + " value '" + result + "'");
183 }
184 return targetType.cast(result);
185 } catch (final Throwable t) {
186 return handleError(targetType, value, t);
187 }
188 }
189
190
191
192
193
194
195
196
197
198 protected Object convertArray(final Object value) {
199 if (value == null) {
200 return null;
201 }
202 if (value.getClass().isArray()) {
203 if (Array.getLength(value) > 0) {
204 return Array.get(value, 0);
205 }
206 return null;
207 }
208 if (value instanceof Collection) {
209 final Collection<?> collection = (Collection<?>) value;
210 if (!collection.isEmpty()) {
211 return collection.iterator().next();
212 }
213 return null;
214 }
215 return value;
216 }
217
218
219
220
221
222
223
224
225
226 @SuppressWarnings("unchecked")
227 private <T> T convertToDefaultType(final Object value) {
228 return (T) convert(getDefaultType(), value);
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242 protected String convertToString(final Object value) {
243 return value.toString();
244 }
245
246
247
248
249
250
251
252
253
254
255
256
257
258 protected abstract <R> R convertToType(Class<R> type, Object value) throws Throwable;
259
260
261
262
263
264
265
266 protected Object getDefault(final Class<?> type) {
267 if (type.equals(String.class)) {
268 return null;
269 }
270 return defaultValue;
271 }
272
273
274
275
276
277
278 protected abstract Class<D> getDefaultType();
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 protected <T> T handleError(final Class<T> type, final Object value, final Throwable cause) {
294 if (log().isDebugEnabled()) {
295 if (cause instanceof ConversionException) {
296 log().debug(" Conversion threw ConversionException: " + cause.getMessage());
297 } else {
298 log().debug(" Conversion threw " + cause);
299 }
300 }
301 if (useDefault) {
302 return handleMissing(type);
303 }
304 ConversionException cex = null;
305 if (cause instanceof ConversionException) {
306 cex = (ConversionException) cause;
307 if (log().isDebugEnabled()) {
308 log().debug(" Re-throwing ConversionException: " + cex.getMessage());
309 log().debug(" " + DEFAULT_CONFIG_MSG);
310 }
311 } else {
312 final String msg = "Error converting from '" + toString(value.getClass()) + "' to '" + toString(type) + "' " + cause.getMessage();
313 cex = new ConversionException(msg, cause);
314 if (log().isDebugEnabled()) {
315 log().debug(" Throwing ConversionException: " + msg);
316 log().debug(" " + DEFAULT_CONFIG_MSG);
317 }
318 }
319 throw cex;
320 }
321
322
323
324
325
326
327
328
329
330
331
332
333 protected <T> T handleMissing(final Class<T> type) {
334 if (useDefault || type.equals(String.class)) {
335 Object value = getDefault(type);
336 if (useDefault && value != null && !type.equals(value.getClass())) {
337 try {
338 value = convertToType(type, defaultValue);
339 } catch (final Throwable t) {
340 throw new ConversionException("Default conversion to " + toString(type) + " failed.", t);
341 }
342 }
343 if (log().isDebugEnabled()) {
344 log().debug(" Using default " + (value == null ? "" : toString(value.getClass()) + " ") + "value '" + defaultValue + "'");
345 }
346
347 return type.cast(value);
348 }
349
350 final ConversionException cex = ConversionException.format("No value specified for '%s'", toString(type));
351 if (log().isDebugEnabled()) {
352 log().debug(" Throwing ConversionException: " + cex.getMessage());
353 log().debug(" " + DEFAULT_CONFIG_MSG);
354 }
355 throw cex;
356 }
357
358
359
360
361
362
363 public boolean isUseDefault() {
364 return useDefault;
365 }
366
367
368
369
370
371
372
373
374
375 Log log() {
376 if (log == null) {
377 log = LogFactory.getLog(getClass());
378 }
379 return log;
380 }
381
382
383
384
385
386
387
388
389
390
391 protected void setDefaultValue(final D defaultValue) {
392 useDefault = false;
393 if (log().isDebugEnabled()) {
394 log().debug("Setting default value: " + defaultValue);
395 }
396 if (defaultValue == null) {
397 this.defaultValue = null;
398 } else {
399 this.defaultValue = convert(getDefaultType(), defaultValue);
400 }
401 useDefault = true;
402 }
403
404
405
406
407
408
409 @Override
410 public String toString() {
411 return toString(getClass()) + "[UseDefault=" + useDefault + "]";
412 }
413
414
415
416
417
418
419
420 String toString(final Class<?> type) {
421 String typeName = null;
422 if (type == null) {
423 typeName = "null";
424 } else if (type.isArray()) {
425 Class<?> elementType = type.getComponentType();
426 int count = 1;
427 while (elementType.isArray()) {
428 elementType = elementType.getComponentType();
429 count++;
430 }
431 final StringBuilder typeNameBuilder = new StringBuilder(elementType.getName());
432 for (int i = 0; i < count; i++) {
433 typeNameBuilder.append("[]");
434 }
435 typeName = typeNameBuilder.toString();
436 } else {
437 typeName = type.getName();
438 }
439 if (typeName.startsWith("java.lang.") || typeName.startsWith("java.util.") || typeName.startsWith("java.math.")) {
440 typeName = typeName.substring("java.lang.".length());
441 } else if (typeName.startsWith(PACKAGE)) {
442 typeName = typeName.substring(PACKAGE.length());
443 }
444 return typeName;
445 }
446
447 }