»
July 06, 2009
»

GWT Command Pattern With Generics

After waching Best Practices For Architecting Your GWT App session video from Google I/O 2009 I got one more time interested in command pattern for GWT RPC mechanism. Last time I failed to implement it — GWT compiler complained about too generic type “on the wire” (for this reason gwt-dispatch project uses a wrapper async interface). So command pattern with generics in GWT can be done after all! Big thanks for showing me how goes to David Peterson.

So I’m currently starting a new GWT project and using gwt-dispatch approach with one small change: instead of providing action class for lookup in server side of things, I’m using Java reflections to get Action class from ActionHandler.

My getActionType-less ActionHandler interface looks like this:

public interface ActionHandler<A extends Action<R>, R extends Result> {
  R execute(A action);
}

…and for lookup I’m using:

@SuppressWarnings("unchecked")
private Class<? extends Action<?>> getHandlerActionClass(ActionHandler<?, ?> handler) {
  Type[] genericInterfaces = handler.getClass().getGenericInterfaces();
  ParameterizedType actionType = (ParameterizedType) genericInterfaces[0];
  return (Class<? extends Action<?>>) actionType.getActualTypeArguments()[0];
}

what can be directly implemented inside gwt-dispatch DefaultActionHandlerRegistry if desired.

-Update: For curious, I’ve uploaded to my BitBucket temporary repository experimental GWT app using this.-

Update #2: See next blog post for more in detail example and for new repository link.

 
Internet Explorer 6
Are you serious?