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
This commit is contained in:
Boris Unckel 2021-04-22 17:44:58 +02:00 committed by GitHub
parent d3fa33058d
commit 4eafccf971
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 16 deletions

View File

@ -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.verifyPadding;
import static io.netty.handler.codec.http2.Http2CodecUtil.writeFrameHeaderInternal; import static io.netty.handler.codec.http2.Http2CodecUtil.writeFrameHeaderInternal;
import static io.netty.handler.codec.http2.Http2FrameTypes.DATA; 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.max;
import static java.lang.Math.min; import static java.lang.Math.min;
@ -124,7 +125,7 @@ public class Http2FrameWriterDataBenchmark extends AbstractMicrobenchmark {
boolean needToReleaseHeaders = true; boolean needToReleaseHeaders = true;
boolean needToReleaseData = true; boolean needToReleaseData = true;
try { try {
verifyStreamId(streamId, "Stream ID"); checkPositive(streamId, "streamId");
verifyPadding(padding); verifyPadding(padding);
boolean lastFrame; boolean lastFrame;
@ -175,12 +176,6 @@ public class Http2FrameWriterDataBenchmark extends AbstractMicrobenchmark {
return promiseAggregator.doneAllocatingPromises(); 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) { private static int paddingBytes(int padding) {
// The padding parameter contains the 1 byte pad length field as well as the trailing padding bytes. // 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. // Subtract 1, so to only get the number of padding bytes that need to be appended to the end of a frame.

View File

@ -16,7 +16,8 @@
package io.netty.resolver.dns; 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.net.InetSocketAddress;
import java.util.ArrayList; import java.util.ArrayList;
@ -148,7 +149,7 @@ public abstract class DnsServerAddresses {
* Returns the {@link DnsServerAddresses} that yields only a single {@code address}. * Returns the {@link DnsServerAddresses} that yields only a single {@code address}.
*/ */
public static DnsServerAddresses singleton(final InetSocketAddress address) { public static DnsServerAddresses singleton(final InetSocketAddress address) {
ObjectUtil.checkNotNull(address, "address"); checkNotNull(address, "address");
if (address.isUnresolved()) { if (address.isUnresolved()) {
throw new IllegalArgumentException("cannot use an unresolved DNS server address: " + address); throw new IllegalArgumentException("cannot use an unresolved DNS server address: " + address);
} }
@ -157,7 +158,7 @@ public abstract class DnsServerAddresses {
} }
private static List<InetSocketAddress> sanitize(Iterable<? extends InetSocketAddress> addresses) { private static List<InetSocketAddress> sanitize(Iterable<? extends InetSocketAddress> addresses) {
ObjectUtil.checkNotNull(addresses, "addresses"); checkNotNull(addresses, "addresses");
final List<InetSocketAddress> list; final List<InetSocketAddress> list;
if (addresses instanceof Collection) { if (addresses instanceof Collection) {
@ -176,15 +177,11 @@ public abstract class DnsServerAddresses {
list.add(a); list.add(a);
} }
if (list.isEmpty()) { return checkNonEmpty(list, "list");
throw new IllegalArgumentException("empty addresses");
}
return list;
} }
private static List<InetSocketAddress> sanitize(InetSocketAddress[] addresses) { private static List<InetSocketAddress> sanitize(InetSocketAddress[] addresses) {
ObjectUtil.checkNotNull(addresses, "addresses"); checkNotNull(addresses, "addresses");
List<InetSocketAddress> list = new ArrayList<InetSocketAddress>(addresses.length); List<InetSocketAddress> list = new ArrayList<InetSocketAddress>(addresses.length);
for (InetSocketAddress a: addresses) { for (InetSocketAddress a: addresses) {