1 16 17 18 package org.apache.commons.beanutils; 19 20 21 import java.io.Serializable ; 22 import java.lang.reflect.Constructor ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.util.HashMap ; 25 26 27 38 39 public class BasicDynaClass implements DynaClass, Serializable { 40 41 42 44 45 48 public BasicDynaClass() { 49 50 this(null, null, null); 51 52 } 53 54 55 61 public BasicDynaClass(String name, Class dynaBeanClass) { 62 63 this(name, dynaBeanClass, null); 64 65 } 66 67 68 75 public BasicDynaClass(String name, Class dynaBeanClass, 76 DynaProperty properties[]) { 77 78 super(); 79 if (name != null) 80 this.name = name; 81 if (dynaBeanClass == null) 82 dynaBeanClass = BasicDynaBean.class; 83 setDynaBeanClass(dynaBeanClass); 84 if (properties != null) 85 setProperties(properties); 86 87 } 88 89 90 92 93 97 protected transient Constructor constructor = null; 98 99 100 104 protected static Class constructorTypes[] = { DynaClass.class }; 105 106 107 111 protected Object constructorValues[] = { this }; 112 113 114 118 protected Class dynaBeanClass = BasicDynaBean.class; 119 120 121 124 protected String name = this.getClass().getName(); 125 126 127 130 protected DynaProperty properties[] = new DynaProperty[0]; 131 132 133 138 protected HashMap propertiesMap = new HashMap (); 139 140 141 143 144 150 public String getName() { 151 152 return (this.name); 153 154 } 155 156 157 166 public DynaProperty getDynaProperty(String name) { 167 168 if (name == null) { 169 throw new IllegalArgumentException 170 ("No property name specified"); 171 } 172 return ((DynaProperty) propertiesMap.get(name)); 173 174 } 175 176 177 186 public DynaProperty[] getDynaProperties() { 187 188 return (properties); 189 190 } 191 192 193 203 public DynaBean newInstance() 204 throws IllegalAccessException , InstantiationException { 205 206 try { 207 if (constructor == null) { 209 setDynaBeanClass(this.dynaBeanClass); 210 } 211 return ((DynaBean) constructor.newInstance(constructorValues)); 213 } catch (InvocationTargetException e) { 214 throw new InstantiationException 215 (e.getTargetException().getMessage()); 216 } 217 218 } 219 220 221 223 224 229 public Class getDynaBeanClass() { 230 231 return (this.dynaBeanClass); 232 233 } 234 235 236 238 239 249 protected void setDynaBeanClass(Class dynaBeanClass) { 250 251 if (dynaBeanClass.isInterface()) 253 throw new IllegalArgumentException 254 ("Class " + dynaBeanClass.getName() + 255 " is an interface, not a class"); 256 if (!DynaBean.class.isAssignableFrom(dynaBeanClass)) 257 throw new IllegalArgumentException 258 ("Class " + dynaBeanClass.getName() + 259 " does not implement DynaBean"); 260 261 try { 263 this.constructor = dynaBeanClass.getConstructor(constructorTypes); 264 } catch (NoSuchMethodException e) { 265 throw new IllegalArgumentException 266 ("Class " + dynaBeanClass.getName() + 267 " does not have an appropriate constructor"); 268 } 269 this.dynaBeanClass = dynaBeanClass; 270 271 } 272 273 274 279 protected void setProperties(DynaProperty properties[]) { 280 281 this.properties = properties; 282 propertiesMap.clear(); 283 for (int i = 0; i < properties.length; i++) { 284 propertiesMap.put(properties[i].getName(), properties[i]); 285 } 286 287 } 288 289 290 } 291 | Popular Tags |