Throw IllegalStateException if implementations of ByteToMessageDecoder and ByteToMessageCodec use @Sharable
This commit is contained in:
parent
52691488ee
commit
094d01873b
@ -22,6 +22,14 @@ import io.netty.channel.ChannelPromise;
|
|||||||
import io.netty.channel.MessageList;
|
import io.netty.channel.MessageList;
|
||||||
import io.netty.util.internal.TypeParameterMatcher;
|
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 {
|
public abstract class ByteToMessageCodec<I> extends ChannelDuplexHandler {
|
||||||
|
|
||||||
private final TypeParameterMatcher outboundMsgMatcher;
|
private final TypeParameterMatcher outboundMsgMatcher;
|
||||||
@ -50,13 +58,21 @@ public abstract class ByteToMessageCodec<I> extends ChannelDuplexHandler {
|
|||||||
};
|
};
|
||||||
|
|
||||||
protected ByteToMessageCodec() {
|
protected ByteToMessageCodec() {
|
||||||
|
checkForSharableAnnotation();
|
||||||
outboundMsgMatcher = TypeParameterMatcher.find(this, ByteToMessageCodec.class, "I");
|
outboundMsgMatcher = TypeParameterMatcher.find(this, ByteToMessageCodec.class, "I");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ByteToMessageCodec(Class<? extends I> outboundMessageType) {
|
protected ByteToMessageCodec(Class<? extends I> outboundMessageType) {
|
||||||
|
checkForSharableAnnotation();
|
||||||
outboundMsgMatcher = TypeParameterMatcher.get(outboundMessageType);
|
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 {
|
public boolean acceptOutboundMessage(Object msg) throws Exception {
|
||||||
return outboundMsgMatcher.match(msg);
|
return outboundMsgMatcher.match(msg);
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,9 @@ import io.netty.util.internal.StringUtil;
|
|||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
* </pre>
|
* </pre>
|
||||||
|
*
|
||||||
|
* Be aware that sub-classes of {@link ByteToMessageDecoder} <strong>MUST NOT</strong>
|
||||||
|
* annotated with {@link @Sharable}.
|
||||||
*/
|
*/
|
||||||
public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter {
|
public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter {
|
||||||
|
|
||||||
@ -46,6 +49,12 @@ public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter
|
|||||||
private boolean decodeWasNull;
|
private boolean decodeWasNull;
|
||||||
private MessageList<Object> out;
|
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)}
|
* 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.
|
* call. This may be useful if you need to do some protocol upgrade and want to make sure nothing is mixed up.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user