bennidi/mbassador — pull request #51 by durron597 (2013-09-07 23:53:45+00:00)
Title: Added arguments to Handler annotation and Handler Metadata.

I thought about how to check for generic type safety for a long time, obviously it's impossible because of erasure, but this is the best I was able to come up with. Now the programmer can add arbitrary arguments to filters, which would allow this:

public GenericListFilter extends IMessageFilter {
@Override
public final boolean accepts(Object message, MessageHandlerMetadata metadata) {
if(metadata.arguments().length == 0) return true;
if(!message instanceof List.class) return false;
List list = (List) message;
if(list.isEmpty()) return true;
return list.get(0).getClass().getSimpleName().equals(arguments[0]);
}
}

And then do

@Handler(filters=GenericListFilter.class, arguments="String")
public void handleStringList(List message) { // No risk of heap pollution now!

Another option would be to have a separate Filter class for each generic type, but that seems very cumbersome - though, it is what my code does currently. Thoughts?