Utilize i.n.u.internal.ObjectUtil to assert Preconditions (codec) (#11170) (#11179)

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 10:57:40 +02:00 committed by GitHub
parent 36dcaf0c50
commit acd9b383bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 21 deletions

View File

@ -15,6 +15,8 @@
*/ */
package io.netty.handler.codec.compression; package io.netty.handler.codec.compression;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -44,10 +46,7 @@ public abstract class ZlibDecoder extends ByteToMessageDecoder {
* If zero, maximum size is decided by the {@link ByteBufAllocator}. * If zero, maximum size is decided by the {@link ByteBufAllocator}.
*/ */
public ZlibDecoder(int maxAllocation) { public ZlibDecoder(int maxAllocation) {
if (maxAllocation < 0) { this.maxAllocation = checkPositiveOrZero(maxAllocation, "maxAllocation");
throw new IllegalArgumentException("maxAllocation must be >= 0");
}
this.maxAllocation = maxAllocation;
} }
/** /**

View File

@ -16,6 +16,8 @@
package io.netty.handler.codec.json; package io.netty.handler.codec.json;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil; import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -80,10 +82,7 @@ public class JsonObjectDecoder extends ByteToMessageDecoder {
* *
*/ */
public JsonObjectDecoder(int maxObjectLength, boolean streamArrayElements) { public JsonObjectDecoder(int maxObjectLength, boolean streamArrayElements) {
if (maxObjectLength < 1) { this.maxObjectLength = checkPositive(maxObjectLength, "maxObjectLength");
throw new IllegalArgumentException("maxObjectLength must be a positive int");
}
this.maxObjectLength = maxObjectLength;
this.streamArrayElements = streamArrayElements; this.streamArrayElements = streamArrayElements;
} }

View File

@ -15,6 +15,8 @@
*/ */
package io.netty.handler.codec.marshalling; package io.netty.handler.codec.marshalling;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import org.jboss.marshalling.ByteInput; import org.jboss.marshalling.ByteInput;
import java.io.IOException; import java.io.IOException;
@ -33,11 +35,8 @@ class LimitingByteInput implements ByteInput {
private long read; private long read;
LimitingByteInput(ByteInput input, long limit) { LimitingByteInput(ByteInput input, long limit) {
if (limit <= 0) {
throw new IllegalArgumentException("The limit MUST be > 0");
}
this.input = input; this.input = input;
this.limit = limit; this.limit = checkPositive(limit, "limit");
} }
@Override @Override

View File

@ -15,6 +15,8 @@
*/ */
package io.netty.handler.codec.serialization; package io.netty.handler.codec.serialization;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufOutputStream; import io.netty.buffer.ByteBufOutputStream;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -53,11 +55,7 @@ public class CompatibleObjectEncoder extends MessageToByteEncoder<Serializable>
* the long term. * the long term.
*/ */
public CompatibleObjectEncoder(int resetInterval) { public CompatibleObjectEncoder(int resetInterval) {
if (resetInterval < 0) { this.resetInterval = checkPositiveOrZero(resetInterval, "resetInterval");
throw new IllegalArgumentException(
"resetInterval: " + resetInterval);
}
this.resetInterval = resetInterval;
} }
/** /**

View File

@ -15,6 +15,8 @@
*/ */
package io.netty.handler.codec.xml; package io.netty.handler.codec.xml;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
@ -78,10 +80,7 @@ public class XmlFrameDecoder extends ByteToMessageDecoder {
private final int maxFrameLength; private final int maxFrameLength;
public XmlFrameDecoder(int maxFrameLength) { public XmlFrameDecoder(int maxFrameLength) {
if (maxFrameLength < 1) { this.maxFrameLength = checkPositive(maxFrameLength, "maxFrameLength");
throw new IllegalArgumentException("maxFrameLength must be a positive int");
}
this.maxFrameLength = maxFrameLength;
} }
@Override @Override