From 8785ff525629b739b1eca725cc97d5b9902fbb3e 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 75cb19869c..be7aad1dd4 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 ChannelHandlerAdapter { * {@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 ChannelHandlerAdapter { * {@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 7e56dcee68..5def01c0db 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java @@ -53,9 +53,7 @@ public abstract class ByteToMessageDecoder extends ChannelHandlerAdapter { 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() { } +}