From eb563c25b43bba2ef0e4e6d0b85284415049d552 Mon Sep 17 00:00:00 2001 From: Boris Unckel Date: Thu, 22 Apr 2021 14:13:14 +0200 Subject: [PATCH] Utilize i.n.u.internal.ObjectUtil to assert Preconditions (transport*) (#11170) (#11181) Motivation: NullChecks resulting in a NullPointerException or IllegalArgumentException, numeric ranges (>0, >=0) checks, not empty strings/arrays checks must never be anonymous but with the parameter or variable name which is checked. They must be specific and should not be done with an "OR-Logic" (if a == null || b == null) throw new NullPointerEx. Modifications: * import static relevant checks * Replace manual checks with ObjectUtil methods Result: All checks needed are done with ObjectUtil, some exception texts are improved. Fixes #11170 --- .../main/java/io/netty/channel/epoll/TcpMd5Util.java | 11 ++++------- .../java/io/netty/channel/DefaultChannelConfig.java | 3 ++- .../java/io/netty/channel/local/LocalAddress.java | 11 +++-------- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/TcpMd5Util.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/TcpMd5Util.java index ebf82c5401..cbf18337d4 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/TcpMd5Util.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/TcpMd5Util.java @@ -16,6 +16,8 @@ package io.netty.channel.epoll; import static java.util.Objects.requireNonNull; +import static io.netty.util.internal.ObjectUtil.checkNotNullWithIAE; +import static io.netty.util.internal.ObjectUtil.checkNonEmpty; import java.io.IOException; import java.net.InetAddress; @@ -36,13 +38,8 @@ final class TcpMd5Util { // Validate incoming values for (Entry e : newKeys.entrySet()) { final byte[] key = e.getValue(); - if (e.getKey() == null) { - throw new IllegalArgumentException("newKeys contains an entry with null address: " + newKeys); - } - requireNonNull(key, "newKeys[" + e.getKey() + "]"); - if (key.length == 0) { - throw new IllegalArgumentException("newKeys[" + e.getKey() + "] has an empty key."); - } + checkNotNullWithIAE(e.getKey(), "e.getKey"); + checkNonEmpty(key, e.getKey().toString()); if (key.length > Native.TCP_MD5SIG_MAXKEYLEN) { throw new IllegalArgumentException("newKeys[" + e.getKey() + "] has a key with invalid length; should not exceed the maximum length (" + diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelConfig.java b/transport/src/main/java/io/netty/channel/DefaultChannelConfig.java index 4154de6dd9..28b6ae0a36 100644 --- a/transport/src/main/java/io/netty/channel/DefaultChannelConfig.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelConfig.java @@ -316,10 +316,11 @@ public class DefaultChannelConfig implements ChannelConfig { * is of type {@link MaxMessagesRecvByteBufAllocator}. */ private void setRecvByteBufAllocator(RecvByteBufAllocator allocator, ChannelMetadata metadata) { + requireNonNull(allocator, "allocator"); + requireNonNull(metadata, "metadata"); if (allocator instanceof MaxMessagesRecvByteBufAllocator) { ((MaxMessagesRecvByteBufAllocator) allocator).maxMessagesPerRead(metadata.defaultMaxMessagesPerRead()); } - requireNonNull(allocator, "allocator"); setRecvByteBufAllocator(allocator); } diff --git a/transport/src/main/java/io/netty/channel/local/LocalAddress.java b/transport/src/main/java/io/netty/channel/local/LocalAddress.java index f4c7587bc9..c45b5bc592 100644 --- a/transport/src/main/java/io/netty/channel/local/LocalAddress.java +++ b/transport/src/main/java/io/netty/channel/local/LocalAddress.java @@ -15,7 +15,7 @@ */ package io.netty.channel.local; -import static java.util.Objects.requireNonNull; +import static io.netty.util.internal.ObjectUtil.checkNonEmptyAfterTrim; import io.netty.channel.Channel; @@ -52,13 +52,8 @@ public final class LocalAddress extends SocketAddress implements Comparable