1 16 package net.sf.cglib.beans; 17 18 import java.beans.*; 19 import java.lang.reflect.Constructor ; 20 import java.lang.reflect.Method ; 21 import java.util.*; 22 import net.sf.cglib.core.*; 23 import org.objectweb.asm.ClassVisitor; 24 25 33 abstract public class BeanMap implements Map { 34 39 public static final int REQUIRE_GETTER = 1; 40 41 46 public static final int REQUIRE_SETTER = 2; 47 48 55 public static BeanMap create(Object bean) { 56 Generator gen = new Generator(); 57 gen.setBean(bean); 58 return gen.create(); 59 } 60 61 public static class Generator extends AbstractClassGenerator { 62 private static final Source SOURCE = new Source(BeanMap.class.getName()); 63 64 private static final BeanMapKey KEY_FACTORY = 65 (BeanMapKey)KeyFactory.create(BeanMapKey.class, KeyFactory.CLASS_BY_NAME); 66 67 interface BeanMapKey { 68 public Object newInstance(Class type, int require); 69 } 70 71 private Object bean; 72 private Class beanClass; 73 private int require; 74 75 public Generator() { 76 super(SOURCE); 77 } 78 79 86 public void setBean(Object bean) { 87 this.bean = bean; 88 if (bean != null) 89 beanClass = bean.getClass(); 90 } 91 92 97 public void setBeanClass(Class beanClass) { 98 this.beanClass = beanClass; 99 } 100 101 106 public void setRequire(int require) { 107 this.require = require; 108 } 109 110 protected ClassLoader getDefaultClassLoader() { 111 return beanClass.getClassLoader(); 112 } 113 114 118 public BeanMap create() { 119 if (beanClass == null) 120 throw new IllegalArgumentException ("Class of bean unknown"); 121 setNamePrefix(beanClass.getName()); 122 return (BeanMap)super.create(KEY_FACTORY.newInstance(beanClass, require)); 123 } 124 125 public void generateClass(ClassVisitor v) throws Exception { 126 new BeanMapEmitter(v, getClassName(), beanClass, require); 127 } 128 129 protected Object firstInstance(Class type) { 130 return ((BeanMap)ReflectUtils.newInstance(type)).newInstance(bean); 131 } 132 133 protected Object nextInstance(Object instance) { 134 return ((BeanMap)instance).newInstance(bean); 135 } 136 } 137 138 144 abstract public BeanMap newInstance(Object bean); 145 146 151 abstract public Class getPropertyType(String name); 152 153 protected Object bean; 154 155 protected BeanMap() { 156 } 157 158 protected BeanMap(Object bean) { 159 setBean(bean); 160 } 161 162 public Object get(Object key) { 163 return get(bean, key); 164 } 165 166 public Object put(Object key, Object value) { 167 return put(bean, key, value); 168 } 169 170 179 abstract public Object get(Object bean, Object key); 180 181 188 abstract public Object put(Object bean, Object key, Object value); 189 190 195 public void setBean(Object bean) { 196 this.bean = bean; 197 } 198 199 204 public Object getBean() { 205 return bean; 206 } 207 208 public void clear() { 209 throw new UnsupportedOperationException (); 210 } 211 212 public boolean containsKey(Object key) { 213 return keySet().contains(key); 214 } 215 216 public boolean containsValue(Object value) { 217 for (Iterator it = keySet().iterator(); it.hasNext();) { 218 Object v = get(it.next()); 219 if (((value == null) && (v == null)) || value.equals(v)) 220 return true; 221 } 222 return false; 223 } 224 225 public int size() { 226 return keySet().size(); 227 } 228 229 public boolean isEmpty() { 230 return size() == 0; 231 } 232 233 public Object remove(Object key) { 234 throw new UnsupportedOperationException (); 235 } 236 237 public void putAll(Map t) { 238 for (Iterator it = t.keySet().iterator(); it.hasNext();) { 239 Object key = it.next(); 240 put(key, t.get(key)); 241 } 242 } 243 244 public boolean equals(Object o) { 245 if (o == null || !(o instanceof Map)) { 246 return false; 247 } 248 Map other = (Map)o; 249 if (size() != other.size()) { 250 return false; 251 } 252 for (Iterator it = keySet().iterator(); it.hasNext();) { 253 Object key = it.next(); 254 if (!other.containsKey(key)) { 255 return false; 256 } 257 Object v1 = get(key); 258 Object v2 = other.get(key); 259 if (!((v1 == null) ? v2 == null : v1.equals(v2))) { 260 return false; 261 } 262 } 263 return true; 264 } 265 266 public int hashCode() { 267 int code = 0; 268 for (Iterator it = keySet().iterator(); it.hasNext();) { 269 Object key = it.next(); 270 Object value = get(key); 271 code += ((key == null) ? 0 : key.hashCode()) ^ 272 ((value == null) ? 0 : value.hashCode()); 273 } 274 return code; 275 } 276 277 public Set entrySet() { 279 HashMap copy = new HashMap(); 280 for (Iterator it = keySet().iterator(); it.hasNext();) { 281 Object key = it.next(); 282 copy.put(key, get(key)); 283 } 284 return Collections.unmodifiableMap(copy).entrySet(); 285 } 286 287 public Collection values() { 288 Set keys = keySet(); 289 List values = new ArrayList(keys.size()); 290 for (Iterator it = keys.iterator(); it.hasNext();) { 291 values.add(get(it.next())); 292 } 293 return Collections.unmodifiableCollection(values); 294 } 295 296 299 public String toString() 300 { 301 StringBuffer sb = new StringBuffer (); 302 sb.append('{'); 303 for (Iterator it = keySet().iterator(); it.hasNext();) { 304 Object key = it.next(); 305 sb.append(key); 306 sb.append('='); 307 sb.append(get(key)); 308 if (it.hasNext()) { 309 sb.append(", "); 310 } 311 } 312 sb.append('}'); 313 return sb.toString(); 314 } 315 } 316 | Popular Tags |