Joe's Java and Web Jottings

your host


Your Host

Calendar

««Nov 2009»»
SMTWTFS
1234567
891011121314
15161718192021
22232425262728
2930

My Top Tags

               

Java Libs and Tools

My Other Pages

My RSS Feeds








Comments on java.beans.EventHandler

posted Tuesday, 23 December 2003
xam commented on how the java.beans.EventHandler, while seemingly useful (uses reflection to build an instance of an event listener to call a specific method, avoiding having to implement one yourself, either directly or via inner classes), has problems in that it only works on public methods. Follow-up comments addressed how it catches and dumps exceptions to the screen, a bad user-interface anti-pattern.

There is no solution for the public v. private issue. Its a side effect of the security restrictions placed on reflection. Reflection can not invoke a method unless the class that's doing the invoking is doing it within the appropriate scope, so even package-protected things have to be invoked in the same package.

Trouble is the invoker in this case belongs to java.beans, not to the package of your GUI code. There's simply no way around that unless the JDK opens up more aspects of the .classfile, and that opens up a larger can of worms that I'd really rather not deal with.

Besides, IMHO, if something can be called via the GUI, its "public", and thus the method that invokes it on my Object should also be public. Its part of what something from the outside can do to my object. By having it public, it also gives me the room to test it with external testing tools.

As one noted, one could instead advertise what actions are available by making public an array of Action objects. It still allows anybody to activate an action, but at least you've made it clear what they're meant to call.

With the stack-trace issue, that was just plain bad, although my version had the same issue. The trouble in that case is the general problem of dispatching Exceptions in any GUI program. If you can't catch them, they propagate up to the AWT event queue, where they'll be printStackTraced anyways, and in the case of InvocationTargetException (written before JDK 1.4 built-in nested exceptions) the real source of the problem may end up not displayed in the dump.

Really, in any framework for a GUI, one should have the means to add an ExceptionHandler type of Object to deal with that sort of thing, but the JDK didn't build it in.




1. a reader left...
Tuesday, 23 December 2003 11:17 pm

An undocumented, but often used 'feature' is the sun.awt.exception.handler property (see http://forum.java.sun.com/thread.jsp?forum=52&thread=92316), which allows you to handle uncaught exceptions yourself. Whilst there are plenty of things wrong with this, its still used in 1.4 apps (afaik there is no replacement), and this could have been used by EventHandler.

cheers
dim

Dmitri Colebatch


2. a reader left...
Wednesday, 24 December 2003 7:54 am

i've commented on this on my own blog ;)

xam