1 16 package org.apache.axis.utils; 17 18 import org.apache.axis.components.logger.LogFactory; 19 import org.apache.commons.logging.Log; 20 21 import java.beans.IndexedPropertyDescriptor ; 22 import java.beans.PropertyDescriptor ; 23 import java.lang.reflect.Array ; 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.InvocationTargetException ; 26 27 28 44 public class BeanPropertyDescriptor 45 { 46 protected static Log log = 47 LogFactory.getLog(BeanPropertyDescriptor.class.getName()); 48 49 protected PropertyDescriptor myPD = null; 50 51 protected static final Object [] noArgs = new Object [] {}; 52 53 58 public BeanPropertyDescriptor(PropertyDescriptor pd) { 59 myPD = pd; 60 } 61 62 65 protected BeanPropertyDescriptor() { 66 } 67 68 71 public String getName(){ 72 return myPD.getName(); 73 } 74 75 79 public boolean isReadable() { 80 return (myPD.getReadMethod() != null); 81 } 82 83 87 public boolean isWriteable() { 88 return (myPD.getWriteMethod() != null); 89 } 90 91 96 public boolean isIndexed() { 97 return (myPD instanceof IndexedPropertyDescriptor ); 98 } 99 100 105 public boolean isIndexedOrArray() { 106 return (isIndexed() || isArray()); 107 } 108 109 113 public boolean isArray() { 114 return ((myPD.getPropertyType() != null) && myPD.getPropertyType() 115 .isArray()); 116 } 117 118 123 public Object get(Object obj) 124 throws InvocationTargetException , IllegalAccessException { 125 Method readMethod = myPD.getReadMethod(); 126 if (readMethod != null) { 127 return readMethod.invoke(obj, noArgs); 128 } else { 129 throw new IllegalAccessException (Messages.getMessage("badGetter00")); 130 } 131 } 132 133 138 public void set(Object obj, Object newValue) 139 throws InvocationTargetException , IllegalAccessException { 140 Method writeMethod = myPD.getWriteMethod(); 141 if (writeMethod != null) { 142 writeMethod.invoke(obj, new Object [] {newValue}); 143 } else { 144 throw new IllegalAccessException (Messages.getMessage("badSetter00")); 145 } 146 } 147 148 154 public Object get(Object obj, int i) 155 throws InvocationTargetException , IllegalAccessException { 156 if (!isIndexed()) { 157 return Array.get(get(obj), i); 158 } else { 159 IndexedPropertyDescriptor id = (IndexedPropertyDescriptor )myPD; 160 return id.getIndexedReadMethod().invoke(obj, 161 new Object [] { 162 new Integer (i)}); 163 } 164 } 165 166 172 public void set(Object obj, int i, Object newValue) 173 throws InvocationTargetException , IllegalAccessException { 174 if (isIndexed()) { 176 IndexedPropertyDescriptor id = (IndexedPropertyDescriptor )myPD; 177 growArrayToSize(obj, id.getIndexedPropertyType(), i); 178 id.getIndexedWriteMethod().invoke(obj, 179 new Object [] { 180 new Integer (i), newValue}); 181 } else { 182 growArrayToSize(obj, myPD.getPropertyType().getComponentType(), i); 183 Array.set(get(obj), i, newValue); 184 } 185 } 186 187 195 protected void growArrayToSize(Object obj, Class componentType, int i) throws InvocationTargetException , IllegalAccessException { 196 Object array = get(obj); 198 if (array == null || Array.getLength(array) <= i) { 199 Object newArray = Array.newInstance(componentType, i + 1); 201 if (array != null) { 203 System.arraycopy(array, 0, newArray, 0, Array.getLength(array)); 204 } 205 set(obj, newArray); 207 } 208 } 209 210 214 public Class getType() { 215 if (isIndexed()) { 216 return ((IndexedPropertyDescriptor )myPD).getIndexedPropertyType(); 217 } else { 218 return myPD.getPropertyType(); 219 } 220 } 221 222 public Class getActualType() { 223 return myPD.getPropertyType(); 224 } 225 } 226 | Popular Tags |