1 3 package jodd.bean; 4 5 import jodd.typeconverter.TypeConverter; 6 import jodd.typeconverter.TypeConverterManager; 7 import jodd.util.ReflectUtil; 8 9 import java.lang.reflect.Method ; 10 import java.lang.reflect.InvocationTargetException ; 11 import java.lang.reflect.Array ; 12 import java.lang.reflect.Field ; 13 import java.util.HashMap ; 14 import java.util.Map ; 15 import java.util.List ; 16 17 21 public class BeanPropertyUtil { 22 23 25 29 public static void invokeSetter(Object bean, Method m, Object value) { 30 Class [] paramTypes = m.getParameterTypes(); 31 TypeConverter objconv = TypeConverterManager.lookup(paramTypes[0]); 32 if (objconv != null) { 33 value = objconv.convert(value); 34 } 35 try { 36 m.invoke(bean, new Object [] {value}); 37 } catch (IllegalAccessException iaex) { 38 throw new BeanException("Unable to access '" + bean.getClass().getName() + '#' + m.getName() + "()'", iaex); 39 } catch (InvocationTargetException itex) { 40 throw new BeanException("Unable to invoke '" + bean.getClass().getName() + '#' + m.getName() + "()'", itex); 41 } 42 } 43 44 47 public static boolean invokeSetterSilent(Object bean, Method m, Object value) { 48 try { 49 Class [] paramTypes = m.getParameterTypes(); 50 TypeConverter objconv = TypeConverterManager.lookup(paramTypes[0]); 51 if (objconv != null) { 52 value = objconv.convert(value); 53 } 54 m.invoke(bean, new Object [] {value}); 55 return true; 56 } catch (Exception ex) { 57 return false; 58 } 59 } 60 61 65 public static Object invokeGetter(Object bean, Method m) { 66 try { 67 return m.invoke(bean, new Object [] {}); 68 } catch (IllegalAccessException iaex) { 69 throw new BeanException("Unable to access '" + bean.getClass().getName() + '#' + m.getName() + "()'", iaex); 70 } catch (InvocationTargetException itex) { 71 throw new BeanException("Unable to invoke '" + bean.getClass().getName() + '#' + m.getName() + "()'", itex); 72 } 73 } 74 75 78 public static void setField(Object bean, Field f, Object value) { 79 TypeConverter objconv = TypeConverterManager.lookup(f.getType()); 80 if (objconv != null) { 81 value = objconv.convert(value); 82 } 83 try { 84 f.set(bean, value); 85 } catch (IllegalAccessException iaex) { 86 throw new BeanException("Unable to access '" + bean.getClass().getName() + '#' + f.getName() + '\'', iaex); 87 } 88 } 89 90 93 public static boolean setFieldSilent(Object bean, Field f, Object value) { 94 try { 95 TypeConverter objconv = TypeConverterManager.lookup(f.getType()); 96 if (objconv != null) { 97 value = objconv.convert(value); 98 } 99 f.set(bean, value); 100 return true; 101 } catch (Exception ex) { 102 return false; 103 } 104 } 105 106 107 110 public static Object getField(Object bean, Field f) { 111 try { 112 return f.get(bean); 113 } catch (IllegalAccessException iaex) { 114 throw new BeanException("Unable to access '" + bean.getClass().getName() + '#' + f.getName() + '\'', iaex); 115 } 116 } 117 118 120 124 public static Object arrayForcedGet(BeanProperty beanProperty, Object array, int index) { 125 if (beanProperty.last == false) { 126 int len = Array.getLength(array); 127 if (index >= len) { 128 Object newArray = Array.newInstance(array.getClass().getComponentType(), index + 1); 129 System.arraycopy(array, 0, newArray, 0, len); 130 Method setter = beanProperty.cd.getBeanSetter(beanProperty.name, true); 131 if (setter != null) { 132 invokeSetter(beanProperty.bean, setter, newArray); 133 } else { 134 Field field = beanProperty.cd.getField(beanProperty.name, true); 135 if (field == null) { 136 throw new BeanException("Unable to find setter or field '" + beanProperty + '\''); 137 } 138 setField(beanProperty.bean, field, newArray); 139 } 140 array = newArray; 141 } 142 } 143 Object value = Array.get(array, index); 144 if (value == null) { 145 Class componentType = array.getClass().getComponentType(); 146 try { 147 value = ReflectUtil.newInstance(componentType); 148 } catch (InstantiationException inex) { 149 throw new BeanException("Unable to instantiate array element '" + beanProperty + '[' + index + "]'", inex); 150 } catch (IllegalAccessException iaex) { 151 throw new BeanException("Unable to access array element '" + beanProperty + '[' + index + "]'", iaex); 152 } 153 Array.set(array, index, value); 154 } 155 return value; 156 } 157 158 162 public static void arrayForcedSet(BeanProperty beanProperty, Object array, int index, Object value) { 163 int len = Array.getLength(array); 164 Class componentType = array.getClass().getComponentType(); 165 if (index >= len) { 166 Object newArray = Array.newInstance(componentType, index + 1); 167 System.arraycopy(array, 0, newArray, 0, len); 168 Method setter = beanProperty.cd.getBeanSetter(beanProperty.name, true); 169 if (setter != null) { 170 invokeSetter(beanProperty.bean, setter, newArray); 171 } else { 172 Field field = beanProperty.cd.getField(beanProperty.name, true); 173 if (field == null) { 174 throw new BeanException("Unable to find setter or field '" + beanProperty + '\''); 175 } 176 setField(beanProperty.bean, field, newArray); 177 } 178 array = newArray; 179 } 180 TypeConverter objconv = TypeConverterManager.lookup(componentType); 181 if (objconv != null) { 182 value = objconv.convert(value); 183 } 184 Array.set(array, index, value); 185 } 186 187 191 public static Object listForcedGet(BeanProperty beanProperty, List list, int index) { 192 if (beanProperty.last == false) { 193 int len = list.size(); 194 while (index >= len) { 195 list.add(null); 196 len++; 197 } 198 } 199 Object value = list.get(index); 200 if (value == null) { 201 value = new HashMap (); 202 list.remove(index); 203 list.add(index, value); 204 } 205 return value; 206 } 207 208 211 public static void listForcedSet(List list, int index, Object value) { 212 int len = list.size(); 213 while (index >= len) { 214 list.add(null); 215 len++; 216 } 217 list.set(index, value); 218 } 219 220 221 226 public static Object mapForcedGet(BeanProperty bp, Map map, String key) { 227 Object value = map.get(key); 228 if (bp.last == false) { 229 if (value == null) { 230 value = new HashMap (); 231 map.put(key, value); 232 } 233 } 234 return value; 235 } 236 237 238 240 241 246 public static String extractIndex(BeanProperty beanProperty) { 247 String name = beanProperty.name; 248 int lastNdx = name.length() - 1; 249 if (name.charAt(lastNdx) == ']') { 250 int leftBracketNdx = name.lastIndexOf('['); 251 if (leftBracketNdx != -1) { 252 beanProperty.name = name.substring(0, leftBracketNdx); 253 return name.substring(leftBracketNdx + 1, lastNdx); 254 } 255 } 256 return null; 257 } 258 259 261 265 public static Object createBeanProperty(BeanProperty beanProperty) { 266 Method setter = beanProperty.cd.getBeanSetter(beanProperty.name, true); 267 Field field = null; 268 Class type; 269 if (setter != null) { 270 type = setter.getParameterTypes()[0]; 271 } else { 272 field = beanProperty.cd.getField(beanProperty.name, true); 273 if (field == null) { 274 return null; 275 } 276 type = field.getType(); 277 } 278 Object newInstance; 279 try { 280 newInstance = ReflectUtil.newInstance(type); 281 if (newInstance == null) { 282 newInstance = type.newInstance(); 283 } 284 } catch (InstantiationException iex) { 285 throw new BeanException("Unable to instantiate '" + beanProperty + "' through its setter."); 286 } catch (IllegalAccessException iaex) { 287 throw new BeanException("Unable to access '" + beanProperty + "' through its setter."); 288 } 289 if (setter != null) { 290 invokeSetter(beanProperty.bean, setter, newInstance); 291 } else { 292 setField(beanProperty.bean, field, newInstance); 293 } 294 return newInstance; 295 } 296 297 300 public static Map createMapProperty(Map destination, String name) { 301 Map value = new HashMap (); 302 destination.put(name, value); 303 return value; 304 } 305 306 307 } 308 | Popular Tags |