1 30 31 37 38 package com.nightlabs.util.bean; 39 40 import java.beans.BeanInfo ; 41 import java.beans.IntrospectionException ; 42 import java.beans.Introspector ; 43 import java.beans.PropertyDescriptor ; 44 import java.lang.reflect.Method ; 45 import java.util.HashMap ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 import java.util.Map ; 49 import java.util.Vector ; 50 51 import com.nightlabs.util.bean.propertyeditor.PropertyEditorUI; 52 import com.nightlabs.util.bean.propertyeditor.PropertyEditorUIException; 53 54 public class BeanUtil 55 { 56 public static final int USE_ALL_BEANINFO = Introspector.USE_ALL_BEANINFO; 57 public static final int IGNORE_ALL_BEANINFO = Introspector.IGNORE_ALL_BEANINFO; 58 public static final int IGNORE_IMMEDIATE_BEANINFO = Introspector.IGNORE_IMMEDIATE_BEANINFO; 59 60 public BeanUtil(int mode) 61 { 62 if (mode != USE_ALL_BEANINFO && 63 mode != IGNORE_ALL_BEANINFO && 64 mode != IGNORE_IMMEDIATE_BEANINFO) 65 throw new IllegalArgumentException ("Param mode is neither USE_ALL_BEANINFO nor IGNORE_ALL_BEANINFO nor IGNORE_IMMEDIATE_BEANINFO!"); 66 67 this.currentMode = mode; 68 } 69 70 80 protected int currentMode = USE_ALL_BEANINFO; 81 public int getCurrentMode() { 82 return currentMode; 83 } 84 public void setCurrentMode(int mode) { 85 currentMode = mode; 86 } 87 88 97 public static List getBeanMethods(Class beanClass, int flag) 98 throws IntrospectionException 99 { 100 Vector propertyMethods = new Vector (); 101 BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, flag); 102 PropertyDescriptor [] pds = beanInfo.getPropertyDescriptors(); 103 propertyMethods = new Vector (pds.length); 104 for (int i=0; i < pds.length; i++) 105 { 106 Method [] methods = new Method [2]; 107 PropertyDescriptor pd = pds[i]; 108 methods[0] = pd.getReadMethod(); 109 methods[1] = pd.getWriteMethod(); 110 propertyMethods.add(methods); 111 } 112 return propertyMethods; 113 } 114 115 124 public static List getBeanMethods(Class beanClass, Class stopClass) 125 throws IntrospectionException 126 { 127 Vector propertyMethods = new Vector (); 128 BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, stopClass); 129 PropertyDescriptor [] pds = beanInfo.getPropertyDescriptors(); 130 propertyMethods = new Vector (pds.length); 131 for (int i=0; i < pds.length; i++) 132 { 133 Method [] methods = new Method [2]; 134 PropertyDescriptor pd = pds[i]; 135 methods[0] = pd.getReadMethod(); 136 methods[1] = pd.getWriteMethod(); 137 propertyMethods.add(methods); 138 } 139 return propertyMethods; 140 } 141 142 143 153 public static PropertyDescriptor [] getPropertyDescriptors(Class beanClass, int flag) 154 throws IntrospectionException 155 { 156 BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, flag); 157 return beanInfo.getPropertyDescriptors(); 158 } 159 160 169 public static List getPropertyDescriptorsAsVector(Class beanClass, int flag) 170 throws IntrospectionException 171 { 172 BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, flag); 173 PropertyDescriptor [] pdsArray = beanInfo.getPropertyDescriptors(); 174 Vector pdsVector = new Vector (pdsArray.length); 175 for (int i=0; i < pdsArray.length; i++) 176 { 177 pdsVector.add(pdsArray[i]); 178 } 179 return pdsVector; 180 } 181 182 public Map getProperty2Bean2Pd(List beans) 183 throws IntrospectionException 184 { 185 List oldProperties = null; 186 List oldPropertyNames = new Vector (); 187 Map property2Bean2Pd = new HashMap (); 188 189 for (Iterator it = beans.iterator(); it.hasNext(); ) 190 { 191 Object bean = it.next(); 192 List pds = getPropertyDescriptorsAsVector(bean.getClass(), getCurrentMode()); 193 if (oldProperties == null) 197 { 198 oldProperties = pds; 199 for (Iterator it2 = oldProperties.iterator(); it2.hasNext(); ) 200 { 201 PropertyDescriptor pd = (PropertyDescriptor ) it2.next(); 202 String propertyName = pd.getName(); 203 oldPropertyNames.add(propertyName); 204 HashMap bean2pd = new HashMap (); 205 bean2pd.put(bean, pd); 206 property2Bean2Pd.put(propertyName, bean2pd); 207 } } 210 else { 214 for (Iterator it2 = pds.iterator(); it2.hasNext(); ) 215 { 216 PropertyDescriptor newPD = (PropertyDescriptor ) it2.next(); 217 String newProperyName = newPD.getName(); 218 if (oldPropertyNames.contains(newProperyName)) 221 { 222 HashMap bean2pd = (HashMap ) property2Bean2Pd.get(newProperyName); 223 bean2pd.put(bean, newPD); 224 } 225 else { 227 property2Bean2Pd.remove(newProperyName); 228 oldPropertyNames.remove(newProperyName); 229 } 230 } 231 } } return property2Bean2Pd; 234 } 235 236 242 public static Class getAllBeansSingleClass(List beans) 243 { 244 Class firstClass = null; 246 boolean sameClass = true; 247 for (Iterator it = beans.iterator(); it.hasNext(); ) 248 { 249 Object bean = it.next(); 250 Class beanClass = bean.getClass(); 251 if (firstClass == null) { 252 firstClass = beanClass; 253 } 254 if(!firstClass.equals(beanClass)) { 255 sameClass = false; 256 break; 257 } 258 } 259 return sameClass ? firstClass : null; 260 } 261 262 public List getSamePropertyDescriptors(List beans) 263 throws IntrospectionException 264 { 265 List oldPropertyDescriptors = null; 266 List classes = new Vector (); 267 268 for (Iterator it = beans.iterator(); it.hasNext(); ) 269 { 270 Object bean = it.next(); 271 List pds = getPropertyDescriptorsAsVector(bean.getClass(), currentMode); 272 273 if (oldPropertyDescriptors == null) 278 { 279 oldPropertyDescriptors = pds; 280 classes.add(bean.getClass()); 281 } 283 else { 287 if (classes.contains(bean.getClass())) 288 { 289 continue; 290 } 291 else { 293 classes.add(bean.getClass()); 294 Vector indexes = new Vector (); 295 for (int i=oldPropertyDescriptors.size()-1; i>=0; i--) 296 { 297 PropertyDescriptor oldPD = (PropertyDescriptor )oldPropertyDescriptors.get(i); 298 if (pds.contains(oldPD)) { 299 continue; 300 } 301 else { 302 indexes.add(new Integer (i)); 303 } 304 } if (!indexes.isEmpty()) 306 { 307 for (Iterator itIndex = indexes.iterator(); itIndex.hasNext(); ) { 308 int index = ((Integer ) itIndex.next()).intValue(); 309 oldPropertyDescriptors.remove(index); 310 } 311 } 312 313 } } 316 } return oldPropertyDescriptors; 318 } 320 328 public List getPropertyEditorUIsFromPDs(List _propertyDescriptors) 329 { 330 Vector editorUIs = new Vector (); 331 for (Iterator it = _propertyDescriptors.iterator(); it.hasNext(); ) 332 { 333 PropertyDescriptor pd = (PropertyDescriptor ) it.next(); 334 if (pd == null) { 335 continue; 336 } 337 PropertyEditorUI editorUI = new PropertyEditorUI(pd); 338 editorUIs.addElement(editorUI); 339 } 340 return editorUIs; 341 } 342 343 351 public List getPropertyEditorUIsFromBeans(List _beans) 352 throws IntrospectionException 353 { 354 Class classForAllBeans = getAllBeansSingleClass(_beans); 355 if (classForAllBeans != null) 356 { 357 List pds = getPropertyDescriptorsAsVector(classForAllBeans, getCurrentMode()); 358 List editorUIs = getPropertyEditorUIsFromPDs(pds); 359 return editorUIs; 360 } 361 else { 362 List pds = getSamePropertyDescriptors(_beans); 363 List editorUIs = getPropertyEditorUIsFromPDs(pds); 364 return editorUIs; 365 } 366 } 367 368 369 374 public static String getPropertyName(String methodName, boolean spaces) 375 { 376 String propertyName = ""; 377 StringBuffer sb = new StringBuffer (methodName); 378 if (sb.substring(0,3).equals("get") || 379 sb.substring(0,3).equals("set")) 380 { 381 propertyName = methodName.substring(3); 382 } 383 else if (sb.substring(0,2).equals("is")) { 384 propertyName = methodName.substring(2); 385 } 386 387 if (spaces) 388 { 389 char buff[] = propertyName.toCharArray(); 393 int n = buff != null ? buff.length : 0; 394 char data[] = new char[n * 2]; 395 int j = 1; 396 data[0] = buff[0]; 397 for (int i = 1; i < n; i++) { 398 char c = buff[i]; 399 if (Character.isUpperCase(c)) 400 data[j++] = ' '; 401 data[j++] = c; 402 } 403 404 return new String (data, 0, j); 405 } 406 return propertyName; 407 } 408 409 public Object getValue(Object bean, String propertyName) 410 { 411 try { 412 BeanInfo bi = Introspector.getBeanInfo(bean.getClass(), currentMode); 413 PropertyDescriptor [] pds = bi.getPropertyDescriptors(); 414 for (int i=0; i<pds.length; i++) { 415 PropertyDescriptor pd = pds[i]; 416 if (pd.getName().equals(propertyName)) 417 return pd.getValue(propertyName); 418 } 419 } catch (IntrospectionException e) { 420 e.printStackTrace(); 421 } 422 return null; 423 } 424 425 public void setValue(Object bean, String propertyName) 426 { 427 try { 428 BeanInfo bi = Introspector.getBeanInfo(bean.getClass(), currentMode); 429 PropertyDescriptor [] pds = bi.getPropertyDescriptors(); 430 for (int i=0; i<pds.length; i++) { 431 PropertyDescriptor pd = pds[i]; 432 if (pd.getName().equals(propertyName)) 433 pd.setValue(propertyName, bean); 434 } 435 } catch (IntrospectionException e) { 436 e.printStackTrace(); 437 } 438 } 439 440 441 444 public void loadUIValues(List _editorUIs, List _beans) 445 throws PropertyEditorUIException 446 { 447 for (Iterator it = _editorUIs.iterator(); it.hasNext(); ) 448 { 449 PropertyEditorUI editorUI = (PropertyEditorUI) it.next(); 450 editorUI.load(_beans); 451 } 452 } 453 454 457 public void loadUIValues(List _editorUIs, Object _bean) 458 throws PropertyEditorUIException 459 { 460 for (Iterator it = _editorUIs.iterator(); it.hasNext(); ) 461 { 462 PropertyEditorUI editorUI = (PropertyEditorUI) it.next(); 463 editorUI.load(_bean); 464 } 465 } 466 467 471 public void saveUIValues(List _editorUIs, List _beans) 472 throws PropertyEditorUIException 473 { 474 for (Iterator it = _editorUIs.iterator(); it.hasNext(); ) 475 { 476 PropertyEditorUI editorUI = (PropertyEditorUI) it.next(); 477 editorUI.save(_beans); 478 } 479 } 480 481 485 public void saveUIValues(List _editorUIs, Object _bean) 486 throws PropertyEditorUIException 487 { 488 for (Iterator it = _editorUIs.iterator(); it.hasNext(); ) 489 { 490 PropertyEditorUI editorUI = (PropertyEditorUI) it.next(); 491 editorUI.save(_bean); 492 } 493 } 494 495 502 public Map getPropertyDescriptors(String _propertyName, List _beans) 503 throws IntrospectionException 504 { 505 Map bean2Pd = new HashMap (); 506 for (Iterator itBean = _beans.iterator(); itBean.hasNext(); ) 507 { 508 Object bean = itBean.next(); 509 PropertyDescriptor [] pds = getPropertyDescriptors(bean.getClass(), currentMode); 510 for (int i=0; i < pds.length; i++) 511 { 512 PropertyDescriptor pd = (PropertyDescriptor ) pds[i]; 513 if (pd.getName().equals(_propertyName)) 514 { 515 bean2Pd.put(bean, pd); 516 } } } return bean2Pd; 520 } 521 522 529 public Map getPropertyEditorUI(String _propertyName, List _beans) 530 throws IntrospectionException 531 { 532 Map bean2EditorUI = new HashMap (); 533 for (Iterator itBean = _beans.iterator(); itBean.hasNext(); ) 534 { 535 Object bean = itBean.next(); 536 PropertyDescriptor [] pds = getPropertyDescriptors(bean.getClass(), currentMode); 537 for (int i=0; i < pds.length; i++) 538 { 539 PropertyDescriptor pd = (PropertyDescriptor ) pds[i]; 540 if (pd.getName().equals(_propertyName)) 541 { 542 PropertyEditorUI editorUI = new PropertyEditorUI(pd); 543 bean2EditorUI.put(bean, editorUI); 544 } } } return bean2EditorUI; 548 } 549 550 } 551 | Popular Tags |