01: package org.strecks.converter.handler;
02:
03: import java.lang.reflect.Type;
04:
05: import org.strecks.converter.Converter;
06: import org.strecks.exceptions.ApplicationRuntimeException;
07: import org.strecks.exceptions.ConversionException;
08: import org.strecks.exceptions.ConversionRuntimeException;
09: import org.strecks.util.PropertyValueGetter;
10: import org.strecks.util.ReflectHelper;
11:
12: /**
13: * Implementation of <code>ConversionHandler</code>. Provides rigourous trapping of
14: * <code>ClassCastExceptions</code>
15: * @author Phil Zoio
16: */
17: public class DefaultConversionHandler implements ConversionHandler {
18:
19: /**
20: * Handles conversion from the source property where the possibility exists that the type of the
21: * source property to be converted may be incompatible with the <code>Converter</code>
22: * parameterization type
23: */
24: @SuppressWarnings("unchecked")
25: public Object getAndConvertInwards(Object source,
26: String sourcePropertyName, Converter converter)
27: throws ConversionRuntimeException {
28: Object propertyValue = PropertyValueGetter.getPropertyValue(
29: source, sourcePropertyName);
30: try {
31: Object targetValue = converter.toTargetType(propertyValue);
32: return targetValue;
33: } catch (ClassCastException e) {
34: Class<?> sourceType = PropertyValueGetter
35: .getPropertyTypeSilently(source, sourcePropertyName);
36:
37: Type[] genericTypes = ReflectHelper.getGenericTypes(
38: converter.getClass(), Converter.class);
39: String description = ReflectHelper
40: .getTypeArrayDescription(genericTypes);
41:
42: throw new ApplicationRuntimeException(
43: "Mismatch between parameterization type of converter: "
44: + converter.getClass().getName()
45: + "("
46: + description
47: + ")), and source type of property being converted: "
48: + source.getClass().getName()
49: + ", property " + sourcePropertyName + " ("
50: + sourceType + ")", e);
51: } catch (ConversionException e) {
52: throw new ConversionRuntimeException(e);
53: }
54: }
55:
56: /**
57: * Handles conversion from the target property where the possibility exists that the type of the
58: * source property to be converted may be incompatible with the <code>Converter</code>
59: * parameterization type
60: */
61: @SuppressWarnings("unchecked")
62: public Object getAndConvertOutwards(Object targetBean,
63: String propertyName, Converter converter)
64: throws ConversionRuntimeException {
65: Object targetValue = PropertyValueGetter.getPropertyValue(
66: targetBean, propertyName);
67:
68: try {
69: Object propertyValue = converter.toSourceType(targetValue);
70: return propertyValue;
71: } catch (ClassCastException e) {
72: Class<?> sourceType = PropertyValueGetter
73: .getPropertyTypeSilently(targetBean, propertyName);
74:
75: Type[] genericTypes = ReflectHelper.getGenericTypes(
76: converter.getClass(), Converter.class);
77: String description = ReflectHelper
78: .getTypeArrayDescription(genericTypes);
79:
80: throw new ApplicationRuntimeException(
81: "Mismatch between parameterization type of converter: "
82: + converter.getClass().getName()
83: + "("
84: + description
85: + ")), and source type of property being converted: "
86: + targetBean.getClass().getName()
87: + ", property " + propertyName + " ("
88: + sourceType + ")", e);
89: } catch (ConversionException e) {
90: throw new ConversionRuntimeException(e);
91: }
92: }
93: }
|