1 7 package java.beans; 8 9 import java.lang.reflect.InvocationHandler ; 10 import java.lang.reflect.InvocationTargetException ; 11 import java.lang.reflect.Proxy ; 12 import java.lang.reflect.Method ; 13 import java.security.AccessControlContext ; 14 import java.security.AccessController ; 15 import java.security.PrivilegedAction ; 16 17 import java.util.EventObject ; 18 import sun.reflect.misc.MethodUtil; 19 20 205 public class EventHandler implements InvocationHandler { 206 private static Object [] empty = new Object []{}; 207 208 private Object target; 209 private Method targetMethod; 210 private String action; 211 private String eventPropertyName; 212 private String listenerMethodName; 213 private AccessControlContext acc; 214 215 232 public EventHandler(Object target, String action, String eventPropertyName, String listenerMethodName) { 233 this.acc = AccessController.getContext(); 234 this.target = target; 235 this.action = action; 236 this.eventPropertyName = eventPropertyName; 237 this.listenerMethodName = listenerMethodName; 238 } 239 240 246 public Object getTarget() { 247 return target; 248 } 249 250 259 public String getAction() { 260 return action; 261 } 262 263 271 public String getEventPropertyName() { 272 return eventPropertyName; 273 } 274 275 284 public String getListenerMethodName() { 285 return listenerMethodName; 286 } 287 288 private Object applyGetters(Object target, String getters) { 289 if (getters == null || getters.equals("")) { 290 return target; 291 } 292 int firstDot = getters.indexOf('.'); 293 if (firstDot == -1) { 294 firstDot = getters.length(); 295 } 296 String first = getters.substring(0, firstDot); 297 String rest = getters.substring(Math.min(firstDot + 1, getters.length())); 298 299 try { 300 Method getter = ReflectionUtils.getMethod(target.getClass(), 301 "get" + NameGenerator.capitalize(first), 302 new Class []{}); 303 if (getter == null) { 304 getter = ReflectionUtils.getMethod(target.getClass(), 305 "is" + NameGenerator.capitalize(first), 306 new Class []{}); 307 } 308 if (getter == null) { 309 getter = ReflectionUtils.getMethod(target.getClass(), first, new Class []{}); 310 } 311 if (getter == null) { 312 throw new RuntimeException ("No method called: " + first + 313 " defined on " + target); 314 } 315 Object newTarget = MethodUtil.invoke(getter, target, new Object []{}); 316 return applyGetters(newTarget, rest); 317 } 318 catch (Throwable e) { 319 throw new RuntimeException ("Failed to call method: " + first + 320 " on " + target, e); 321 } 322 } 323 324 335 public Object invoke(final Object proxy, final Method method, final Object [] arguments) { 336 return AccessController.doPrivileged(new PrivilegedAction () { 337 public Object run() { 338 return invokeInternal(proxy, method, arguments); 339 } 340 }, acc); 341 } 342 343 private Object invokeInternal(Object proxy, Method method, Object [] arguments) { 344 String methodName = method.getName(); 345 if (method.getDeclaringClass() == Object .class) { 346 if (methodName.equals("hashCode")) { 348 return new Integer (System.identityHashCode(proxy)); 349 } else if (methodName.equals("equals")) { 350 return (proxy == arguments[0] ? Boolean.TRUE : Boolean.FALSE); 351 } else if (methodName.equals("toString")) { 352 return proxy.getClass().getName() + '@' + Integer.toHexString(proxy.hashCode()); 353 } 354 } 355 356 if (listenerMethodName == null || listenerMethodName.equals(methodName)) { 357 Class [] argTypes = null; 358 Object [] newArgs = null; 359 360 if (eventPropertyName == null) { newArgs = new Object []{}; 362 argTypes = new Class []{}; 363 } 364 else { 365 Object input = applyGetters(arguments[0], getEventPropertyName()); 366 newArgs = new Object []{input}; 367 argTypes = new Class []{input.getClass()}; 368 } 369 try { 370 if (targetMethod == null) { 371 targetMethod = ReflectionUtils.getMethod(target.getClass(), 372 action, argTypes); 373 } 374 if (targetMethod == null) { 375 targetMethod = ReflectionUtils.getMethod(target.getClass(), 376 "set" + NameGenerator.capitalize(action), argTypes); 377 } 378 if (targetMethod == null) { 379 throw new RuntimeException ("No method called: " + 380 action + " on class " + 381 target.getClass() + " with argument " 382 + argTypes[0]); 383 } 384 return MethodUtil.invoke(targetMethod, target, newArgs); 385 } 386 catch (IllegalAccessException ex) { 387 throw new RuntimeException (ex); 388 } 389 catch (InvocationTargetException ex) { 390 throw new RuntimeException (ex.getTargetException()); 391 } 392 } 393 return null; 394 } 395 396 424 public static <T> T create(Class <T> listenerInterface, 425 Object target, String action) 426 { 427 return create(listenerInterface, target, action, null, null); 428 } 429 430 467 public static <T> T create(Class <T> listenerInterface, 468 Object target, String action, 469 String eventPropertyName) 470 { 471 return create(listenerInterface, target, action, eventPropertyName, null); 472 } 473 474 526 public static <T> T create(Class <T> listenerInterface, 527 Object target, String action, 528 String eventPropertyName, 529 String listenerMethodName) 530 { 531 return (T)Proxy.newProxyInstance(target.getClass().getClassLoader(), 532 new Class [] {listenerInterface}, 533 new EventHandler (target, action, 534 eventPropertyName, 535 listenerMethodName)); 536 } 537 } 538 539 540 541 542 543 | Popular Tags |