From fbf853375982acdddf5be676b1c647182b2d210c Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sat, 23 Aug 2014 21:01:23 +0200 Subject: [PATCH] [#2812] Ensure we call checkForSharableAnnotation in all constructors of ByteToMessageCodec Motivation: ByteToMessageCodec miss to check for @Sharable annotation in one of its constructors. Modifications: Ensure we call checkForSharableAnnotation in all constructors. Result: After your change, what will change. --- .../handler/codec/ByteToMessageCodec.java | 1 + .../handler/codec/ByteToMessageCodecTest.java | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 codec/src/test/java/io/netty/handler/codec/ByteToMessageCodecTest.java diff --git a/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java b/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java index bcec34d960..e3c0655e01 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java @@ -70,6 +70,7 @@ public abstract class ByteToMessageCodec extends ChannelDuplexHandler { * {@link ByteBuf}, which is backed by an byte array. */ protected ByteToMessageCodec(boolean preferDirect) { + checkForSharableAnnotation(); outboundMsgMatcher = TypeParameterMatcher.find(this, ByteToMessageCodec.class, "I"); encoder = new Encoder(preferDirect); } diff --git a/codec/src/test/java/io/netty/handler/codec/ByteToMessageCodecTest.java b/codec/src/test/java/io/netty/handler/codec/ByteToMessageCodecTest.java new file mode 100644 index 0000000000..47cb911be7 --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/ByteToMessageCodecTest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2014 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import org.junit.Test; + +import java.util.List; + +public class ByteToMessageCodecTest { + + @Test(expected = IllegalStateException.class) + public void testSharable() { + new InvalidByteToMessageCodec(); + } + + @Test(expected = IllegalStateException.class) + public void testSharable2() { + new InvalidByteToMessageCodec2(); + } + + @ChannelHandler.Sharable + private static final class InvalidByteToMessageCodec extends ByteToMessageCodec { + InvalidByteToMessageCodec() { + super(true); + } + + @Override + protected void encode(ChannelHandlerContext ctx, Integer msg, ByteBuf out) throws Exception { } + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { } + } + + @ChannelHandler.Sharable + private static final class InvalidByteToMessageCodec2 extends ByteToMessageCodec { + InvalidByteToMessageCodec2() { + super(Integer.class, true); + } + + @Override + protected void encode(ChannelHandlerContext ctx, Integer msg, ByteBuf out) throws Exception { } + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { } + } +}