KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > reflect > BeanUtilsBeanHelper


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.arooa.reflect;
5
6 import java.lang.reflect.Array JavaDoc;
7 import java.lang.reflect.InvocationTargetException JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import org.apache.commons.beanutils.BeanAccessLanguageException;
12 import org.apache.commons.beanutils.BeanUtilsBean;
13 import org.apache.commons.beanutils.ConversionException;
14 import org.apache.commons.beanutils.Converter;
15 import org.apache.commons.beanutils.PropertyUtils;
16 import org.apache.log4j.Logger;
17 import org.oddjob.arooa.Location;
18
19 /**
20  * BeanUtilsBean with bespoke conversion and ArooaExceptions.
21  */

22 public class BeanUtilsBeanHelper {
23     private static final Logger logger = Logger.getLogger(BeanUtilsBeanHelper.class);
24     
25     private static final Map JavaDoc PRIMITIVE_TYPE_MAP = new HashMap JavaDoc(8);
26       
27     // Set up PRIMITIVE_TYPE_MAP
28
static {
29         Class JavaDoc[] primitives = {Boolean.TYPE, Byte.TYPE, Character.TYPE,
30                               Short.TYPE, Integer.TYPE, Long.TYPE,
31                               Float.TYPE, Double.TYPE};
32         Class JavaDoc[] wrappers = {Boolean JavaDoc.class, Byte JavaDoc.class, Character JavaDoc.class,
33                             Short JavaDoc.class, Integer JavaDoc.class, Long JavaDoc.class,
34                             Float JavaDoc.class, Double JavaDoc.class};
35         for (int i = 0; i < primitives.length; i++) {
36             PRIMITIVE_TYPE_MAP.put (primitives[i], wrappers[i]);
37         }
38     }
39     
40     private final BeanUtilsBean bub;
41     private final Location location;
42     private final String JavaDoc tag;
43
44     /**
45      * Constructor which takes a BeanUtilsBean and tag and location
46      * for exceptions.
47      *
48      * @param bub The BeanUtilsBean.
49      * @param tag The tag.
50      * @param location The location.
51      */

52     public BeanUtilsBeanHelper(BeanUtilsBean bub, String JavaDoc tag, Location location) {
53         if (bub == null) {
54             throw new NullPointerException JavaDoc("BeanUtilsBean must not be null.");
55         }
56         this.location = location;
57         this.tag = tag;
58         this.bub = bub;
59     }
60     
61     /**
62      * Constructor which takes a BeanUtilsBean to wrap.
63      * @param bub
64      */

65     public BeanUtilsBeanHelper(BeanUtilsBean bub) {
66         this(bub, null, null);
67     }
68     
69     /**
70      * Set a property on a bean.
71      *
72      * @param bean The bean. Must not be null.
73      * @param name The name. Must not be null.
74      * @param value The value. Can be null.
75      */

76     public void setProperty(Object JavaDoc bean, String JavaDoc name, Object JavaDoc value) {
77         if (bean == null) {
78             throw new NullPointerException JavaDoc("Bean must not be null!");
79         }
80         if (name == null) {
81             throw new NullPointerException JavaDoc("Property name must not be null");
82         }
83         Class JavaDoc type = getPropertyType(bean, name);
84         logger.debug("Setting property [" + name + "] ("
85                 + type.getName() + ") on ["
86                 + bean.getClass().getName() + "] value [" + value + "]");
87         Object JavaDoc converted = convert(type, value);
88         try {
89             bub.getPropertyUtils().setProperty(bean, name, converted);
90         } catch (IllegalAccessException JavaDoc e) {
91             throw new ArooaPropertyException(bean, name, tag, location, e);
92         } catch (InvocationTargetException JavaDoc e) {
93             throw new ArooaPropertyException(bean, name, tag, location, e);
94         } catch (NoSuchMethodException JavaDoc e) {
95             throw new ArooaNoPropertyException(name, tag, location, bean.getClass());
96         } catch (Exception JavaDoc e) {
97             throw new ArooaPropertyException(bean, name, tag, location, e);
98         }
99     }
100     
101     /**
102      * Set a mapped property on a bean.
103      *
104      * @param bean The bean. Must not be null.
105      * @param name The name. Must not be null.
106      * @param key The mapped property's key. Must not be null.
107      * @param value The value. Can be null.
108      */

109     public void setMappedProperty(Object JavaDoc bean, String JavaDoc name, String JavaDoc key, Object JavaDoc value) {
110         if (bean == null) {
111             throw new NullPointerException JavaDoc("Bean must not be null!");
112         }
113         if (name == null) {
114             throw new NullPointerException JavaDoc("Property name must not be null");
115         }
116         if (key == null) {
117             throw new NullPointerException JavaDoc("Key must not be null");
118         }
119
120         Class JavaDoc type = getPropertyType(bean, name);
121         logger.debug("Setting mapped property [" + name + "] ("
122                 + type.getName() + ") key ["
123              + key + "] on ["
124                 + bean.getClass().getName() + "] value [" + value + "]");
125         Object JavaDoc converted = convert(type, value);
126         try {
127             bub.getPropertyUtils().setMappedProperty(bean, name, key, converted);
128         } catch (IllegalAccessException JavaDoc e) {
129             throw new ArooaPropertyException(bean, name, tag, location, e);
130         } catch (InvocationTargetException JavaDoc e) {
131             throw new ArooaPropertyException(bean, name, tag, location, e);
132         } catch (NoSuchMethodException JavaDoc e) {
133             throw new ArooaNoPropertyException(name, tag, location, bean.getClass());
134         } catch (Exception JavaDoc e) {
135             throw new ArooaPropertyException(bean, name, tag, location, e);
136         }
137     }
138     
139     /**
140      * Get the property type.
141      *
142      * @param bean
143      * @param name
144      * @return
145      */

146     public Class JavaDoc getPropertyType(Object JavaDoc bean, String JavaDoc name) {
147         Class JavaDoc type = null;
148         try {
149             type = bub.getPropertyUtils().getPropertyType(bean, name);
150         } catch (IllegalAccessException JavaDoc e) {
151             throw new ArooaPropertyException(bean, name, tag, location, e);
152         } catch (InvocationTargetException JavaDoc e) {
153             throw new ArooaPropertyException(bean, name, tag, location, e);
154         } catch (NoSuchMethodException JavaDoc e) {
155         throw new ArooaNoPropertyException(name, tag, location, bean.getClass());
156         } catch (Exception JavaDoc e) {
157             throw new ArooaPropertyException(bean, name, tag, location, e);
158         }
159         // could be a DynaBean
160
if (type == null) {
161             return Object JavaDoc.class;
162         }
163         return type;
164     }
165     
166     
167     /**
168      * Get a property.
169      *
170      * @param bean The bean.
171      * @param The property.
172      * @return The property value.
173      */

174     public Object JavaDoc getProperty(Object JavaDoc bean, String JavaDoc property) {
175         try {
176             return PropertyUtils.getProperty(bean, property);
177         } catch (BeanAccessLanguageException e) {
178             throw new ArooaPropertyException(property, e);
179         } catch (IllegalAccessException JavaDoc e) {
180             throw new ArooaPropertyException(property, e);
181         } catch (NoSuchMethodException JavaDoc e) {
182             throw new ArooaPropertyException(property, e);
183         } catch (InvocationTargetException JavaDoc e) {
184             throw new ArooaPropertyException(property, e);
185         }
186     }
187     
188     /**
189      * Get a property of a required type.
190      *
191      * @param bean The bean.
192      * @param The property.
193      * @return The property value.
194      */

195     public Object JavaDoc getProperty(Object JavaDoc bean, String JavaDoc property, Class JavaDoc required) {
196         Object JavaDoc prop = getProperty(bean, property);
197         Object JavaDoc value = IntrospectionHelper.valueFor(prop, required);
198         return convert(required, value);
199     }
200     
201     /**
202      * Resolve a given object into an oject of the required class.
203      *
204      * @param from The object to resolve from.
205      * @param required The required class;
206      * @return A resolved object or null if the from object was null;
207      *
208      * @throws ClassCastException if no resolution is possible.
209      */

210     public Object JavaDoc convert(Class JavaDoc required, Object JavaDoc from)
211             throws ClassCastException JavaDoc {
212         if (from == null) {
213             return null;
214         }
215         
216         if (required.isInstance(from)) {
217             logger.debug("No more conversion as [" + from + "] is already an instance of " + required.getName());
218             return from;
219         }
220         
221         logger.debug("Converting type from " + from.getClass().getName() + " to " + required.getName());
222         
223         Class JavaDoc fromClass = from.getClass();
224         
225         // array conversion
226
if (required.isArray()) {
227             if (fromClass.isArray()) {
228                 Object JavaDoc newArray = Array.newInstance(
229                         required.getComponentType(), Array.getLength(from));
230                 for (int i = 0; i < Array.getLength(from); ++i) {
231                     Array.set(newArray, i,
232                             convert(required.getComponentType(), Array.get(from, i)));
233                 }
234                 return newArray;
235             }
236             else {
237                 Object JavaDoc newFrom = Array.newInstance(fromClass, 1);
238                 Array.set(newFrom, 0, from);
239                 return convert(required, newFrom);
240             }
241         
242         }
243         else {
244             if (fromClass.isArray()) {
245                 if (Array.getLength(from) != 1) {
246                     throw new ClassCastException JavaDoc("Unable to convert from a type \"" +
247                             fromClass.getName() + "\" + to a type \"" +
248                                     required.getName() + "\" as it does not contain exactly 1 element.");
249                 }
250                 return convert(required, Array.get(from, 0));
251             }
252         }
253         
254         // try a converter first
255
Converter converter = bub.getConvertUtils().lookup(required);
256         if (converter != null) {
257             Object JavaDoc converted = null;
258             try {
259                 converted = converter.convert(required, from);
260             } catch (ConversionException e) {
261                 throw new ClassCastException JavaDoc("Unable to convert [" +
262                         from + "] from a type of \"" +
263                         from.getClass().getName() + "\" to a type of \"" +
264                                 required.getName() + "\"");
265             }
266             if (required.isPrimitive()) {
267                 return converted;
268             }
269             return convert(required, converted);
270         }
271         
272         throw new ClassCastException JavaDoc("Unable to convert from a type of \"" +
273                 from.getClass().getName() + "\" to a type of \"" +
274                         required.getName() + "\"");
275     }
276
277 }
278
Popular Tags