1 16 17 package org.apache.commons.beanutils; 18 19 20 import java.beans.PropertyDescriptor ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 26 44 45 public class WrapDynaClass implements DynaClass { 46 47 48 50 51 58 private WrapDynaClass(Class beanClass) { 59 60 this.beanClass = beanClass; 61 introspect(); 62 63 } 64 65 66 68 69 73 protected Class beanClass = null; 74 75 76 79 protected PropertyDescriptor descriptors[] = null; 80 81 82 87 protected HashMap descriptorsMap = new HashMap (); 88 89 90 93 protected DynaProperty properties[] = null; 94 95 96 101 protected HashMap propertiesMap = new HashMap (); 102 103 104 106 107 111 protected static HashMap dynaClasses = new HashMap (); 112 113 114 116 117 123 public String getName() { 124 125 return (this.beanClass.getName()); 126 127 } 128 129 130 139 public DynaProperty getDynaProperty(String name) { 140 141 if (name == null) { 142 throw new IllegalArgumentException 143 ("No property name specified"); 144 } 145 return ((DynaProperty) propertiesMap.get(name)); 146 147 } 148 149 150 159 public DynaProperty[] getDynaProperties() { 160 161 return (properties); 162 163 } 164 165 166 190 public DynaBean newInstance() 191 throws IllegalAccessException , InstantiationException { 192 193 return new WrapDynaBean(beanClass.newInstance()); 194 195 } 196 197 198 200 201 207 public PropertyDescriptor getPropertyDescriptor(String name) { 208 209 return ((PropertyDescriptor ) descriptorsMap.get(name)); 210 211 } 212 213 214 216 217 220 public static void clear() { 221 222 synchronized (dynaClasses) { 223 dynaClasses.clear(); 224 } 225 226 } 227 228 229 235 public static WrapDynaClass createDynaClass(Class beanClass) { 236 237 synchronized (dynaClasses) { 238 WrapDynaClass dynaClass = 239 (WrapDynaClass) dynaClasses.get(beanClass); 240 if (dynaClass == null) { 241 dynaClass = new WrapDynaClass(beanClass); 242 dynaClasses.put(beanClass, dynaClass); 243 } 244 return (dynaClass); 245 } 246 247 } 248 249 250 252 253 256 protected void introspect() { 257 258 PropertyDescriptor regulars[] = 260 PropertyUtils.getPropertyDescriptors(beanClass); 261 if (regulars == null) { 262 regulars = new PropertyDescriptor [0]; 263 } 264 HashMap mappeds = 265 PropertyUtils.getMappedPropertyDescriptors(beanClass); 266 if (mappeds == null) { 267 mappeds = new HashMap (); 268 } 269 270 properties = new DynaProperty[regulars.length + mappeds.size()]; 272 for (int i = 0; i < regulars.length; i++) { 273 descriptorsMap.put(regulars[i].getName(), 274 regulars[i]); 275 properties[i] = 276 new DynaProperty(regulars[i].getName(), 277 regulars[i].getPropertyType()); 278 propertiesMap.put(properties[i].getName(), 279 properties[i]); 280 } 281 int j = regulars.length; 282 Iterator names = mappeds.keySet().iterator(); 283 while (names.hasNext()) { 284 String name = (String ) names.next(); 285 PropertyDescriptor descriptor = 286 (PropertyDescriptor ) mappeds.get(name); 287 properties[j] = 288 new DynaProperty(descriptor.getName(), 289 Map .class); 290 propertiesMap.put(properties[j].getName(), 291 properties[j]); 292 j++; 293 } 294 295 } 296 297 298 } 299 | Popular Tags |