Throw IllegalStateException if implementations of ByteToMessageDecoder and ByteToMessageCodec use @Sharable

This commit is contained in:
Norman Maurer 2013-06-27 22:36:08 +02:00
parent 52691488ee
commit 094d01873b
2 changed files with 25 additions and 0 deletions

View File

@ -22,6 +22,14 @@ import io.netty.channel.ChannelPromise;
import io.netty.channel.MessageList;
import io.netty.util.internal.TypeParameterMatcher;
/**
* A Codec for on-the-fly encoding/decoding of bytes to messages and vise-versa.
*
* This can be thought of as a combination of {@link ByteToMessageDecoder} and {@link MessageToByteEncoder}.
*
* Be aware that sub-classes of {@link ByteToMessageCodec} <strong>MUST NOT</strong>
* annotated with {@link @Sharable}.
*/
public abstract class ByteToMessageCodec<I> extends ChannelDuplexHandler {
private final TypeParameterMatcher outboundMsgMatcher;
@ -50,13 +58,21 @@ public abstract class ByteToMessageCodec<I> extends ChannelDuplexHandler {
};
protected ByteToMessageCodec() {
checkForSharableAnnotation();
outboundMsgMatcher = TypeParameterMatcher.find(this, ByteToMessageCodec.class, "I");
}
protected ByteToMessageCodec(Class<? extends I> outboundMessageType) {
checkForSharableAnnotation();
outboundMsgMatcher = TypeParameterMatcher.get(outboundMessageType);
}
private void checkForSharableAnnotation() {
if (getClass().isAnnotationPresent(Sharable.class)) {
throw new IllegalStateException("@Sharable annotation is not allowed");
}
}
public boolean acceptOutboundMessage(Object msg) throws Exception {
return outboundMsgMatcher.match(msg);
}

View File

@ -38,6 +38,9 @@ import io.netty.util.internal.StringUtil;
* }
* }
* </pre>
*
* Be aware that sub-classes of {@link ByteToMessageDecoder} <strong>MUST NOT</strong>
* annotated with {@link @Sharable}.
*/
public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter {
@ -46,6 +49,12 @@ public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter
private boolean decodeWasNull;
private MessageList<Object> out;
protected ByteToMessageDecoder() {
if (getClass().isAnnotationPresent(Sharable.class)) {
throw new IllegalStateException("@Sharable annotation is not allowed");
}
}
/**
* If set then only one message is decoded on each {@link #messageReceived(ChannelHandlerContext, MessageList)}
* call. This may be useful if you need to do some protocol upgrade and want to make sure nothing is mixed up.