From b2018777347162936c55f4433db58598ca4a7a56 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 25 Aug 2014 07:36:03 +0200 Subject: [PATCH] Move duplicated code into CodecUtil Motivation: We have some duplicated code that can be reused. Modifications: Create package private class called CodecUtil that now contains the shared code / helper method. Result: Less code-duplication --- .../handler/codec/ByteToMessageCodec.java | 10 ++---- .../handler/codec/ByteToMessageDecoder.java | 4 +-- .../io/netty/handler/codec/CodecUtil.java | 32 +++++++++++++++++++ 3 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 codec/src/main/java/io/netty/handler/codec/CodecUtil.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 e3c0655e01..1ea8503b6c 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java @@ -70,7 +70,7 @@ public abstract class ByteToMessageCodec extends ChannelDuplexHandler { * {@link ByteBuf}, which is backed by an byte array. */ protected ByteToMessageCodec(boolean preferDirect) { - checkForSharableAnnotation(); + CodecUtil.ensureNotSharable(this); outboundMsgMatcher = TypeParameterMatcher.find(this, ByteToMessageCodec.class, "I"); encoder = new Encoder(preferDirect); } @@ -84,17 +84,11 @@ public abstract class ByteToMessageCodec extends ChannelDuplexHandler { * {@link ByteBuf}, which is backed by an byte array. */ protected ByteToMessageCodec(Class outboundMessageType, boolean preferDirect) { - checkForSharableAnnotation(); + CodecUtil.ensureNotSharable(this); outboundMsgMatcher = TypeParameterMatcher.get(outboundMessageType); encoder = new Encoder(preferDirect); } - private void checkForSharableAnnotation() { - if (isSharable()) { - throw new IllegalStateException("@Sharable annotation is not allowed"); - } - } - /** * Returns {@code true} if and only if the specified message can be encoded by this codec. * diff --git a/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java b/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java index 7512696609..35dcc00668 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java @@ -52,9 +52,7 @@ public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter private boolean first; protected ByteToMessageDecoder() { - if (isSharable()) { - throw new IllegalStateException("@Sharable annotation is not allowed"); - } + CodecUtil.ensureNotSharable(this); } /** diff --git a/codec/src/main/java/io/netty/handler/codec/CodecUtil.java b/codec/src/main/java/io/netty/handler/codec/CodecUtil.java new file mode 100644 index 0000000000..8526decba1 --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/CodecUtil.java @@ -0,0 +1,32 @@ +/* + * 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.channel.ChannelHandlerAdapter; + +final class CodecUtil { + + /** + * Throws {@link IllegalStateException} if {@link ChannelHandlerAdapter#isSharable()} returns {@code true} + */ + static void ensureNotSharable(ChannelHandlerAdapter handler) { + if (handler.isSharable()) { + throw new IllegalStateException("@Sharable annotation is not allowed"); + } + } + + private CodecUtil() { } +}