1 16 17 18 package org.apache.commons.beanutils; 19 20 21 22 23 24 46 47 public class WrapDynaBean implements DynaBean { 48 49 50 52 53 59 public WrapDynaBean(Object instance) { 60 61 super(); 62 this.instance = instance; 63 this.dynaClass = WrapDynaClass.createDynaClass(instance.getClass()); 64 65 } 66 67 68 70 71 75 protected WrapDynaClass dynaClass = null; 76 77 78 81 protected Object instance = null; 82 83 84 86 87 97 public boolean contains(String name, String key) { 98 99 throw new UnsupportedOperationException 100 ("WrapDynaBean does not support contains()"); 101 102 } 103 104 105 113 public Object get(String name) { 114 115 Object value = null; 116 try { 117 value = PropertyUtils.getSimpleProperty(instance, name); 118 } catch (Throwable t) { 119 throw new IllegalArgumentException 120 ("Property '" + name + "' has no read method"); 121 } 122 return (value); 123 124 } 125 126 127 142 public Object get(String name, int index) { 143 144 Object value = null; 145 try { 146 value = PropertyUtils.getIndexedProperty(instance, name, index); 147 } catch (IndexOutOfBoundsException e) { 148 throw e; 149 } catch (Throwable t) { 150 throw new IllegalArgumentException 151 ("Property '" + name + "' has no indexed read method"); 152 } 153 return (value); 154 155 } 156 157 158 170 public Object get(String name, String key) { 171 172 Object value = null; 173 try { 174 value = PropertyUtils.getMappedProperty(instance, name, key); 175 } catch (Throwable t) { 176 throw new IllegalArgumentException 177 ("Property '" + name + "' has no mapped read method"); 178 } 179 return (value); 180 181 } 182 183 184 188 public DynaClass getDynaClass() { 189 190 return (this.dynaClass); 191 192 } 193 194 195 206 public void remove(String name, String key) { 207 208 209 throw new UnsupportedOperationException 210 ("WrapDynaBean does not support remove()"); 211 212 } 213 214 215 228 public void set(String name, Object value) { 229 230 try { 231 PropertyUtils.setSimpleProperty(instance, name, value); 232 } catch (Throwable t) { 233 throw new IllegalArgumentException 234 ("Property '" + name + "' has no write method"); 235 } 236 237 } 238 239 240 256 public void set(String name, int index, Object value) { 257 258 try { 259 PropertyUtils.setIndexedProperty(instance, name, index, value); 260 } catch (IndexOutOfBoundsException e) { 261 throw e; 262 } catch (Throwable t) { 263 throw new IllegalArgumentException 264 ("Property '" + name + "' has no indexed write method"); 265 } 266 267 } 268 269 270 284 public void set(String name, String key, Object value) { 285 286 try { 287 PropertyUtils.setMappedProperty(instance, name, key, value); 288 } catch (Throwable t) { 289 throw new IllegalArgumentException 290 ("Property '" + name + "' has no mapped write method"); 291 } 292 293 } 294 295 305 public Object getInstance() { 306 return instance; 307 } 308 309 310 312 313 321 protected DynaProperty getDynaProperty(String name) { 322 323 DynaProperty descriptor = getDynaClass().getDynaProperty(name); 324 if (descriptor == null) { 325 throw new IllegalArgumentException 326 ("Invalid property name '" + name + "'"); 327 } 328 return (descriptor); 329 330 } 331 332 333 } 334 | Popular Tags |