1 55 56 package org.apache.bsf.util; 57 58 import java.util.*; 59 import java.io.*; 60 import java.beans.*; 61 import java.lang.reflect.*; 62 63 import org.apache.bsf.util.event.*; 64 import org.apache.bsf.util.type.*; 65 66 74 public class ReflectionUtils { 75 76 78 96 public static void addEventListener (Object source, String eventSetName, 97 EventProcessor processor) 98 throws IntrospectionException, IllegalArgumentException , 99 IllegalAccessException , InstantiationException , 100 InvocationTargetException { 101 BeanInfo bi = Introspector.getBeanInfo (source.getClass ()); 103 EventSetDescriptor esd = (EventSetDescriptor) 104 findFeatureByName ("event", eventSetName, bi.getEventSetDescriptors ()); 105 if (esd == null) { 106 throw new IllegalArgumentException ("event set '" + eventSetName + 107 "' unknown for source type '" + 108 source.getClass () + "'"); 109 } 110 111 Class listenerType = esd.getListenerType (); 113 114 Class adapterClass = EventAdapterRegistry.lookup (listenerType); 116 if (adapterClass == null) { 117 throw new IllegalArgumentException ("event adapter for listner type " + 118 "'" + listenerType + "' (eventset " + 119 "'" + eventSetName + "') unknown"); 120 } 121 122 EventAdapter adapter = (EventAdapter) adapterClass.newInstance (); 124 adapter.setEventProcessor (processor); 125 126 Method addListenerMethod; 128 Object [] args; 129 if (eventSetName.equals ("propertyChange") || 130 eventSetName.equals ("vetoableChange")) { 131 addListenerMethod = esd.getAddListenerMethod (); 139 args = new Object [] {adapter}; 140 } else { 141 addListenerMethod = esd.getAddListenerMethod (); 142 args = new Object [] {adapter}; 143 } 144 addListenerMethod.invoke (source, args); 145 } 146 148 167 public static Bean createBean (ClassLoader cld, String className, 168 Class [] argTypes, Object [] args) 169 throws ClassNotFoundException , NoSuchMethodException , 170 InstantiationException , IllegalAccessException , 171 IllegalArgumentException , InvocationTargetException, 172 IOException { 173 if (argTypes != null) { 174 Class cl = (cld != null) ? cld.loadClass (className) 176 : Class.forName (className); 177 Constructor c = MethodUtils.getConstructor (cl, argTypes); 178 return new Bean (cl, c.newInstance (args)); 179 } else { 180 Object obj = Beans.instantiate (cld, className); 182 return new Bean (obj.getClass (), obj); 183 } 184 } 185 187 206 public static Bean createBean (ClassLoader cld, String className, 207 Object [] args) 208 throws ClassNotFoundException , NoSuchMethodException , 209 InstantiationException , IllegalAccessException , 210 IllegalArgumentException , InvocationTargetException, 211 IOException { 212 Class [] argTypes = null; 213 if (args != null) { 214 argTypes = new Class [args.length]; 215 for (int i = 0; i < args.length; i++) { 216 argTypes[i] = (args[i] != null) ? args[i].getClass () : null; 217 } 218 } 219 return createBean (cld, className, argTypes, args); 220 } 221 223 227 private static 228 FeatureDescriptor findFeatureByName (String featureType, String name, 229 FeatureDescriptor[] fds) { 230 for (int i = 0; i < fds.length; i++) { 231 if (name.equals (fds[i].getName())) { 232 return fds[i]; 233 } 234 } 235 return null; 236 } 237 public static Bean getField (Object target, String fieldName) 238 throws IllegalArgumentException , IllegalAccessException { 239 Class targetClass = (target instanceof Class ) 241 ? (Class ) target 242 : target.getClass (); 243 244 try { 245 Field f = targetClass.getField (fieldName); 246 Class fieldType = f.getType (); 247 248 Object value = f.get (target); 250 return new Bean (fieldType, value); 251 } catch (NoSuchFieldException e) { 252 throw new IllegalArgumentException ("field '" + fieldName + "' is " + 253 "unknown for '" + target + "'"); 254 } 255 } 256 258 273 public static Bean getProperty (Object target, String propName, 274 Integer index) 275 throws IntrospectionException, IllegalArgumentException , 276 IllegalAccessException , InvocationTargetException { 277 BeanInfo bi = Introspector.getBeanInfo (target.getClass ()); 279 PropertyDescriptor pd = (PropertyDescriptor) 280 findFeatureByName ("property", propName, bi.getPropertyDescriptors ()); 281 if (pd == null) { 282 throw new IllegalArgumentException ("property '" + propName + "' is " + 283 "unknown for '" + target + "'"); 284 } 285 286 Method rm; 288 Class propType; 289 if (index != null) { 290 if (!(pd instanceof IndexedPropertyDescriptor)) { 292 throw new IllegalArgumentException ("attempt to get non-indexed " + 293 "property '" + propName + 294 "' as being indexed"); 295 } 296 IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd; 297 rm = ipd.getIndexedReadMethod (); 298 propType = ipd.getIndexedPropertyType (); 299 } else { 300 rm = pd.getReadMethod (); 301 propType = pd.getPropertyType (); 302 } 303 304 if (rm == null) { 305 throw new IllegalArgumentException ("property '" + propName + 306 "' is not readable"); 307 } 308 309 Object propVal = null; 311 if (index != null) { 312 propVal = rm.invoke (target, new Object [] {index}); 313 } else { 314 propVal = rm.invoke (target, null); 315 } 316 return new Bean (propType, propVal); 317 } 318 public static void setField (Object target, String fieldName, Bean value, 319 TypeConvertorRegistry tcr) 320 throws IllegalArgumentException , IllegalAccessException { 321 Class targetClass = (target instanceof Class ) 323 ? (Class ) target 324 : target.getClass (); 325 326 try { 327 Field f = targetClass.getField (fieldName); 328 Class fieldType = f.getType (); 329 330 Object fieldVal = null; 332 boolean okeydokey = true; 333 if (fieldType.isAssignableFrom (value.type)) { 334 fieldVal = value.value; 335 } else if (tcr != null) { 336 TypeConvertor cvtor = tcr.lookup (value.type, fieldType); 337 if (cvtor != null) { 338 fieldVal = cvtor.convert (value.type, fieldType, value.value); 339 } else { 340 okeydokey = false; 341 } 342 } else { 343 okeydokey = false; 344 } 345 if (!okeydokey) { 346 throw new IllegalArgumentException ("unable to assign '" + value.value + 347 "' to field '" + fieldName + "'"); 348 } 349 350 f.set (target, fieldVal); 352 } catch (NoSuchFieldException e) { 353 throw new IllegalArgumentException ("field '" + fieldName + "' is " + 354 "unknown for '" + target + "'"); 355 } 356 } 357 359 378 public static void setProperty (Object target, String propName, 379 Integer index, Object value, 380 Class valueType, TypeConvertorRegistry tcr) 381 throws IntrospectionException, IllegalArgumentException , 382 IllegalAccessException , InvocationTargetException { 383 BeanInfo bi = Introspector.getBeanInfo (target.getClass ()); 385 PropertyDescriptor pd = (PropertyDescriptor) 386 findFeatureByName ("property", propName, bi.getPropertyDescriptors ()); 387 if (pd == null) { 388 throw new IllegalArgumentException ("property '" + propName + "' is " + 389 "unknown for '" + target + "'"); 390 } 391 392 Method wm; 394 Class propType; 395 Object [] args; 396 if (index != null) { 397 if (!(pd instanceof IndexedPropertyDescriptor)) { 399 throw new IllegalArgumentException ("attempt to set non-indexed " + 400 "property '" + propName + 401 "' as being indexed"); 402 } 403 IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd; 404 wm = ipd.getIndexedWriteMethod (); 405 propType = ipd.getIndexedPropertyType (); 406 } else { 407 wm = pd.getWriteMethod (); 408 propType = pd.getPropertyType (); 409 } 410 411 if (wm == null) { 412 throw new IllegalArgumentException ("property '" + propName + 413 "' is not writeable"); 414 } 415 416 Object propVal = null; 418 boolean okeydokey = true; 419 if (propType.isAssignableFrom (valueType)) { 420 propVal = value; 421 } else if (tcr != null) { 422 TypeConvertor cvtor = tcr.lookup (valueType, propType); 423 if (cvtor != null) { 424 propVal = cvtor.convert (valueType, propType, value); 425 } else { 426 okeydokey = false; 427 } 428 } else { 429 okeydokey = false; 430 } 431 if (!okeydokey) { 432 throw new IllegalArgumentException ("unable to assign '" + value + 433 "' to property '" + propName + "'"); 434 } 435 436 if (index != null) { 438 wm.invoke (target, new Object [] {index, propVal}); 439 } else { 440 wm.invoke (target, new Object [] {propVal}); 441 } 442 } 443 } 444 | Popular Tags |