Replace all logic that checks Null with the ObjectUtil utility class (#9145)

Motivation:

Clean the code , replace all logic that checks Null with the ObjectUtil utility class in bootstrap package

Modification:
Replace all logic that checks null with the ObjectUtil utility class

Result:

Less verbose code.
This commit is contained in:
秦世成 2019-05-14 01:53:45 +08:00 committed by Norman Maurer
parent 3221bf6854
commit cf2f1f54b6
3 changed files with 19 additions and 45 deletions

View File

@ -26,10 +26,11 @@ import io.netty.channel.DefaultChannelPromise;
import io.netty.channel.EventLoop; import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup; import io.netty.channel.EventLoopGroup;
import io.netty.channel.ReflectiveChannelFactory; import io.netty.channel.ReflectiveChannelFactory;
import io.netty.util.internal.SocketUtils;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
import io.netty.util.concurrent.EventExecutor; import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.GlobalEventExecutor; import io.netty.util.concurrent.GlobalEventExecutor;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.SocketUtils;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
@ -79,9 +80,7 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C ext
* {@link Channel} * {@link Channel}
*/ */
public B group(EventLoopGroup group) { public B group(EventLoopGroup group) {
if (group == null) { ObjectUtil.checkNotNull(group, "group");
throw new NullPointerException("group");
}
if (this.group != null) { if (this.group != null) {
throw new IllegalStateException("group set already"); throw new IllegalStateException("group set already");
} }
@ -100,10 +99,9 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C ext
* {@link Channel} implementation has no no-args constructor. * {@link Channel} implementation has no no-args constructor.
*/ */
public B channel(Class<? extends C> channelClass) { public B channel(Class<? extends C> channelClass) {
if (channelClass == null) { return channelFactory(new ReflectiveChannelFactory<C>(
throw new NullPointerException("channelClass"); ObjectUtil.checkNotNull(channelClass, "channelClass")
} ));
return channelFactory(new ReflectiveChannelFactory<C>(channelClass));
} }
/** /**
@ -111,9 +109,7 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C ext
*/ */
@Deprecated @Deprecated
public B channelFactory(ChannelFactory<? extends C> channelFactory) { public B channelFactory(ChannelFactory<? extends C> channelFactory) {
if (channelFactory == null) { ObjectUtil.checkNotNull(channelFactory, "channelFactory");
throw new NullPointerException("channelFactory");
}
if (this.channelFactory != null) { if (this.channelFactory != null) {
throw new IllegalStateException("channelFactory set already"); throw new IllegalStateException("channelFactory set already");
} }
@ -168,9 +164,7 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C ext
* created. Use a value of {@code null} to remove a previous set {@link ChannelOption}. * created. Use a value of {@code null} to remove a previous set {@link ChannelOption}.
*/ */
public <T> B option(ChannelOption<T> option, T value) { public <T> B option(ChannelOption<T> option, T value) {
if (option == null) { ObjectUtil.checkNotNull(option, "option");
throw new NullPointerException("option");
}
if (value == null) { if (value == null) {
synchronized (options) { synchronized (options) {
options.remove(option); options.remove(option);
@ -188,9 +182,7 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C ext
* {@code null}, the attribute of the specified {@code key} is removed. * {@code null}, the attribute of the specified {@code key} is removed.
*/ */
public <T> B attr(AttributeKey<T> key, T value) { public <T> B attr(AttributeKey<T> key, T value) {
if (key == null) { ObjectUtil.checkNotNull(key, "key");
throw new NullPointerException("key");
}
if (value == null) { if (value == null) {
synchronized (attrs) { synchronized (attrs) {
attrs.remove(key); attrs.remove(key);
@ -272,10 +264,7 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C ext
*/ */
public ChannelFuture bind(SocketAddress localAddress) { public ChannelFuture bind(SocketAddress localAddress) {
validate(); validate();
if (localAddress == null) { return doBind(ObjectUtil.checkNotNull(localAddress, "localAddress"));
throw new NullPointerException("localAddress");
}
return doBind(localAddress);
} }
private ChannelFuture doBind(final SocketAddress localAddress) { private ChannelFuture doBind(final SocketAddress localAddress) {
@ -375,10 +364,7 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C ext
* the {@link ChannelHandler} to use for serving the requests. * the {@link ChannelHandler} to use for serving the requests.
*/ */
public B handler(ChannelHandler handler) { public B handler(ChannelHandler handler) {
if (handler == null) { this.handler = ObjectUtil.checkNotNull(handler, "handler");
throw new NullPointerException("handler");
}
this.handler = handler;
return self(); return self();
} }

View File

@ -30,6 +30,7 @@ import io.netty.resolver.AddressResolverGroup;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener; import io.netty.util.concurrent.FutureListener;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
@ -137,10 +138,7 @@ public class Bootstrap extends AbstractBootstrap<Bootstrap, Channel> {
* Connect a {@link Channel} to the remote peer. * Connect a {@link Channel} to the remote peer.
*/ */
public ChannelFuture connect(SocketAddress remoteAddress) { public ChannelFuture connect(SocketAddress remoteAddress) {
if (remoteAddress == null) { ObjectUtil.checkNotNull(remoteAddress, "remoteAddress");
throw new NullPointerException("remoteAddress");
}
validate(); validate();
return doResolveAndConnect(remoteAddress, config.localAddress()); return doResolveAndConnect(remoteAddress, config.localAddress());
} }
@ -149,9 +147,7 @@ public class Bootstrap extends AbstractBootstrap<Bootstrap, Channel> {
* Connect a {@link Channel} to the remote peer. * Connect a {@link Channel} to the remote peer.
*/ */
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) { public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) {
if (remoteAddress == null) { ObjectUtil.checkNotNull(remoteAddress, "remoteAddress");
throw new NullPointerException("remoteAddress");
}
validate(); validate();
return doResolveAndConnect(remoteAddress, localAddress); return doResolveAndConnect(remoteAddress, localAddress);
} }

View File

@ -28,6 +28,7 @@ import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup; import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel; import io.netty.channel.ServerChannel;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
@ -79,9 +80,7 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerCh
*/ */
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) { public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) {
super.group(parentGroup); super.group(parentGroup);
if (childGroup == null) { ObjectUtil.checkNotNull(childGroup, "childGroup");
throw new NullPointerException("childGroup");
}
if (this.childGroup != null) { if (this.childGroup != null) {
throw new IllegalStateException("childGroup set already"); throw new IllegalStateException("childGroup set already");
} }
@ -95,9 +94,7 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerCh
* {@link ChannelOption}. * {@link ChannelOption}.
*/ */
public <T> ServerBootstrap childOption(ChannelOption<T> childOption, T value) { public <T> ServerBootstrap childOption(ChannelOption<T> childOption, T value) {
if (childOption == null) { ObjectUtil.checkNotNull(childOption, "childOption");
throw new NullPointerException("childOption");
}
if (value == null) { if (value == null) {
synchronized (childOptions) { synchronized (childOptions) {
childOptions.remove(childOption); childOptions.remove(childOption);
@ -115,9 +112,7 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerCh
* {@code null} the {@link AttributeKey} is removed * {@code null} the {@link AttributeKey} is removed
*/ */
public <T> ServerBootstrap childAttr(AttributeKey<T> childKey, T value) { public <T> ServerBootstrap childAttr(AttributeKey<T> childKey, T value) {
if (childKey == null) { ObjectUtil.checkNotNull(childKey, "childKey");
throw new NullPointerException("childKey");
}
if (value == null) { if (value == null) {
childAttrs.remove(childKey); childAttrs.remove(childKey);
} else { } else {
@ -130,10 +125,7 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerCh
* Set the {@link ChannelHandler} which is used to serve the request for the {@link Channel}'s. * Set the {@link ChannelHandler} which is used to serve the request for the {@link Channel}'s.
*/ */
public ServerBootstrap childHandler(ChannelHandler childHandler) { public ServerBootstrap childHandler(ChannelHandler childHandler) {
if (childHandler == null) { this.childHandler = ObjectUtil.checkNotNull(childHandler, "childHandler");
throw new NullPointerException("childHandler");
}
this.childHandler = childHandler;
return this; return this;
} }