diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java index 41c99d3e41..abc83f7e1e 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java @@ -58,7 +58,6 @@ public class WebSocketServerProtocolHandler extends ChannelInboundMessageHandler } public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions) { - super(WebSocketFrame.class); this.websocketPath = websocketPath; this.subprotocols = subprotocols; this.allowExtensions = allowExtensions; diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java index 4d0deb0cae..16102df5aa 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java @@ -44,7 +44,6 @@ public class WebSocketServerProtocolHandshakeHandler public WebSocketServerProtocolHandshakeHandler(String websocketPath, String subprotocols, boolean allowExtensions) { - super(FullHttpRequest.class); this.websocketPath = websocketPath; this.subprotocols = subprotocols; this.allowExtensions = allowExtensions; diff --git a/example/src/main/java/io/netty/example/socksproxy/SocksServerConnectHandler.java b/example/src/main/java/io/netty/example/socksproxy/SocksServerConnectHandler.java index b1d2398490..5615e84236 100644 --- a/example/src/main/java/io/netty/example/socksproxy/SocksServerConnectHandler.java +++ b/example/src/main/java/io/netty/example/socksproxy/SocksServerConnectHandler.java @@ -37,12 +37,7 @@ public final class SocksServerConnectHandler extends ChannelInboundMessageHandle return name; } - private final Bootstrap b; - - public SocksServerConnectHandler() { - super(SocksCmdRequest.class); - b = new Bootstrap(); - } + private final Bootstrap b = new Bootstrap(); @Override public void messageReceived(final ChannelHandlerContext ctx, final SocksCmdRequest request) throws Exception { diff --git a/example/src/main/java/io/netty/example/socksproxy/SocksServerHandler.java b/example/src/main/java/io/netty/example/socksproxy/SocksServerHandler.java index cc3c920c05..18b50c6e19 100644 --- a/example/src/main/java/io/netty/example/socksproxy/SocksServerHandler.java +++ b/example/src/main/java/io/netty/example/socksproxy/SocksServerHandler.java @@ -34,10 +34,6 @@ public final class SocksServerHandler extends ChannelInboundMessageHandlerAdapte return name; } - public SocksServerHandler() { - super(SocksRequest.class); - } - @Override public void messageReceived(ChannelHandlerContext ctx, SocksRequest socksRequest) throws Exception { switch (socksRequest.requestType()) { diff --git a/transport-sctp/src/main/java/io/netty/handler/codec/sctp/SctpInboundByteStreamHandler.java b/transport-sctp/src/main/java/io/netty/handler/codec/sctp/SctpInboundByteStreamHandler.java index 951fa4bcf4..057ac4f12d 100644 --- a/transport-sctp/src/main/java/io/netty/handler/codec/sctp/SctpInboundByteStreamHandler.java +++ b/transport-sctp/src/main/java/io/netty/handler/codec/sctp/SctpInboundByteStreamHandler.java @@ -35,7 +35,6 @@ public class SctpInboundByteStreamHandler extends ChannelInboundMessageHandlerAd * @param protocolIdentifier supported application protocol. */ public SctpInboundByteStreamHandler(int protocolIdentifier, int streamIdentifier) { - super(SctpMessage.class); this.protocolIdentifier = protocolIdentifier; this.streamIdentifier = streamIdentifier; } diff --git a/transport-udt/src/test/java/io/netty/test/udt/util/EchoMessageHandler.java b/transport-udt/src/test/java/io/netty/test/udt/util/EchoMessageHandler.java index 4683f12ee8..d91f78c910 100644 --- a/transport-udt/src/test/java/io/netty/test/udt/util/EchoMessageHandler.java +++ b/transport-udt/src/test/java/io/netty/test/udt/util/EchoMessageHandler.java @@ -46,9 +46,6 @@ public class EchoMessageHandler extends } public EchoMessageHandler(final Meter meter, final int messageSize) { - - super(UdtMessage.class); - this.meter = meter; final ByteBuf byteBuf = Unpooled.buffer(messageSize); diff --git a/transport/src/main/java/io/netty/channel/ChannelInboundMessageHandlerAdapter.java b/transport/src/main/java/io/netty/channel/ChannelInboundMessageHandlerAdapter.java index 3fe4d28df1..7bfd25cd57 100644 --- a/transport/src/main/java/io/netty/channel/ChannelInboundMessageHandlerAdapter.java +++ b/transport/src/main/java/io/netty/channel/ChannelInboundMessageHandlerAdapter.java @@ -18,6 +18,10 @@ package io.netty.channel; import io.netty.buffer.MessageBuf; import io.netty.buffer.Unpooled; +import java.lang.reflect.Method; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + /** * {@link ChannelHandler} which handles inbound messages of a specific type. * @@ -43,14 +47,43 @@ import io.netty.buffer.Unpooled; public abstract class ChannelInboundMessageHandlerAdapter extends ChannelStateHandlerAdapter implements ChannelInboundMessageHandler { - private final Class[] acceptedMsgTypes; + private static final ConcurrentMap, Class> messageTypeMap = + new ConcurrentHashMap, Class>(); - /** - * The types which will be accepted by the message handler. If a received message is an other type it will be just - * forwarded to the next {@link ChannelInboundMessageHandler} in the {@link ChannelPipeline}. - */ - protected ChannelInboundMessageHandlerAdapter(Class... acceptedMsgTypes) { - this.acceptedMsgTypes = ChannelHandlerUtil.acceptedMessageTypes(acceptedMsgTypes); + private final Class acceptedMsgType = findMessageType(); + + private Class findMessageType() { + Class thisClass = getClass(); + Class messageType = messageTypeMap.get(thisClass); + if (messageType == null) { + for (Method m: getClass().getDeclaredMethods()) { + if (!"messageReceived".equals(m.getName())) { + continue; + } + if (m.isSynthetic() || m.isBridge()) { + continue; + } + Class[] p = m.getParameterTypes(); + if (p.length != 2) { + continue; + } + if (p[0] != ChannelHandlerContext.class) { + continue; + } + + messageType = p[1]; + break; + } + + if (messageType == null) { + throw new IllegalStateException( + "cannot determine the inbound message type of " + thisClass.getSimpleName()); + } + + messageTypeMap.put(thisClass, messageType); + } + + return messageType; } @Override @@ -121,7 +154,7 @@ public abstract class ChannelInboundMessageHandlerAdapter * @param msg the message */ public boolean isSupported(Object msg) throws Exception { - return ChannelHandlerUtil.acceptMessage(acceptedMsgTypes, msg); + return acceptedMsgType.isInstance(msg); } /** diff --git a/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java b/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java index 10a03c30d2..540916a506 100644 --- a/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java +++ b/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java @@ -155,10 +155,6 @@ public class DefaultChannelPipelineTest { private static final class StringInboundHandler extends ChannelInboundMessageHandlerAdapter { boolean called; - public StringInboundHandler() { - super(String.class); - } - @Override public boolean isSupported(Object msg) throws Exception { called = true;