From acd9b383bc0a4597c81aea7fd52190c5dbfe5a00 Mon Sep 17 00:00:00 2001 From: Boris Unckel Date: Thu, 22 Apr 2021 10:57:40 +0200 Subject: [PATCH] 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 --- .../io/netty/handler/codec/compression/ZlibDecoder.java | 7 +++---- .../io/netty/handler/codec/json/JsonObjectDecoder.java | 7 +++---- .../handler/codec/marshalling/LimitingByteInput.java | 7 +++---- .../codec/serialization/CompatibleObjectEncoder.java | 8 +++----- .../java/io/netty/handler/codec/xml/XmlFrameDecoder.java | 7 +++---- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/codec/src/main/java/io/netty/handler/codec/compression/ZlibDecoder.java b/codec/src/main/java/io/netty/handler/codec/compression/ZlibDecoder.java index 7a1edfe2da..bc05424e0a 100644 --- a/codec/src/main/java/io/netty/handler/codec/compression/ZlibDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/compression/ZlibDecoder.java @@ -15,6 +15,8 @@ */ package io.netty.handler.codec.compression; +import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero; + import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; 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}. */ public ZlibDecoder(int maxAllocation) { - if (maxAllocation < 0) { - throw new IllegalArgumentException("maxAllocation must be >= 0"); - } - this.maxAllocation = maxAllocation; + this.maxAllocation = checkPositiveOrZero(maxAllocation, "maxAllocation"); } /** diff --git a/codec/src/main/java/io/netty/handler/codec/json/JsonObjectDecoder.java b/codec/src/main/java/io/netty/handler/codec/json/JsonObjectDecoder.java index f8a94c5b01..a1b8c3e7a2 100644 --- a/codec/src/main/java/io/netty/handler/codec/json/JsonObjectDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/json/JsonObjectDecoder.java @@ -16,6 +16,8 @@ package io.netty.handler.codec.json; +import static io.netty.util.internal.ObjectUtil.checkPositive; + import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; import io.netty.channel.ChannelHandlerContext; @@ -80,10 +82,7 @@ public class JsonObjectDecoder extends ByteToMessageDecoder { * */ public JsonObjectDecoder(int maxObjectLength, boolean streamArrayElements) { - if (maxObjectLength < 1) { - throw new IllegalArgumentException("maxObjectLength must be a positive int"); - } - this.maxObjectLength = maxObjectLength; + this.maxObjectLength = checkPositive(maxObjectLength, "maxObjectLength"); this.streamArrayElements = streamArrayElements; } diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/LimitingByteInput.java b/codec/src/main/java/io/netty/handler/codec/marshalling/LimitingByteInput.java index 60f1698751..46a3236e73 100644 --- a/codec/src/main/java/io/netty/handler/codec/marshalling/LimitingByteInput.java +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/LimitingByteInput.java @@ -15,6 +15,8 @@ */ package io.netty.handler.codec.marshalling; +import static io.netty.util.internal.ObjectUtil.checkPositive; + import org.jboss.marshalling.ByteInput; import java.io.IOException; @@ -33,11 +35,8 @@ class LimitingByteInput implements ByteInput { private long read; LimitingByteInput(ByteInput input, long limit) { - if (limit <= 0) { - throw new IllegalArgumentException("The limit MUST be > 0"); - } this.input = input; - this.limit = limit; + this.limit = checkPositive(limit, "limit"); } @Override diff --git a/codec/src/main/java/io/netty/handler/codec/serialization/CompatibleObjectEncoder.java b/codec/src/main/java/io/netty/handler/codec/serialization/CompatibleObjectEncoder.java index c5a5aaddaf..1324a9321b 100644 --- a/codec/src/main/java/io/netty/handler/codec/serialization/CompatibleObjectEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/serialization/CompatibleObjectEncoder.java @@ -15,6 +15,8 @@ */ package io.netty.handler.codec.serialization; +import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero; + import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufOutputStream; import io.netty.channel.ChannelHandlerContext; @@ -53,11 +55,7 @@ public class CompatibleObjectEncoder extends MessageToByteEncoder * the long term. */ public CompatibleObjectEncoder(int resetInterval) { - if (resetInterval < 0) { - throw new IllegalArgumentException( - "resetInterval: " + resetInterval); - } - this.resetInterval = resetInterval; + this.resetInterval = checkPositiveOrZero(resetInterval, "resetInterval"); } /** diff --git a/codec/src/main/java/io/netty/handler/codec/xml/XmlFrameDecoder.java b/codec/src/main/java/io/netty/handler/codec/xml/XmlFrameDecoder.java index 0e6f76de69..ed169a6622 100644 --- a/codec/src/main/java/io/netty/handler/codec/xml/XmlFrameDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/xml/XmlFrameDecoder.java @@ -15,6 +15,8 @@ */ package io.netty.handler.codec.xml; +import static io.netty.util.internal.ObjectUtil.checkPositive; + import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; @@ -78,10 +80,7 @@ public class XmlFrameDecoder extends ByteToMessageDecoder { private final int maxFrameLength; public XmlFrameDecoder(int maxFrameLength) { - if (maxFrameLength < 1) { - throw new IllegalArgumentException("maxFrameLength must be a positive int"); - } - this.maxFrameLength = maxFrameLength; + this.maxFrameLength = checkPositive(maxFrameLength, "maxFrameLength"); } @Override