From 8ebaa1b9721c9a6e2d4c85ce833143878a0895c1 Mon Sep 17 00:00:00 2001 From: kezhenxu94 Date: Sat, 19 Jan 2019 02:44:47 +0800 Subject: [PATCH] enhancement: extract duplicate code (#8732) Motivation: Clean up code to increase readability. Modification: Extract duplicate code and remove unnecessary throws Result: Share more code. --- .../netty/channel/DefaultChannelPipeline.java | 111 ++++++++---------- 1 file changed, 48 insertions(+), 63 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java index 2155a7cbb3..2b307cc911 100644 --- a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java @@ -53,7 +53,7 @@ public class DefaultChannelPipeline implements ChannelPipeline { private static final FastThreadLocal, String>> nameCaches = new FastThreadLocal, String>>() { @Override - protected Map, String> initialValue() throws Exception { + protected Map, String> initialValue() { return new WeakHashMap, String>(); } }; @@ -163,7 +163,7 @@ public class DefaultChannelPipeline implements ChannelPipeline { addFirst0(newCtx); - // If the registered is false it means that the channel was not registered on an eventloop yet. + // If the registered is false it means that the channel was not registered on an eventLoop yet. // In this case we add the context to the pipeline and add a task that will call // ChannelHandler.handlerAdded(...) once the channel is registered. if (!registered) { @@ -174,13 +174,7 @@ public class DefaultChannelPipeline implements ChannelPipeline { EventExecutor executor = newCtx.executor(); if (!executor.inEventLoop()) { - newCtx.setAddPending(); - executor.execute(new Runnable() { - @Override - public void run() { - callHandlerAdded0(newCtx); - } - }); + callHandlerAddedInEventLoop(newCtx, executor); return this; } } @@ -211,7 +205,7 @@ public class DefaultChannelPipeline implements ChannelPipeline { addLast0(newCtx); - // If the registered is false it means that the channel was not registered on an eventloop yet. + // If the registered is false it means that the channel was not registered on an eventLoop yet. // In this case we add the context to the pipeline and add a task that will call // ChannelHandler.handlerAdded(...) once the channel is registered. if (!registered) { @@ -222,13 +216,7 @@ public class DefaultChannelPipeline implements ChannelPipeline { EventExecutor executor = newCtx.executor(); if (!executor.inEventLoop()) { - newCtx.setAddPending(); - executor.execute(new Runnable() { - @Override - public void run() { - callHandlerAdded0(newCtx); - } - }); + callHandlerAddedInEventLoop(newCtx, executor); return this; } } @@ -263,7 +251,7 @@ public class DefaultChannelPipeline implements ChannelPipeline { addBefore0(ctx, newCtx); - // If the registered is false it means that the channel was not registered on an eventloop yet. + // If the registered is false it means that the channel was not registered on an eventLoop yet. // In this case we add the context to the pipeline and add a task that will call // ChannelHandler.handlerAdded(...) once the channel is registered. if (!registered) { @@ -274,13 +262,7 @@ public class DefaultChannelPipeline implements ChannelPipeline { EventExecutor executor = newCtx.executor(); if (!executor.inEventLoop()) { - newCtx.setAddPending(); - executor.execute(new Runnable() { - @Override - public void run() { - callHandlerAdded0(newCtx); - } - }); + callHandlerAddedInEventLoop(newCtx, executor); return this; } } @@ -323,7 +305,7 @@ public class DefaultChannelPipeline implements ChannelPipeline { addAfter0(ctx, newCtx); - // If the registered is false it means that the channel was not registered on an eventloop yet. + // If the registered is false it means that the channel was not registered on an eventLoop yet. // In this case we remove the context from the pipeline and add a task that will call // ChannelHandler.handlerRemoved(...) once the channel is registered. if (!registered) { @@ -333,13 +315,7 @@ public class DefaultChannelPipeline implements ChannelPipeline { } EventExecutor executor = newCtx.executor(); if (!executor.inEventLoop()) { - newCtx.setAddPending(); - executor.execute(new Runnable() { - @Override - public void run() { - callHandlerAdded0(newCtx); - } - }); + callHandlerAddedInEventLoop(newCtx, executor); return this; } } @@ -1168,6 +1144,16 @@ public class DefaultChannelPipeline implements ChannelPipeline { } } + private void callHandlerAddedInEventLoop(final AbstractChannelHandlerContext newCtx, EventExecutor executor) { + newCtx.setAddPending(); + executor.execute(new Runnable() { + @Override + public void run() { + callHandlerAdded0(newCtx); + } + }); + } + /** * Called once a {@link Throwable} hit the end of the {@link ChannelPipeline} without been handled by the user * in {@link ChannelHandler#exceptionCaught(ChannelHandlerContext, Throwable)}. @@ -1267,49 +1253,49 @@ public class DefaultChannelPipeline implements ChannelPipeline { } @Override - public void channelRegistered(ChannelHandlerContext ctx) throws Exception { } + public void channelRegistered(ChannelHandlerContext ctx) { } @Override - public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { } + public void channelUnregistered(ChannelHandlerContext ctx) { } @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { + public void channelActive(ChannelHandlerContext ctx) { onUnhandledInboundChannelActive(); } @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { + public void channelInactive(ChannelHandlerContext ctx) { onUnhandledInboundChannelInactive(); } @Override - public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { + public void channelWritabilityChanged(ChannelHandlerContext ctx) { onUnhandledChannelWritabilityChanged(); } @Override - public void handlerAdded(ChannelHandlerContext ctx) throws Exception { } + public void handlerAdded(ChannelHandlerContext ctx) { } @Override - public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { } + public void handlerRemoved(ChannelHandlerContext ctx) { } @Override - public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { onUnhandledInboundUserEventTriggered(evt); } @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { onUnhandledInboundException(cause); } @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + public void channelRead(ChannelHandlerContext ctx, Object msg) { onUnhandledInboundMessage(msg); } @Override - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + public void channelReadComplete(ChannelHandlerContext ctx) { onUnhandledInboundChannelReadComplete(); } } @@ -1331,19 +1317,18 @@ public class DefaultChannelPipeline implements ChannelPipeline { } @Override - public void handlerAdded(ChannelHandlerContext ctx) throws Exception { + public void handlerAdded(ChannelHandlerContext ctx) { // NOOP } @Override - public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { + public void handlerRemoved(ChannelHandlerContext ctx) { // NOOP } @Override public void bind( - ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) - throws Exception { + ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) { unsafe.bind(localAddress, promise); } @@ -1351,22 +1336,22 @@ public class DefaultChannelPipeline implements ChannelPipeline { public void connect( ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, - ChannelPromise promise) throws Exception { + ChannelPromise promise) { unsafe.connect(remoteAddress, localAddress, promise); } @Override - public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) { unsafe.disconnect(promise); } @Override - public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + public void close(ChannelHandlerContext ctx, ChannelPromise promise) { unsafe.close(promise); } @Override - public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) { unsafe.deregister(promise); } @@ -1376,28 +1361,28 @@ public class DefaultChannelPipeline implements ChannelPipeline { } @Override - public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) { unsafe.write(msg, promise); } @Override - public void flush(ChannelHandlerContext ctx) throws Exception { + public void flush(ChannelHandlerContext ctx) { unsafe.flush(); } @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { ctx.fireExceptionCaught(cause); } @Override - public void channelRegistered(ChannelHandlerContext ctx) throws Exception { + public void channelRegistered(ChannelHandlerContext ctx) { invokeHandlerAddedIfNeeded(); ctx.fireChannelRegistered(); } @Override - public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { + public void channelUnregistered(ChannelHandlerContext ctx) { ctx.fireChannelUnregistered(); // Remove all handlers sequentially if channel is closed and unregistered. @@ -1407,24 +1392,24 @@ public class DefaultChannelPipeline implements ChannelPipeline { } @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { + public void channelActive(ChannelHandlerContext ctx) { ctx.fireChannelActive(); readIfIsAutoRead(); } @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { + public void channelInactive(ChannelHandlerContext ctx) { ctx.fireChannelInactive(); } @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + public void channelRead(ChannelHandlerContext ctx, Object msg) { ctx.fireChannelRead(msg); } @Override - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + public void channelReadComplete(ChannelHandlerContext ctx) { ctx.fireChannelReadComplete(); readIfIsAutoRead(); @@ -1437,12 +1422,12 @@ public class DefaultChannelPipeline implements ChannelPipeline { } @Override - public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { ctx.fireUserEventTriggered(evt); } @Override - public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { + public void channelWritabilityChanged(ChannelHandlerContext ctx) { ctx.fireChannelWritabilityChanged(); } }