From 4eafccf9714a331c1964acc73cf2d7db48af62fa Mon Sep 17 00:00:00 2001 From: Boris Unckel Date: Thu, 22 Apr 2021 17:44:58 +0200 Subject: [PATCH] Utilize i.n.u.internal.ObjectUtil to assert Preconditions (misc) (#11170) (#11186) 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 in microbench and resolver-dns Fixes #11170 --- .../http2/Http2FrameWriterDataBenchmark.java | 9 ++------- .../io/netty/resolver/dns/DnsServerAddresses.java | 15 ++++++--------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/microbench/src/main/java/io/netty/handler/codec/http2/Http2FrameWriterDataBenchmark.java b/microbench/src/main/java/io/netty/handler/codec/http2/Http2FrameWriterDataBenchmark.java index bfc6e639d9..8d5147b230 100644 --- a/microbench/src/main/java/io/netty/handler/codec/http2/Http2FrameWriterDataBenchmark.java +++ b/microbench/src/main/java/io/netty/handler/codec/http2/Http2FrameWriterDataBenchmark.java @@ -48,6 +48,7 @@ import static io.netty.handler.codec.http2.Http2CodecUtil.MAX_UNSIGNED_BYTE; import static io.netty.handler.codec.http2.Http2CodecUtil.verifyPadding; import static io.netty.handler.codec.http2.Http2CodecUtil.writeFrameHeaderInternal; import static io.netty.handler.codec.http2.Http2FrameTypes.DATA; +import static io.netty.util.internal.ObjectUtil.checkPositive; import static java.lang.Math.max; import static java.lang.Math.min; @@ -124,7 +125,7 @@ public class Http2FrameWriterDataBenchmark extends AbstractMicrobenchmark { boolean needToReleaseHeaders = true; boolean needToReleaseData = true; try { - verifyStreamId(streamId, "Stream ID"); + checkPositive(streamId, "streamId"); verifyPadding(padding); boolean lastFrame; @@ -175,12 +176,6 @@ public class Http2FrameWriterDataBenchmark extends AbstractMicrobenchmark { return promiseAggregator.doneAllocatingPromises(); } - private static void verifyStreamId(int streamId, String argumentName) { - if (streamId <= 0) { - throw new IllegalArgumentException(argumentName + " must be > 0"); - } - } - private static int paddingBytes(int padding) { // The padding parameter contains the 1 byte pad length field as well as the trailing padding bytes. // Subtract 1, so to only get the number of padding bytes that need to be appended to the end of a frame. diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsServerAddresses.java b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsServerAddresses.java index 11705d7670..9cc9c0b46b 100644 --- a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsServerAddresses.java +++ b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsServerAddresses.java @@ -16,7 +16,8 @@ package io.netty.resolver.dns; -import io.netty.util.internal.ObjectUtil; +import static io.netty.util.internal.ObjectUtil.checkNotNull; +import static io.netty.util.internal.ObjectUtil.checkNonEmpty; import java.net.InetSocketAddress; import java.util.ArrayList; @@ -148,7 +149,7 @@ public abstract class DnsServerAddresses { * Returns the {@link DnsServerAddresses} that yields only a single {@code address}. */ public static DnsServerAddresses singleton(final InetSocketAddress address) { - ObjectUtil.checkNotNull(address, "address"); + checkNotNull(address, "address"); if (address.isUnresolved()) { throw new IllegalArgumentException("cannot use an unresolved DNS server address: " + address); } @@ -157,7 +158,7 @@ public abstract class DnsServerAddresses { } private static List sanitize(Iterable addresses) { - ObjectUtil.checkNotNull(addresses, "addresses"); + checkNotNull(addresses, "addresses"); final List list; if (addresses instanceof Collection) { @@ -176,15 +177,11 @@ public abstract class DnsServerAddresses { list.add(a); } - if (list.isEmpty()) { - throw new IllegalArgumentException("empty addresses"); - } - - return list; + return checkNonEmpty(list, "list"); } private static List sanitize(InetSocketAddress[] addresses) { - ObjectUtil.checkNotNull(addresses, "addresses"); + checkNotNull(addresses, "addresses"); List list = new ArrayList(addresses.length); for (InetSocketAddress a: addresses) {