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
This commit is contained in:
parent
ed0121fb44
commit
bac833a1e2
@ -70,7 +70,7 @@ public abstract class ByteToMessageCodec<I> extends ChannelDuplexHandler {
|
|||||||
* {@link ByteBuf}, which is backed by an byte array.
|
* {@link ByteBuf}, which is backed by an byte array.
|
||||||
*/
|
*/
|
||||||
protected ByteToMessageCodec(boolean preferDirect) {
|
protected ByteToMessageCodec(boolean preferDirect) {
|
||||||
checkForSharableAnnotation();
|
CodecUtil.ensureNotSharable(this);
|
||||||
outboundMsgMatcher = TypeParameterMatcher.find(this, ByteToMessageCodec.class, "I");
|
outboundMsgMatcher = TypeParameterMatcher.find(this, ByteToMessageCodec.class, "I");
|
||||||
encoder = new Encoder(preferDirect);
|
encoder = new Encoder(preferDirect);
|
||||||
}
|
}
|
||||||
@ -84,17 +84,11 @@ public abstract class ByteToMessageCodec<I> extends ChannelDuplexHandler {
|
|||||||
* {@link ByteBuf}, which is backed by an byte array.
|
* {@link ByteBuf}, which is backed by an byte array.
|
||||||
*/
|
*/
|
||||||
protected ByteToMessageCodec(Class<? extends I> outboundMessageType, boolean preferDirect) {
|
protected ByteToMessageCodec(Class<? extends I> outboundMessageType, boolean preferDirect) {
|
||||||
checkForSharableAnnotation();
|
CodecUtil.ensureNotSharable(this);
|
||||||
outboundMsgMatcher = TypeParameterMatcher.get(outboundMessageType);
|
outboundMsgMatcher = TypeParameterMatcher.get(outboundMessageType);
|
||||||
encoder = new Encoder(preferDirect);
|
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.
|
* Returns {@code true} if and only if the specified message can be encoded by this codec.
|
||||||
*
|
*
|
||||||
|
@ -52,9 +52,7 @@ public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter
|
|||||||
private boolean first;
|
private boolean first;
|
||||||
|
|
||||||
protected ByteToMessageDecoder() {
|
protected ByteToMessageDecoder() {
|
||||||
if (isSharable()) {
|
CodecUtil.ensureNotSharable(this);
|
||||||
throw new IllegalStateException("@Sharable annotation is not allowed");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
32
codec/src/main/java/io/netty/handler/codec/CodecUtil.java
Normal file
32
codec/src/main/java/io/netty/handler/codec/CodecUtil.java
Normal file
@ -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() { }
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user