diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentEncoder.java index d156491596..0e36b77495 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentEncoder.java @@ -47,22 +47,13 @@ import java.util.Queue; * so that this handler can intercept HTTP responses before {@link HttpObjectEncoder} * converts them into {@link ByteBuf}s. */ -public abstract class HttpContentEncoder extends MessageToMessageCodec { +public abstract class HttpContentEncoder extends MessageToMessageCodec { private final Queue acceptEncodingQueue = new ArrayDeque(); private EmbeddedByteChannel encoder; private HttpMessage message; private boolean encodeStarted; - /** - * Creates a new instance. - */ - protected HttpContentEncoder() { - super( - new Class[] { HttpMessage.class }, - new Class[] { HttpObject.class }); - } - @Override protected Object decode(ChannelHandlerContext ctx, HttpMessage msg) throws Exception { @@ -76,7 +67,7 @@ public abstract class HttpContentEncoder extends MessageToMessageCodec ids = new LinkedList(); - public SpdyHttpResponseStreamIdHandler() { - super(new Class[] { HttpMessage.class, SpdyRstStreamFrame.class }, new Class[] { HttpMessage.class }); + @Override + public boolean acceptInboundMessage(Object msg) throws Exception { + return msg instanceof HttpMessage || msg instanceof SpdyRstStreamFrame; } @Override diff --git a/codec/src/main/java/io/netty/handler/codec/MessageToMessageCodec.java b/codec/src/main/java/io/netty/handler/codec/MessageToMessageCodec.java index f2417ed7c2..8edc103491 100644 --- a/codec/src/main/java/io/netty/handler/codec/MessageToMessageCodec.java +++ b/codec/src/main/java/io/netty/handler/codec/MessageToMessageCodec.java @@ -20,8 +20,11 @@ import io.netty.channel.ChannelDuplexHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerUtil; import io.netty.channel.ChannelInboundMessageHandler; +import io.netty.channel.ChannelInboundMessageHandlerAdapter; import io.netty.channel.ChannelOutboundMessageHandler; +import io.netty.channel.ChannelOutboundMessageHandlerAdapter; import io.netty.channel.ChannelPromise; +import io.netty.util.internal.TypeParameterFinder; /** * A Codec for on-the-fly encoding/decoding of message. @@ -53,59 +56,73 @@ public abstract class MessageToMessageCodec implements ChannelInboundMessageHandler, ChannelOutboundMessageHandler { - private final MessageToMessageEncoder encoder = - new MessageToMessageEncoder() { + private final MessageToMessageEncoder encoder = + new MessageToMessageEncoder() { @Override public boolean isEncodable(Object msg) throws Exception { - return MessageToMessageCodec.this.isEncodable(msg); + return MessageToMessageCodec.this.acceptOutboundMessage(msg); } @Override - public Object encode(ChannelHandlerContext ctx, OUTBOUND_IN msg) throws Exception { - return MessageToMessageCodec.this.encode(ctx, msg); + @SuppressWarnings("unchecked") + public Object encode(ChannelHandlerContext ctx, Object msg) throws Exception { + return MessageToMessageCodec.this.encode(ctx, (OUTBOUND_IN) msg); } @Override - protected void freeOutboundMessage(OUTBOUND_IN msg) throws Exception { - MessageToMessageCodec.this.freeOutboundMessage(msg); + @SuppressWarnings("unchecked") + protected void freeOutboundMessage(Object msg) throws Exception { + MessageToMessageCodec.this.freeOutboundMessage((OUTBOUND_IN) msg); } }; - private final MessageToMessageDecoder decoder = - new MessageToMessageDecoder() { + private final MessageToMessageDecoder decoder = + new MessageToMessageDecoder() { @Override public boolean acceptInboundMessage(Object msg) throws Exception { - return isDecodable(msg); + return MessageToMessageCodec.this.acceptInboundMessage(msg); } @Override - public Object decode(ChannelHandlerContext ctx, INBOUND_IN msg) throws Exception { - return MessageToMessageCodec.this.decode(ctx, msg); + @SuppressWarnings("unchecked") + public Object decode(ChannelHandlerContext ctx, Object msg) throws Exception { + return MessageToMessageCodec.this.decode(ctx, (INBOUND_IN) msg); } @Override - protected void freeInboundMessage(INBOUND_IN msg) throws Exception { - MessageToMessageCodec.this.freeInboundMessage(msg); + @SuppressWarnings("unchecked") + protected void freeInboundMessage(Object msg) throws Exception { + MessageToMessageCodec.this.freeInboundMessage((INBOUND_IN) msg); } }; - private final Class[] acceptedInboundMsgTypes; - private final Class[] acceptedOutboundMsgTypes; + private final Class acceptedInboundMsgType; + private final Class acceptedOutboundMsgType; protected MessageToMessageCodec() { - this(null, null); + acceptedInboundMsgType = TypeParameterFinder.findActualTypeParameter(this, MessageToMessageCodec.class, 0); + acceptedOutboundMsgType = TypeParameterFinder.findActualTypeParameter(this, MessageToMessageCodec.class, 1); } protected MessageToMessageCodec( - Class[] acceptedInboundMsgTypes, Class[] acceptedOutboundMsgTypes) { - this.acceptedInboundMsgTypes = ChannelHandlerUtil.acceptedMessageTypes(acceptedInboundMsgTypes); - this.acceptedOutboundMsgTypes = ChannelHandlerUtil.acceptedMessageTypes(acceptedOutboundMsgTypes); + @SuppressWarnings("rawtypes") + Class parameterizedInboundHandlerType, + int inboundMessageTypeParamIndex, + @SuppressWarnings("rawtypes") + Class parameterizedOutboundHandlerType, + int outboundMessageTypeParamIndex) { + + acceptedInboundMsgType = TypeParameterFinder.findActualTypeParameter( + this, parameterizedInboundHandlerType, inboundMessageTypeParamIndex); + acceptedOutboundMsgType = TypeParameterFinder.findActualTypeParameter( + this, parameterizedOutboundHandlerType, outboundMessageTypeParamIndex); } @Override + @SuppressWarnings("unchecked") public MessageBuf newInboundBuffer(ChannelHandlerContext ctx) throws Exception { - return decoder.newInboundBuffer(ctx); + return (MessageBuf) decoder.newInboundBuffer(ctx); } @Override @@ -114,8 +131,9 @@ public abstract class MessageToMessageCodec } @Override + @SuppressWarnings("unchecked") public MessageBuf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception { - return encoder.newOutboundBuffer(ctx); + return (MessageBuf) encoder.newOutboundBuffer(ctx); } @Override @@ -139,8 +157,8 @@ public abstract class MessageToMessageCodec * * @param msg the message */ - public boolean isDecodable(Object msg) throws Exception { - return ChannelHandlerUtil.acceptMessage(acceptedInboundMsgTypes, msg); + public boolean acceptInboundMessage(Object msg) throws Exception { + return acceptedInboundMsgType.isInstance(msg); } /** @@ -148,8 +166,8 @@ public abstract class MessageToMessageCodec * * @param msg the message */ - public boolean isEncodable(Object msg) throws Exception { - return ChannelHandlerUtil.acceptMessage(acceptedOutboundMsgTypes, msg); + public boolean acceptOutboundMessage(Object msg) throws Exception { + return acceptedOutboundMsgType.isInstance(msg); } protected abstract Object encode(ChannelHandlerContext ctx, OUTBOUND_IN msg) throws Exception; diff --git a/common/src/main/java/io/netty/util/internal/TypeParameterFinder.java b/common/src/main/java/io/netty/util/internal/TypeParameterFinder.java index 3e5574e158..55bdcb25bb 100644 --- a/common/src/main/java/io/netty/util/internal/TypeParameterFinder.java +++ b/common/src/main/java/io/netty/util/internal/TypeParameterFinder.java @@ -52,6 +52,5 @@ public final class TypeParameterFinder { return messageType; } - private TypeParameterFinder() { } }