1 55 package org.jboss.axis.utils; 56 57 import org.jboss.logging.Logger; 58 59 import java.beans.IndexedPropertyDescriptor ; 60 import java.beans.PropertyDescriptor ; 61 import java.lang.reflect.Array ; 62 import java.lang.reflect.InvocationTargetException ; 63 import java.lang.reflect.Method ; 64 65 66 82 public class BeanPropertyDescriptor 83 { 84 private static Logger log = Logger.getLogger(BeanPropertyDescriptor.class.getName()); 85 86 protected PropertyDescriptor myPD = null; 87 88 protected static final Object [] noArgs = new Object []{}; 89 90 95 public BeanPropertyDescriptor(PropertyDescriptor pd) 96 { 97 myPD = pd; 98 } 99 100 103 protected BeanPropertyDescriptor() 104 { 105 } 106 107 110 public String getName() 111 { 112 return myPD.getName(); 113 } 114 115 120 public boolean isReadable() 121 { 122 return (myPD.getReadMethod() != null); 123 } 124 125 130 public boolean isWriteable() 131 { 132 return (myPD.getWriteMethod() != null); 133 } 134 135 140 public boolean isIndexed() 141 { 142 return (myPD instanceof IndexedPropertyDescriptor ); 143 } 144 145 151 public Object get(Object obj) 152 throws InvocationTargetException , IllegalAccessException 153 { 154 155 if (obj == null) 156 throw new IllegalArgumentException ("Target object cannot be null"); 157 158 Method readMethod = myPD.getReadMethod(); 159 log.debug("get: [method=" + readMethod + "]"); 160 if (readMethod != null) 161 { 162 Object retObj = readMethod.invoke(obj, noArgs); 163 log.debug("got: " + retObj); 164 return retObj; 165 } 166 throw new IllegalAccessException (Messages.getMessage("badGetter00")); 167 } 168 169 175 public void set(Object obj, Object newValue) 176 throws InvocationTargetException , IllegalAccessException 177 { 178 179 if (obj == null) 180 throw new IllegalArgumentException ("Target object cannot be null"); 181 182 Method writeMethod = myPD.getWriteMethod(); 183 log.debug("set: [val=" + newValue + ",method=" + writeMethod + "]"); 184 if (writeMethod != null) 185 { 186 writeMethod.invoke(obj, new Object []{newValue}); 187 } 188 else 189 { 190 throw new IllegalAccessException (Messages.getMessage("badSetter00")); 191 } 192 } 193 194 201 public Object get(Object obj, int i) 202 throws InvocationTargetException , IllegalAccessException 203 { 204 205 if (obj == null) 206 throw new IllegalArgumentException ("Target object cannot be null"); 207 208 if (!isIndexed()) 209 { 210 return Array.get(get(obj), i); 211 } 212 else 213 { 214 IndexedPropertyDescriptor id = (IndexedPropertyDescriptor )myPD; 215 Method readMethod = id.getIndexedReadMethod(); 216 log.debug("get: [method=" + readMethod + ",index=" + i + "]"); 217 Object retObj = readMethod.invoke(obj, new Object []{new Integer (i)}); 218 log.debug("got: " + retObj); 219 return retObj; 220 } 221 } 222 223 230 public void set(Object obj, int i, Object newValue) 231 throws InvocationTargetException , IllegalAccessException 232 { 233 234 if (obj == null) 235 throw new IllegalArgumentException ("Target object cannot be null"); 236 237 if (isIndexed()) 239 { 240 IndexedPropertyDescriptor id = (IndexedPropertyDescriptor )myPD; 241 growArrayToSize(obj, id.getIndexedPropertyType(), i); 242 id.getIndexedWriteMethod().invoke(obj, 243 new Object []{ 244 new Integer (i), newValue}); 245 } 246 else 247 { 248 Object array = get(obj); 249 if (array == null || Array.getLength(array) <= i) 250 { 251 Class componentType = getType().getComponentType(); 252 growArrayToSize(obj, componentType, i); 253 array = get(obj); 254 } 255 Array.set(array, i, newValue); 256 } 257 } 258 259 268 protected void growArrayToSize(Object obj, Class componentType, int i) throws InvocationTargetException , IllegalAccessException 269 { 270 271 if (obj == null) 272 throw new IllegalArgumentException ("Target object cannot be null"); 273 274 Object array = get(obj); 276 if (array == null || Array.getLength(array) <= i) 277 { 278 Object newArray = Array.newInstance(componentType, i + 1); 280 if (array != null) 282 { 283 System.arraycopy(array, 0, newArray, 0, Array.getLength(array)); 284 } 285 286 set(obj, newArray); 288 } 289 } 290 291 296 public Class getType() 297 { 298 if (isIndexed()) 299 { 300 return ((IndexedPropertyDescriptor )myPD).getIndexedPropertyType(); 301 } 302 else 303 { 304 return myPD.getPropertyType(); 305 } 306 } 307 } 308 | Popular Tags |