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
This commit is contained in:
Boris Unckel 2021-04-22 14:13:14 +02:00 committed by Norman Maurer
parent 83297ed2ba
commit eb563c25b4
3 changed files with 9 additions and 16 deletions

View File

@ -16,6 +16,8 @@
package io.netty.channel.epoll; package io.netty.channel.epoll;
import static java.util.Objects.requireNonNull; 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.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
@ -36,13 +38,8 @@ final class TcpMd5Util {
// Validate incoming values // Validate incoming values
for (Entry<InetAddress, byte[]> e : newKeys.entrySet()) { for (Entry<InetAddress, byte[]> e : newKeys.entrySet()) {
final byte[] key = e.getValue(); final byte[] key = e.getValue();
if (e.getKey() == null) { checkNotNullWithIAE(e.getKey(), "e.getKey");
throw new IllegalArgumentException("newKeys contains an entry with null address: " + newKeys); checkNonEmpty(key, e.getKey().toString());
}
requireNonNull(key, "newKeys[" + e.getKey() + "]");
if (key.length == 0) {
throw new IllegalArgumentException("newKeys[" + e.getKey() + "] has an empty key.");
}
if (key.length > Native.TCP_MD5SIG_MAXKEYLEN) { if (key.length > Native.TCP_MD5SIG_MAXKEYLEN) {
throw new IllegalArgumentException("newKeys[" + e.getKey() + throw new IllegalArgumentException("newKeys[" + e.getKey() +
"] has a key with invalid length; should not exceed the maximum length (" + "] has a key with invalid length; should not exceed the maximum length (" +

View File

@ -316,10 +316,11 @@ public class DefaultChannelConfig implements ChannelConfig {
* is of type {@link MaxMessagesRecvByteBufAllocator}. * is of type {@link MaxMessagesRecvByteBufAllocator}.
*/ */
private void setRecvByteBufAllocator(RecvByteBufAllocator allocator, ChannelMetadata metadata) { private void setRecvByteBufAllocator(RecvByteBufAllocator allocator, ChannelMetadata metadata) {
requireNonNull(allocator, "allocator");
requireNonNull(metadata, "metadata");
if (allocator instanceof MaxMessagesRecvByteBufAllocator) { if (allocator instanceof MaxMessagesRecvByteBufAllocator) {
((MaxMessagesRecvByteBufAllocator) allocator).maxMessagesPerRead(metadata.defaultMaxMessagesPerRead()); ((MaxMessagesRecvByteBufAllocator) allocator).maxMessagesPerRead(metadata.defaultMaxMessagesPerRead());
} }
requireNonNull(allocator, "allocator");
setRecvByteBufAllocator(allocator); setRecvByteBufAllocator(allocator);
} }

View File

@ -15,7 +15,7 @@
*/ */
package io.netty.channel.local; package io.netty.channel.local;
import static java.util.Objects.requireNonNull; import static io.netty.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
import io.netty.channel.Channel; import io.netty.channel.Channel;
@ -52,13 +52,8 @@ public final class LocalAddress extends SocketAddress implements Comparable<Loca
* Creates a new instance with the specified ID. * Creates a new instance with the specified ID.
*/ */
public LocalAddress(String id) { public LocalAddress(String id) {
requireNonNull(id, "id"); this.id = checkNonEmptyAfterTrim(id, "id").toLowerCase();
id = id.trim().toLowerCase(); this.strVal = "local:" + this.id;
if (id.isEmpty()) {
throw new IllegalArgumentException("empty id");
}
this.id = id;
strVal = "local:" + id;
} }
/** /**