From 5e6dd3778e8a4cb4a3464e4af5ca9e4bb53c19c0 Mon Sep 17 00:00:00 2001 From: Sky Ao Date: Tue, 19 Sep 2017 00:49:49 +0800 Subject: [PATCH] extract self() method to avoid too many unchecked @SuppressWarnings Motivation: There are many @SuppressWarnings("unchecked") in the code for the same purpose that we want to do this return: @SuppressWarnings("unchecked") public B someMethod() { ...... return (B) this; } Modification: Add a method self() and reuse in all these return lines: @SuppressWarnings("unchecked") private B self() { return (B) this; } Result: Then only one @SuppressWarnings("unchecked") left in the code. --- .../io/netty/bootstrap/AbstractBootstrap.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java b/transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java index f73bc53c5a..62a7b7d517 100644 --- a/transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java +++ b/transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java @@ -76,7 +76,6 @@ public abstract class AbstractBootstrap, C ext * The {@link EventLoopGroup} which is used to handle all the events for the to-be-created * {@link Channel} */ - @SuppressWarnings("unchecked") public B group(EventLoopGroup group) { if (group == null) { throw new NullPointerException("group"); @@ -85,6 +84,11 @@ public abstract class AbstractBootstrap, C ext throw new IllegalStateException("group set already"); } this.group = group; + return self(); + } + + @SuppressWarnings("unchecked") + private B self() { return (B) this; } @@ -107,7 +111,6 @@ public abstract class AbstractBootstrap, C ext * has a no-args constructor, its highly recommend to just use {@link #channel(Class)} for * simplify your code. */ - @SuppressWarnings("unchecked") public B channelFactory(ChannelFactory channelFactory) { if (channelFactory == null) { throw new NullPointerException("channelFactory"); @@ -117,17 +120,16 @@ public abstract class AbstractBootstrap, C ext } this.channelFactory = channelFactory; - return (B) this; + return self(); } /** * The {@link SocketAddress} which is used to bind the local "end" to. * */ - @SuppressWarnings("unchecked") public B localAddress(SocketAddress localAddress) { this.localAddress = localAddress; - return (B) this; + return self(); } /** @@ -155,7 +157,6 @@ public abstract class AbstractBootstrap, C ext * Allow to specify a {@link ChannelOption} which is used for the {@link Channel} instances once they got * created. Use a value of {@code null} to remove a previous set {@link ChannelOption}. */ - @SuppressWarnings("unchecked") public B option(ChannelOption option, T value) { if (option == null) { throw new NullPointerException("option"); @@ -169,14 +170,13 @@ public abstract class AbstractBootstrap, C ext options.put(option, value); } } - return (B) this; + return self(); } /** * Allow to specify an initial attribute of the newly created {@link Channel}. If the {@code value} is * {@code null}, the attribute of the specified {@code key} is removed. */ - @SuppressWarnings("unchecked") public B attr(AttributeKey key, T value) { if (key == null) { throw new NullPointerException("key"); @@ -190,14 +190,13 @@ public abstract class AbstractBootstrap, C ext attrs.put(key, value); } } - return (B) this; + return self(); } /** * Validate all the parameters. Sub-classes may override this, but should * call the super method in that case. */ - @SuppressWarnings("unchecked") public B validate() { if (group == null) { throw new IllegalStateException("group not set"); @@ -205,7 +204,7 @@ public abstract class AbstractBootstrap, C ext if (channelFactory == null) { throw new IllegalStateException("channel or channelFactory not set"); } - return (B) this; + return self(); } /** @@ -363,13 +362,12 @@ public abstract class AbstractBootstrap, C ext /** * the {@link ChannelHandler} to use for serving the requests. */ - @SuppressWarnings("unchecked") public B handler(ChannelHandler handler) { if (handler == null) { throw new NullPointerException("handler"); } this.handler = handler; - return (B) this; + return self(); } final SocketAddress localAddress() {