View Javadoc
1 /* 2 * Copyright (c) 2003, Henri Yandell 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or 6 * without modification, are permitted provided that the 7 * following conditions are met: 8 * 9 * + Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * + Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * + Neither the name of Genjava-Core nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package com.generationjava.collections; 33 34 import java.beans.BeanInfo; 35 import java.beans.Introspector; 36 import java.beans.IntrospectionException; 37 import java.beans.PropertyDescriptor; 38 39 import java.lang.reflect.InvocationTargetException; 40 import java.lang.reflect.Method; 41 42 import java.util.Map; 43 import java.util.HashMap; 44 import java.util.Collection; 45 import java.util.Set; 46 import java.util.Iterator; 47 import java.util.LinkedList; 48 import java.util.List; 49 50 import org.apache.commons.collections.CollectionUtils; 51 52 import com.generationjava.beans.AbstractBeanViewer; 53 import com.generationjava.beans.BeanViewRuntime; 54 55 /*** 56 * A Map which wraps a JavaBean. 57 * 58 * @date 2001-05-11 59 */ 60 public class BeanMap extends AbstractBeanViewer implements Map { 61 62 private Object myBean = null; 63 private Map pdCache = new HashMap(); 64 65 /*** 66 * Construct a BeanMap around the supplied Java Bean. 67 */ 68 public BeanMap(Object bean) { 69 myBean = bean; 70 try { 71 BeanInfo info = Introspector.getBeanInfo( myBean.getClass() ); 72 PropertyDescriptor[] pds = info.getPropertyDescriptors(); 73 for(int i=0; i<pds.length; i++) { 74 pdCache.put( pds[i].getName(), pds ); 75 } 76 } catch(IntrospectionException ie) { 77 } 78 } 79 80 /*** 81 * Get the PropertyDescriptor for a particular property name. 82 */ 83 public PropertyDescriptor getPropertyDescriptor(Object key) { 84 return (PropertyDescriptor)pdCache.get( key.toString() ); 85 } 86 87 /* map interface */ 88 89 // Removes all mappings from this map (optional operation) {. 90 public void clear() { 91 } 92 93 // Returns true if this map contains a mapping for the specified key. 94 public boolean containsKey(Object key) { 95 return pdCache.containsKey(key); 96 } 97 98 // Returns true if this map maps one or more keys to the specified value. 99 // TODO: Implement this 100 public boolean containsValue(Object value) { 101 return false; 102 } 103 104 // Returns a set view of the mappings contained in this map. 105 // TODO: Implement this 106 public Set entrySet() { 107 return null; 108 } 109 110 // Compares the specified object with this map for equality. 111 public boolean equals(Object o) { 112 return myBean.equals(o); 113 } 114 115 // Returns the value to which this map maps the specified key. 116 /* 117 public Object get(Object key) { 118 PropertyDescriptor desc = getPropertyDescriptor(key); 119 Method get = desc.getReadMethod(); 120 if(get == null) { 121 return null; 122 } 123 Object[] objs = new Object[0]; 124 try { 125 return get.invoke( myBean, objs ); 126 127 } catch(IllegalAccessException iae) { 128 return null; 129 } catch(IllegalArgumentException iae) { 130 return null; 131 } catch(InvocationTargetException ite) { 132 return null; 133 } 134 } 135 */ 136 137 // Returns the hash code value for this map. 138 public int hashCode() { 139 return myBean.hashCode(); 140 } 141 142 // Returns true if this map contains no key-value mappings. 143 // QUERY: Is a map empty if it contains blah:null pairings? 144 public boolean isEmpty() { 145 return pdCache.isEmpty(); 146 } 147 148 // Returns a set view of the keys contained in this map. 149 public Set keySet() { 150 return pdCache.keySet(); 151 } 152 153 // Associates the specified value with the specified key in this map (optional operation) {. 154 // TODO: Implement this 155 /*** 156 * Unimplemented. 157 */ 158 public Object put(Object key, Object value) { 159 Object oldValue = get(key); 160 PropertyDescriptor desc = getPropertyDescriptor(key); 161 Method set = desc.getWriteMethod(); 162 if(set == null) { 163 return oldValue; // ?? 164 } 165 Object[] objs = new Object[1]; 166 objs[0] = value; 167 try { 168 return set.invoke( myBean, objs ); 169 } catch(IllegalAccessException iae) { 170 return oldValue; 171 } catch(IllegalArgumentException iae) { 172 return oldValue; 173 } catch(InvocationTargetException ite) { 174 return oldValue; 175 } 176 } 177 178 // Copies all of the mappings from the specified map to this map (optional operation) {. 179 public void putAll(Map t) { 180 Iterator keys = t.keySet().iterator(); 181 while(keys.hasNext()) { 182 Object key = keys.next(); 183 put(key, t.get(key)); 184 } 185 } 186 187 // Removes the mapping for this key from this map if present (optional operation) {. 188 // TODO: Implement this 189 /*** 190 * Unimplemented. 191 */ 192 public Object remove(Object key) { 193 return null; 194 } 195 196 // Returns the number of key-value mappings in this map. 197 public int size() { 198 return pdCache.size(); 199 } 200 201 // Returns a collection view of the values contained in this map. 202 public Collection values() { 203 Set set = keySet(); 204 LinkedList values = new LinkedList(); 205 Iterator iterator = set.iterator(); 206 while(iterator.hasNext()) { 207 values.add( get( iterator.next() ) ); 208 } 209 return values; 210 } 211 212 public Object get(Object key) { 213 return get(key, this.myBean); 214 /* if( key == null ) { 215 return null; 216 } 217 218 if(myBean == null) { 219 return null; 220 } 221 222 String name = key.toString(); 223 224 int idx = 0; 225 Object obj = myBean; 226 while( (idx = name.indexOf('.', idx)) != -1) { 227 String pre = name.substring(0, idx); 228 name = name.substring(idx+1); 229 if(obj != null) { 230 obj = handleGet(obj, pre); 231 } else { 232 return null; 233 } 234 } 235 236 return handleGet(obj, name); 237 */ 238 } 239 /* 240 public Object handleGet(Object bean, String name) { 241 242 // deal with array 243 Object index = null; 244 int i = name.indexOf('['); 245 String pre = null; 246 // boolean arrayLike = false; 247 if(i != -1) { 248 pre = name.substring(i+1); 249 if(pre.endsWith("]")) { 250 pre = pre.substring(0, pre.length() -1); 251 } 252 try { 253 index = Integer.valueOf(pre); 254 // arrayLike = true; 255 } catch(NumberFormatException nfe) { 256 index = pre; 257 } 258 name = name.substring(0,i); 259 } 260 261 // capitalise 262 name = name.substring(0,1).toUpperCase(); 263 if(name.length() > 1) { 264 name += name.substring(1); 265 } 266 267 /* 268 methodName = "get"+methodName; 269 270 Method getMethod = null; 271 Class[] clss0 = new Class[0]; 272 Class[] clss = null; 273 try { 274 if(idx != -1) { 275 clss = new Class[1]; 276 clss[0] = Integer.TYPE; 277 } else { 278 clss = clss0; 279 } 280 getMethod = bean.getClass().getMethod(methodName, clss); 281 } catch(SecurityException se) { 282 } catch(NoSuchMethodException nsme) { 283 try { 284 if(clss != clss0) { 285 getMethod = bean.getClass().getMethod(methodName, clss0); 286 } 287 arrayLike = false; 288 } catch(SecurityException se2) { 289 } catch(NoSuchMethodException nsme2) { 290 } 291 } 292 293 if(getMethod == null) { 294 return null; 295 } 296 try { 297 Object[] param = null; 298 if(arrayLike) { 299 param = new Object[1]; 300 param[0] = new Integer(idx); 301 } else { 302 param = new Object[0]; 303 } 304 Object obj = getMethod.invoke( bean, param ); 305 * / 306 Object obj = invokeGet( bean, name, index ); 307 obj = CollectionUtils.index(obj, index); 308 return obj; 309 /* 310 if( (obj instanceof Map) && (pre != null) ) { 311 return ((Map)obj).get(pre); 312 } else 313 if( (obj instanceof List) && (idx != -1) ) { 314 return ((List)obj).get(idx); 315 } else 316 if( (obj instanceof Object[]) && (idx != -1) ) { 317 return ((Object[])obj)[idx]; 318 } else { 319 return obj; 320 } 321 */ 322 /* 323 } catch(IllegalAccessException iae) { 324 return null; 325 } catch(IllegalArgumentException iae) { 326 return null; 327 } catch(InvocationTargetException ite) { 328 return null; 329 } 330 * / 331 } 332 */ 333 /*** 334 * Returns bean.get<methodName>[idx] 335 */ 336 public Object invokeGet(BeanViewRuntime runtime, Object bean, String methodName, Object index) { 337 int idx = -1; 338 boolean arrayLike = (index instanceof Integer); 339 if(arrayLike) { 340 idx = ((Integer)index).intValue(); 341 } 342 methodName = "get"+methodName; 343 344 Method getMethod = null; 345 Class[] clss0 = new Class[0]; 346 Class[] clss = null; 347 try { 348 if(arrayLike) { 349 clss = new Class[1]; 350 clss[0] = Integer.TYPE; 351 } else { 352 clss = clss0; 353 } 354 getMethod = bean.getClass().getMethod(methodName, clss); 355 } catch(SecurityException se) { 356 } catch(NoSuchMethodException nsme) { 357 try { 358 if(clss != clss0) { 359 getMethod = bean.getClass().getMethod(methodName, clss0); 360 } 361 arrayLike = false; 362 } catch(SecurityException se2) { 363 } catch(NoSuchMethodException nsme2) { 364 } 365 } 366 367 if(getMethod == null) { 368 return null; 369 } 370 try { 371 Object[] param = null; 372 if(arrayLike) { 373 param = new Object[1]; 374 param[0] = new Integer(idx); 375 } else { 376 param = new Object[0]; 377 } 378 Object obj = getMethod.invoke( bean, param ); 379 obj = CollectionUtils.index(obj, idx); 380 381 return obj; 382 // if( (obj instanceof Map) && (pre != null) ) { 383 // return ((Map)obj).get(pre); 384 // } else 385 // if( (obj instanceof List) && (idx != -1) ) { 386 // return ((List)obj).get(idx); 387 // } else 388 // if( (obj instanceof Object[]) && (idx != -1) ) { 389 // return ((Object[])obj)[idx]; 390 // } else { 391 // return obj; 392 // } 393 } catch(IllegalAccessException iae) { 394 return null; 395 } catch(IllegalArgumentException iae) { 396 return null; 397 } catch(InvocationTargetException ite) { 398 return null; 399 } 400 } 401 402 public void invokeSet(BeanViewRuntime runtime, Object bean, String name, Object idx, Object value) { 403 } 404 }

This page was automatically generated by Maven