Skip to content

Instantly share code, notes, and snippets.

@trustin
Created July 3, 2012 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trustin/3039071 to your computer and use it in GitHub Desktop.
Save trustin/3039071 to your computer and use it in GitHub Desktop.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent evt) {
Object msg = evt.getMessage();
if (msg instanceof ChannelBuffer) {
ChannelBuffer buf = (ChannelBuffer) msg;
...
} else {
MyMessage myMsg = (MyMessage) msg;
...
}
}
@nicholashagen
Copy link

Should that be evt.getMessage(....currently it is called getMessaget <- note the trailing t.

Also, what about a convenience method T getMessageAs(Class type)

That would do the cast check and return so users don't have to do checks if they always expect a certain type.

@trustin
Copy link
Author

trustin commented Jul 4, 2012

Thanks! We can even make getMessage() generics friendly:

<T> T getMessage();

Even if the type is not specified, it will do casting. WDYT?

@nicholashagen
Copy link

nicholashagen commented Jul 4, 2012 via email

@trustin
Copy link
Author

trustin commented Jul 4, 2012

Does forcing a user to specify the type as a parameter really help? A user can still type completely wrong type in two places.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment