Skip to content

Commit

Permalink
Merge pull request #7855 from belugabehr/ObjectsRequireNonNull
Browse files Browse the repository at this point in the history
Standardize NPE checks on JDK Objects utility
  • Loading branch information
elharo authored Aug 29, 2021
2 parents f79f956 + 5d168fa commit a3e5587
Show file tree
Hide file tree
Showing 33 changed files with 110 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@

package com.google.protobuf;

import static com.google.protobuf.Internal.checkNotNull;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
* A partial implementation of the {@link MessageLite} interface which implements as many methods of
Expand Down Expand Up @@ -402,7 +401,7 @@ protected static <T> void addAll(final Iterable<T> values, final Collection<? su
* null.
*/
protected static <T> void addAll(final Iterable<T> values, final List<? super T> list) {
checkNotNull(values);
Objects.requireNonNull(values);
if (values instanceof LazyStringList) {
// For StringOrByteStringLists, check the underlying elements to avoid
// forcing conversions of ByteStrings to Strings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@

package com.google.protobuf;

import static com.google.protobuf.Internal.checkNotNull;

import java.nio.ByteBuffer;
import java.util.Objects;

/**
* A buffer that was allocated by a {@link BufferAllocator}. For every buffer, it is guaranteed that
Expand Down Expand Up @@ -151,7 +150,7 @@ public static AllocatedBuffer wrap(final byte[] bytes, final int offset, final i
* returned buffer will have {@link #hasNioBuffer} == {@code true}.
*/
public static AllocatedBuffer wrap(final ByteBuffer buffer) {
checkNotNull(buffer, "buffer");
Objects.requireNonNull(buffer, "buffer");

return new AllocatedBuffer() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import com.google.protobuf.Internal.ProtobufList;
import java.io.IOException;
import java.util.Objects;

/**
* Helper functions to decode protobuf wire format from a byte array.
Expand Down Expand Up @@ -64,10 +65,7 @@ static final class Registers {
}

Registers(ExtensionRegistryLite extensionRegistry) {
if (extensionRegistry == null) {
throw new NullPointerException();
}
this.extensionRegistry = extensionRegistry;
this.extensionRegistry = Objects.requireNonNull(extensionRegistry);
}
}

Expand Down
4 changes: 2 additions & 2 deletions java/core/src/main/java/com/google/protobuf/BinaryWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

package com.google.protobuf;

import static com.google.protobuf.Internal.checkNotNull;
import static com.google.protobuf.WireFormat.FIXED32_SIZE;
import static com.google.protobuf.WireFormat.FIXED64_SIZE;
import static com.google.protobuf.WireFormat.MAX_VARINT32_SIZE;
Expand All @@ -51,6 +50,7 @@
import java.util.ArrayDeque;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;

/**
Expand Down Expand Up @@ -146,7 +146,7 @@ private BinaryWriter(BufferAllocator alloc, int chunkSize) {
if (chunkSize <= 0) {
throw new IllegalArgumentException("chunkSize must be > 0");
}
this.alloc = checkNotNull(alloc, "alloc");
this.alloc = Objects.requireNonNull(alloc, "alloc");
this.chunkSize = chunkSize;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@

package com.google.protobuf;

import static com.google.protobuf.Internal.checkNotNull;

import com.google.protobuf.Internal.BooleanList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.RandomAccess;

/**
Expand Down Expand Up @@ -238,7 +237,7 @@ private void addBoolean(int index, boolean element) {
public boolean addAll(Collection<? extends Boolean> collection) {
ensureIsMutable();

checkNotNull(collection);
Objects.requireNonNull(collection);

// We specialize when adding another BooleanArrayList to avoid boxing elements.
if (!(collection instanceof BooleanArrayList)) {
Expand Down
6 changes: 2 additions & 4 deletions java/core/src/main/java/com/google/protobuf/ByteString.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.List;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Objects;

/**
* Immutable sequence of bytes. Provides conversions to and from {@code byte[]}, {@link
Expand Down Expand Up @@ -1313,10 +1314,7 @@ private static class LiteralByteString extends ByteString.LeafByteString {
* @param bytes array to wrap
*/
LiteralByteString(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException();
}
this.bytes = bytes;
this.bytes = Objects.requireNonNull(bytes);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import static com.google.protobuf.Internal.EMPTY_BYTE_ARRAY;
import static com.google.protobuf.Internal.EMPTY_BYTE_BUFFER;
import static com.google.protobuf.Internal.UTF_8;
import static com.google.protobuf.Internal.checkNotNull;
import static com.google.protobuf.WireFormat.FIXED32_SIZE;
import static com.google.protobuf.WireFormat.FIXED64_SIZE;
import static com.google.protobuf.WireFormat.MAX_VARINT_SIZE;
Expand All @@ -47,6 +46,7 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
* Reads and decodes protocol message fields.
Expand Down Expand Up @@ -2056,8 +2056,7 @@ private static final class StreamDecoder extends CodedInputStream {
private int currentLimit = Integer.MAX_VALUE;

private StreamDecoder(final InputStream input, int bufferSize) {
checkNotNull(input, "input");
this.input = input;
this.input = Objects.requireNonNull(input, "input");
this.buffer = new byte[bufferSize];
this.bufferSize = 0;
pos = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/** An adapter between the {@link Reader} interface and {@link CodedInputStream}. */
@ExperimentalApi
Expand All @@ -63,7 +64,7 @@ public static CodedInputStreamReader forCodedInput(CodedInputStream input) {
}

private CodedInputStreamReader(CodedInputStream input) {
this.input = Internal.checkNotNull(input, "input");
this.input = Objects.requireNonNull(input, "input");
this.input.wrapper = this;
}

Expand Down
22 changes: 6 additions & 16 deletions java/core/src/main/java/com/google/protobuf/CodedOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -1154,9 +1155,7 @@ private static class ArrayEncoder extends CodedOutputStream {
private int position;

ArrayEncoder(byte[] buffer, int offset, int length) {
if (buffer == null) {
throw new NullPointerException("buffer");
}
Objects.requireNonNull(buffer, "buffer");
if ((offset | length | (buffer.length - (offset + length))) < 0) {
throw new IllegalArgumentException(
String.format(
Expand Down Expand Up @@ -2120,14 +2119,11 @@ public void writeFixed64NoTag(long value) throws IOException {

@Override
public void write(byte[] value, int offset, int length) throws IOException {
if (value == null
|| offset < 0
Objects.requireNonNull(value, "value");
if (offset < 0
|| length < 0
|| (value.length - length) < offset
|| (limit - length) < position) {
if (value == null) {
throw new NullPointerException("value");
}
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, length));
}
Expand Down Expand Up @@ -2396,10 +2392,7 @@ private static final class ByteOutputEncoder extends AbstractBufferedEncoder {

ByteOutputEncoder(ByteOutput out, int bufferSize) {
super(bufferSize);
if (out == null) {
throw new NullPointerException("out");
}
this.out = out;
this.out = Objects.requireNonNull(out, "out");
}

@Override
Expand Down Expand Up @@ -2710,10 +2703,7 @@ private static final class OutputStreamEncoder extends AbstractBufferedEncoder {

OutputStreamEncoder(OutputStream out, int bufferSize) {
super(bufferSize);
if (out == null) {
throw new NullPointerException("out");
}
this.out = out;
this.out = Objects.requireNonNull(out, "out");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@

package com.google.protobuf;

import static com.google.protobuf.Internal.checkNotNull;
import static com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/** An adapter between the {@link Writer} interface and {@link CodedOutputStream}. */
@ExperimentalApi
Expand All @@ -51,7 +51,7 @@ public static CodedOutputStreamWriter forCodedOutput(CodedOutputStream output) {
}

private CodedOutputStreamWriter(CodedOutputStream output) {
this.output = checkNotNull(output, "output");
this.output = Objects.requireNonNull(output, "output");
this.output.wrapper = this;
}

Expand Down
5 changes: 2 additions & 3 deletions java/core/src/main/java/com/google/protobuf/Descriptors.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

package com.google.protobuf;

import static com.google.protobuf.Internal.checkNotNull;

import com.google.protobuf.DescriptorProtos.DescriptorProto;
import com.google.protobuf.DescriptorProtos.EnumDescriptorProto;
import com.google.protobuf.DescriptorProtos.EnumOptions;
Expand All @@ -57,6 +55,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.logging.Logger;
Expand Down Expand Up @@ -744,7 +743,7 @@ public boolean isReservedNumber(final int number) {

/** Determines if the given field name is reserved. */
public boolean isReservedName(final String name) {
checkNotNull(name);
Objects.requireNonNull(name);
for (final String reservedName : proto.getReservedNameList()) {
if (reservedName.equals(name)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@

package com.google.protobuf;

import static com.google.protobuf.Internal.checkNotNull;

import com.google.protobuf.Internal.DoubleList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.RandomAccess;

/**
Expand Down Expand Up @@ -238,7 +237,7 @@ private void addDouble(int index, double element) {
public boolean addAll(Collection<? extends Double> collection) {
ensureIsMutable();

checkNotNull(collection);
Objects.requireNonNull(collection);

// We specialize when adding another DoubleArrayList to avoid boxing elements.
if (!(collection instanceof DoubleArrayList)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

package com.google.protobuf;

import static com.google.protobuf.Internal.checkNotNull;

import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
Expand All @@ -41,6 +39,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* An implementation of {@link Message} that can represent arbitrary types, given a {@link
Expand Down Expand Up @@ -624,7 +623,7 @@ private void verifyOneofContainingType(OneofDescriptor oneof) {

/** Verifies that the value is EnumValueDescriptor and matches Enum Type. */
private void ensureSingularEnumValueDescriptor(FieldDescriptor field, Object value) {
checkNotNull(value);
Objects.requireNonNull(value);
if (!(value instanceof EnumValueDescriptor)) {
throw new IllegalArgumentException(
"DynamicMessage should use EnumValueDescriptor to set Enum Value.");
Expand Down
Loading

0 comments on commit a3e5587

Please sign in to comment.