Utilize i.n.u.internal.ObjectUtil to assert Preconditions (example) (#11170) (#11183)

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 15:03:13 +02:00 committed by GitHub
parent d5fabe78a7
commit 52ca64033a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 42 deletions

View File

@ -16,6 +16,8 @@
package io.netty.example.http2.helloworld.frame.server; package io.netty.example.http2.helloworld.frame.server;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
@ -62,11 +64,8 @@ public class Http2ServerInitializer extends ChannelInitializer<SocketChannel> {
} }
public Http2ServerInitializer(SslContext sslCtx, int maxHttpContentLength) { public Http2ServerInitializer(SslContext sslCtx, int maxHttpContentLength) {
if (maxHttpContentLength < 0) {
throw new IllegalArgumentException("maxHttpContentLength (expected >= 0): " + maxHttpContentLength);
}
this.sslCtx = sslCtx; this.sslCtx = sslCtx;
this.maxHttpContentLength = maxHttpContentLength; this.maxHttpContentLength = checkPositiveOrZero(maxHttpContentLength, "maxHttpContentLength");
} }
@Override @Override

View File

@ -16,6 +16,8 @@
package io.netty.example.http2.helloworld.multiplex.server; package io.netty.example.http2.helloworld.multiplex.server;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
@ -64,11 +66,8 @@ public class Http2ServerInitializer extends ChannelInitializer<SocketChannel> {
} }
public Http2ServerInitializer(SslContext sslCtx, int maxHttpContentLength) { public Http2ServerInitializer(SslContext sslCtx, int maxHttpContentLength) {
if (maxHttpContentLength < 0) {
throw new IllegalArgumentException("maxHttpContentLength (expected >= 0): " + maxHttpContentLength);
}
this.sslCtx = sslCtx; this.sslCtx = sslCtx;
this.maxHttpContentLength = maxHttpContentLength; this.maxHttpContentLength = checkPositiveOrZero(maxHttpContentLength, "maxHttpContentLength");
} }
@Override @Override

View File

@ -16,6 +16,8 @@
package io.netty.example.http2.helloworld.server; package io.netty.example.http2.helloworld.server;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
@ -60,11 +62,8 @@ public class Http2ServerInitializer extends ChannelInitializer<SocketChannel> {
} }
public Http2ServerInitializer(SslContext sslCtx, int maxHttpContentLength) { public Http2ServerInitializer(SslContext sslCtx, int maxHttpContentLength) {
if (maxHttpContentLength < 0) {
throw new IllegalArgumentException("maxHttpContentLength (expected >= 0): " + maxHttpContentLength);
}
this.sslCtx = sslCtx; this.sslCtx = sslCtx;
this.maxHttpContentLength = maxHttpContentLength; this.maxHttpContentLength = checkPositiveOrZero(maxHttpContentLength, "maxHttpContentLength");
} }
@Override @Override

View File

@ -19,6 +19,8 @@
package io.netty.example.worldclock; package io.netty.example.worldclock;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
@SuppressWarnings("all") @SuppressWarnings("all")
public final class WorldClockProtocol { public final class WorldClockProtocol {
private WorldClockProtocol() {} private WorldClockProtocol() {}
@ -822,9 +824,7 @@ public final class WorldClockProtocol {
* <code>required .io.netty.example.worldclock.Continent continent = 1;</code> * <code>required .io.netty.example.worldclock.Continent continent = 1;</code>
*/ */
public Builder setContinent(io.netty.example.worldclock.WorldClockProtocol.Continent value) { public Builder setContinent(io.netty.example.worldclock.WorldClockProtocol.Continent value) {
if (value == null) { checkNotNull(value, "value");
throw new NullPointerException();
}
bitField0_ |= 0x00000001; bitField0_ |= 0x00000001;
continent_ = value; continent_ = value;
onChanged(); onChanged();
@ -886,9 +886,7 @@ public final class WorldClockProtocol {
*/ */
public Builder setCity( public Builder setCity(
java.lang.String value) { java.lang.String value) {
if (value == null) { checkNotNull(value, "value");
throw new NullPointerException();
}
bitField0_ |= 0x00000002; bitField0_ |= 0x00000002;
city_ = value; city_ = value;
onChanged(); onChanged();
@ -908,9 +906,7 @@ public final class WorldClockProtocol {
*/ */
public Builder setCityBytes( public Builder setCityBytes(
com.google.protobuf.ByteString value) { com.google.protobuf.ByteString value) {
if (value == null) { checkNotNull(value, "value");
throw new NullPointerException();
}
bitField0_ |= 0x00000002; bitField0_ |= 0x00000002;
city_ = value; city_ = value;
onChanged(); onChanged();
@ -1438,9 +1434,7 @@ public final class WorldClockProtocol {
public Builder setLocation( public Builder setLocation(
int index, io.netty.example.worldclock.WorldClockProtocol.Location value) { int index, io.netty.example.worldclock.WorldClockProtocol.Location value) {
if (locationBuilder_ == null) { if (locationBuilder_ == null) {
if (value == null) { checkNotNull(value, "value");
throw new NullPointerException();
}
ensureLocationIsMutable(); ensureLocationIsMutable();
location_.set(index, value); location_.set(index, value);
onChanged(); onChanged();
@ -1468,9 +1462,7 @@ public final class WorldClockProtocol {
*/ */
public Builder addLocation(io.netty.example.worldclock.WorldClockProtocol.Location value) { public Builder addLocation(io.netty.example.worldclock.WorldClockProtocol.Location value) {
if (locationBuilder_ == null) { if (locationBuilder_ == null) {
if (value == null) { checkNotNull(value, "value");
throw new NullPointerException();
}
ensureLocationIsMutable(); ensureLocationIsMutable();
location_.add(value); location_.add(value);
onChanged(); onChanged();
@ -1485,9 +1477,7 @@ public final class WorldClockProtocol {
public Builder addLocation( public Builder addLocation(
int index, io.netty.example.worldclock.WorldClockProtocol.Location value) { int index, io.netty.example.worldclock.WorldClockProtocol.Location value) {
if (locationBuilder_ == null) { if (locationBuilder_ == null) {
if (value == null) { checkNotNull(value, "value");
throw new NullPointerException();
}
ensureLocationIsMutable(); ensureLocationIsMutable();
location_.add(index, value); location_.add(index, value);
onChanged(); onChanged();
@ -2501,9 +2491,7 @@ public final class WorldClockProtocol {
* <code>required .io.netty.example.worldclock.DayOfWeek dayOfWeek = 5;</code> * <code>required .io.netty.example.worldclock.DayOfWeek dayOfWeek = 5;</code>
*/ */
public Builder setDayOfWeek(io.netty.example.worldclock.WorldClockProtocol.DayOfWeek value) { public Builder setDayOfWeek(io.netty.example.worldclock.WorldClockProtocol.DayOfWeek value) {
if (value == null) { checkNotNull(value, "value");
throw new NullPointerException();
}
bitField0_ |= 0x00000008; bitField0_ |= 0x00000008;
dayOfWeek_ = value; dayOfWeek_ = value;
onChanged(); onChanged();
@ -3145,9 +3133,7 @@ public final class WorldClockProtocol {
public Builder setLocalTime( public Builder setLocalTime(
int index, io.netty.example.worldclock.WorldClockProtocol.LocalTime value) { int index, io.netty.example.worldclock.WorldClockProtocol.LocalTime value) {
if (localTimeBuilder_ == null) { if (localTimeBuilder_ == null) {
if (value == null) { checkNotNull(value, "value");
throw new NullPointerException();
}
ensureLocalTimeIsMutable(); ensureLocalTimeIsMutable();
localTime_.set(index, value); localTime_.set(index, value);
onChanged(); onChanged();
@ -3175,9 +3161,7 @@ public final class WorldClockProtocol {
*/ */
public Builder addLocalTime(io.netty.example.worldclock.WorldClockProtocol.LocalTime value) { public Builder addLocalTime(io.netty.example.worldclock.WorldClockProtocol.LocalTime value) {
if (localTimeBuilder_ == null) { if (localTimeBuilder_ == null) {
if (value == null) { checkNotNull(value, "value");
throw new NullPointerException();
}
ensureLocalTimeIsMutable(); ensureLocalTimeIsMutable();
localTime_.add(value); localTime_.add(value);
onChanged(); onChanged();
@ -3192,9 +3176,7 @@ public final class WorldClockProtocol {
public Builder addLocalTime( public Builder addLocalTime(
int index, io.netty.example.worldclock.WorldClockProtocol.LocalTime value) { int index, io.netty.example.worldclock.WorldClockProtocol.LocalTime value) {
if (localTimeBuilder_ == null) { if (localTimeBuilder_ == null) {
if (value == null) { checkNotNull(value, "value");
throw new NullPointerException();
}
ensureLocalTimeIsMutable(); ensureLocalTimeIsMutable();
localTime_.add(index, value); localTime_.add(index, value);
onChanged(); onChanged();