Originally posted by Ove Lindstr�m:
Also, if I would find out that someone was doing that, I would crank the security on my protected class so that reflection is not allowed and then your application would be broken. *Evil laugh*
Funny thing, I actually found I had to use that exact technique last week.
I was working on a Swing project which uses drag-and-drop on JTrees. The basic functionality for DnD is built in to JTree, but you can't extend it by writing a subclass of JTree. That's because the functions are performed by a TransferHandler and not by methods of the JTree itself.
You can get the TransferHandler, but of course you can't directly subclass an object. But you can delegate to an object. All that requires is to write a new DelegatingTransferHandler class which implements all of the methods. Each method just calls the corresponding method of the original TransferHandler with the same parameters and returns its result, if any. Then you can create subclasses of that class.
Only problem is, some of the TransferHandler methods are protected. That's where this trick comes in. It's working fine and I don't feel bad about it at all. After all, what I am doing there is equivalent to writing a subclass of TransferHandler.