| 1 17 18 package org.sape.carbon.core.util.reflection; 19 20 import java.beans.BeanInfo ; 21 import java.beans.IntrospectionException ; 22 import java.beans.Introspector ; 23 import java.beans.PropertyDescriptor ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 import java.util.StringTokenizer ; 27 28 29 39 public final class BeanUtil { 40 44 private static final String PROPERTY_SEPARATOR = "."; 45 46 49 private static Class [] NO_PARAMETERS_ARRAY = new Class [] { 50 }; 51 54 private static Object [] NO_ARGUMENTS_ARRAY = new Object [] { 55 }; 56 57 60 private BeanUtil() { 61 } 62 63 79 private static final PropertyDescriptor getPropertyDescriptor( 80 String propertyName, 81 Class beanClass) { 82 83 PropertyDescriptor resultPropertyDescriptor = null; 84 85 86 char[] pNameArray = propertyName.toCharArray(); 87 pNameArray[0] = Character.toLowerCase(pNameArray[0]); 88 propertyName = new String (pNameArray); 89 90 try { 91 resultPropertyDescriptor = 92 new PropertyDescriptor (propertyName, beanClass); 93 } catch (IntrospectionException e1) { 94 98 try { 102 BeanInfo beanInfo = Introspector.getBeanInfo(beanClass); 103 104 PropertyDescriptor [] propertyDescriptors = 105 beanInfo.getPropertyDescriptors(); 106 107 for (int i = 0; i < propertyDescriptors.length; i++) { 108 if (propertyDescriptors[i] 109 .getName() 110 .equals(propertyName)) { 111 112 resultPropertyDescriptor = propertyDescriptors[i]; 116 break; 117 } 118 } 119 } catch (IntrospectionException e2) { 120 throw new PropertyNotFoundException( 121 BeanUtil.class, 122 "Encountered " 123 + "exception looking up property [" 124 + propertyName 125 + "] on class [" 126 + beanClass 127 + "]", 128 e2); 129 } 130 } 131 132 if (resultPropertyDescriptor == null) { 135 throw new PropertyNotFoundException( 136 BeanUtil.class, 137 "Failed to find " 138 + "property descriptor for property [" 139 + propertyName 140 + "] on class [" 141 + beanClass 142 + "]"); 143 } 144 145 return resultPropertyDescriptor; 146 } 147 148 175 public static Object getObjectAttribute(Object bean, String propertyNames) 176 throws 177 NoSuchMethodException , 178 InvocationTargetException , 179 IllegalAccessException { 180 181 182 Object result = bean; 183 184 StringTokenizer propertyTokenizer = 185 new StringTokenizer (propertyNames, PROPERTY_SEPARATOR); 186 187 while (propertyTokenizer.hasMoreElements() && result != null) { 191 Class resultClass = result.getClass(); 192 String currentPropertyName = propertyTokenizer.nextToken(); 193 194 PropertyDescriptor propertyDescriptor = 195 getPropertyDescriptor(currentPropertyName, resultClass); 196 197 Method readMethod = propertyDescriptor.getReadMethod(); 198 if (readMethod == null) { 199 throw new IllegalAccessException ( 200 "User is attempting to " 201 + "read from a property that has no read method. " 202 + " This is likely a write-only bean property. Caused " 203 + "by property [" 204 + currentPropertyName 205 + "] on class [" 206 + resultClass 207 + "]"); 208 } 209 210 result = readMethod.invoke(result, NO_ARGUMENTS_ARRAY); 211 } 212 213 return result; 214 } 215 216 240 public static void setObjectAttribute( 241 Object bean, 242 String propertyNames, 243 Object value) 244 throws 245 IllegalAccessException , 246 IllegalArgumentException , 247 InvocationTargetException , 248 NoSuchMethodException { 249 250 251 Object result = bean; 252 String propertyName = propertyNames; 253 254 int indexOfLastPropertySeparator = 257 propertyName.lastIndexOf(PROPERTY_SEPARATOR); 258 259 if (indexOfLastPropertySeparator >= 0) { 260 String embeddedProperties = 261 propertyName.substring(0, indexOfLastPropertySeparator); 262 263 propertyName = 265 propertyName.substring( 266 indexOfLastPropertySeparator + PROPERTY_SEPARATOR.length()); 267 268 result = getObjectAttribute(result, embeddedProperties); 269 } 270 271 Class resultClass = result.getClass(); 272 273 PropertyDescriptor propertyDescriptor = 274 getPropertyDescriptor(propertyName, resultClass); 275 276 Method writeMethod = propertyDescriptor.getWriteMethod(); 277 if (writeMethod == null) { 278 throw new IllegalAccessException ( 279 "User is attempting to write " 280 + "to a property that has no write method. This is likely " 281 + "a read-only bean property. Caused by property [" 282 + propertyName 283 + "] on class [" 284 + resultClass 285 + "]"); 286 } 287 288 writeMethod.invoke(result, new Object [] { value }); 289 } 290 } 291 | Popular Tags |