1 4 package org.oddjob.arooa.reflect; 5 6 import java.lang.reflect.Array ; 7 import java.lang.reflect.InvocationTargetException ; 8 import java.util.HashMap ; 9 import java.util.Map ; 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 22 public class BeanUtilsBeanHelper { 23 private static final Logger logger = Logger.getLogger(BeanUtilsBeanHelper.class); 24 25 private static final Map PRIMITIVE_TYPE_MAP = new HashMap (8); 26 27 static { 29 Class [] primitives = {Boolean.TYPE, Byte.TYPE, Character.TYPE, 30 Short.TYPE, Integer.TYPE, Long.TYPE, 31 Float.TYPE, Double.TYPE}; 32 Class [] wrappers = {Boolean .class, Byte .class, Character .class, 33 Short .class, Integer .class, Long .class, 34 Float .class, Double .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 tag; 43 44 52 public BeanUtilsBeanHelper(BeanUtilsBean bub, String tag, Location location) { 53 if (bub == null) { 54 throw new NullPointerException ("BeanUtilsBean must not be null."); 55 } 56 this.location = location; 57 this.tag = tag; 58 this.bub = bub; 59 } 60 61 65 public BeanUtilsBeanHelper(BeanUtilsBean bub) { 66 this(bub, null, null); 67 } 68 69 76 public void setProperty(Object bean, String name, Object value) { 77 if (bean == null) { 78 throw new NullPointerException ("Bean must not be null!"); 79 } 80 if (name == null) { 81 throw new NullPointerException ("Property name must not be null"); 82 } 83 Class type = getPropertyType(bean, name); 84 logger.debug("Setting property [" + name + "] (" 85 + type.getName() + ") on [" 86 + bean.getClass().getName() + "] value [" + value + "]"); 87 Object converted = convert(type, value); 88 try { 89 bub.getPropertyUtils().setProperty(bean, name, converted); 90 } catch (IllegalAccessException e) { 91 throw new ArooaPropertyException(bean, name, tag, location, e); 92 } catch (InvocationTargetException e) { 93 throw new ArooaPropertyException(bean, name, tag, location, e); 94 } catch (NoSuchMethodException e) { 95 throw new ArooaNoPropertyException(name, tag, location, bean.getClass()); 96 } catch (Exception e) { 97 throw new ArooaPropertyException(bean, name, tag, location, e); 98 } 99 } 100 101 109 public void setMappedProperty(Object bean, String name, String key, Object value) { 110 if (bean == null) { 111 throw new NullPointerException ("Bean must not be null!"); 112 } 113 if (name == null) { 114 throw new NullPointerException ("Property name must not be null"); 115 } 116 if (key == null) { 117 throw new NullPointerException ("Key must not be null"); 118 } 119 120 Class 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 converted = convert(type, value); 126 try { 127 bub.getPropertyUtils().setMappedProperty(bean, name, key, converted); 128 } catch (IllegalAccessException e) { 129 throw new ArooaPropertyException(bean, name, tag, location, e); 130 } catch (InvocationTargetException e) { 131 throw new ArooaPropertyException(bean, name, tag, location, e); 132 } catch (NoSuchMethodException e) { 133 throw new ArooaNoPropertyException(name, tag, location, bean.getClass()); 134 } catch (Exception e) { 135 throw new ArooaPropertyException(bean, name, tag, location, e); 136 } 137 } 138 139 146 public Class getPropertyType(Object bean, String name) { 147 Class type = null; 148 try { 149 type = bub.getPropertyUtils().getPropertyType(bean, name); 150 } catch (IllegalAccessException e) { 151 throw new ArooaPropertyException(bean, name, tag, location, e); 152 } catch (InvocationTargetException e) { 153 throw new ArooaPropertyException(bean, name, tag, location, e); 154 } catch (NoSuchMethodException e) { 155 throw new ArooaNoPropertyException(name, tag, location, bean.getClass()); 156 } catch (Exception e) { 157 throw new ArooaPropertyException(bean, name, tag, location, e); 158 } 159 if (type == null) { 161 return Object .class; 162 } 163 return type; 164 } 165 166 167 174 public Object getProperty(Object bean, String property) { 175 try { 176 return PropertyUtils.getProperty(bean, property); 177 } catch (BeanAccessLanguageException e) { 178 throw new ArooaPropertyException(property, e); 179 } catch (IllegalAccessException e) { 180 throw new ArooaPropertyException(property, e); 181 } catch (NoSuchMethodException e) { 182 throw new ArooaPropertyException(property, e); 183 } catch (InvocationTargetException e) { 184 throw new ArooaPropertyException(property, e); 185 } 186 } 187 188 195 public Object getProperty(Object bean, String property, Class required) { 196 Object prop = getProperty(bean, property); 197 Object value = IntrospectionHelper.valueFor(prop, required); 198 return convert(required, value); 199 } 200 201 210 public Object convert(Class required, Object from) 211 throws ClassCastException { 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 fromClass = from.getClass(); 224 225 if (required.isArray()) { 227 if (fromClass.isArray()) { 228 Object 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 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 ("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 Converter converter = bub.getConvertUtils().lookup(required); 256 if (converter != null) { 257 Object converted = null; 258 try { 259 converted = converter.convert(required, from); 260 } catch (ConversionException e) { 261 throw new ClassCastException ("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 ("Unable to convert from a type of \"" + 273 from.getClass().getName() + "\" to a type of \"" + 274 required.getName() + "\""); 275 } 276 277 } 278 | Popular Tags |