Created
July 3, 2012 10:58
-
-
Save trustin/3039071 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
... | |
} | |
} |
Thanks! We can even make getMessage() generics friendly:
<T> T getMessage();
Even if the type is not specified, it will do casting. WDYT?
That case worries me a bit. The JVM will insert the explicit cast in the bytecode and result in unwanted potential class cast exceptions at runtime. It makes it too error prone to mistype. In that case it's better to force the developer to do the cast explicitly so they realize it. Especially since the IDE will force you to insert the cast.
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
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.