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.
This commit is contained in:
Sky Ao 2017-09-19 00:49:49 +08:00 committed by Norman Maurer
parent 1cac3d6cee
commit 5e6dd3778e

View File

@ -76,7 +76,6 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, 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<B extends AbstractBootstrap<B, C>, 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<B extends AbstractBootstrap<B, C>, 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<? extends C> channelFactory) {
if (channelFactory == null) {
throw new NullPointerException("channelFactory");
@ -117,17 +120,16 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, 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<B extends AbstractBootstrap<B, C>, 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 <T> B option(ChannelOption<T> option, T value) {
if (option == null) {
throw new NullPointerException("option");
@ -169,14 +170,13 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, 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 <T> B attr(AttributeKey<T> key, T value) {
if (key == null) {
throw new NullPointerException("key");
@ -190,14 +190,13 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, 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<B extends AbstractBootstrap<B, C>, 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<B extends AbstractBootstrap<B, C>, 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() {