Saturday, July 25, 2009

Great Java Blog + I want CGLIB Multi Dispatch.

I came across a bit of code that, while I really liked the functionality, I despised the implementation.
It basically boils down to Multiple Dispatch:
http://en.wikipedia.org/wiki/Multiple_dispatch

You have an event handler that takes a generic event:
public void handleEvent( GenericEvent event )
{..}

But of course as you generate specific event types ( FooEvent, BarEvent, etc) that extend GenericEvent you end up having to write a big if/else or switch etc.
It would be nice if I could just write handleEvent( FooEvent f ) and have the event hit that method on dispatch...

Of course you can do this in java by writing an EventDispatcher class that reflects on the various methods and then does a method.invoke(...). But I don't really care for the reflection/invoke overhead...
There must be a way to annotate the class and/or the methods, and then use cglib to generate the dispatch plan for you..
For example:

@MultiDispatch
public class EventHandler
{
@MultiMethod
public void handleEvent(GenericEvent g)
{}
@MultiMethod
public void handleEvent(SpecifcEvent s)
{}
@MultiMethod
public void handleEvent(MoreSpecificEvent m)
{}

}

This would even remove the need for an EventDispatcher class.. your container/Advice would generate the bytecode for the dispatch for each individual class.
Obviously you would be lacking the static analysis/compile time checking to make sure all the methods had the same number of params/etc but... It would be faster than reflection.

Frankly I'm shocked this isn't a standard bit of Spring, etc.

While searching for an implementation, I ran across this java/programming blog that was chok full o goodness.
http://mybreadbasket.blogspot.com/


Update: PolyD
Is this what I'm looking for?
http://www.cunei.com/polyd/

1 Comments:

Blogger Vinuth said...

Hey thanks a zillion!!!!

10:28 AM  

Post a Comment

<< Home