1 14 package org.wings.util; 15 16 import java.lang.reflect.Method ; 17 18 22 public class PropertyAccessor { 23 26 public static boolean hasProperty(Object o, String name) { 27 try { 28 Method [] m = o.getClass().getMethods(); 29 String setterName = "set" + capitalize(name); 30 for (int i = 0; i < m.length; ++i) { 31 if (m[i].getParameterTypes().length == 1 32 && m[i].getName().equals(setterName)) { 33 return true; 35 } 36 } 37 } catch (Exception e) {} 38 return false; 39 } 40 41 42 public static void setProperty(Object o, String name, Object value) { 43 try { 44 Method [] m = o.getClass().getMethods(); 45 String setterName = "set" + capitalize(name); 46 for (int i = 0; i < m.length; ++i) { 47 if (m[i].getParameterTypes().length == 1 48 && m[i].getName().equals(setterName) 49 && (value == null 50 || (m[i].getParameterTypes()[0] 51 .isAssignableFrom(value.getClass())))) { 52 m[i].invoke(o, new Object []{value}); 53 } 54 } 55 } catch (Exception e) {} 56 } 57 58 59 public static void getProperty(Object o, String name, Object value) { 60 } 62 63 67 private static String capitalize(String s) { 68 s = s.trim(); 69 return s.substring(0, 1).toUpperCase() + s.substring(1); 70 } 71 } 72 73 74 | Popular Tags |