From 5f55fe1d6b2ee4b8d54d83eb49279089e5b67b9f Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Mon, 13 Sep 2021 19:49:00 +0000 Subject: [PATCH] enforce android API level 14 (#8971) * enforce android API level 14 Add animal-sniffer plugin to check for android compatibility * Revert "Merge pull request #7855 from belugabehr/ObjectsRequireNonNull" This reverts commit a3e55874472403870275778ade0b91cce6333633, reversing changes made to f79f956b7459ba15aa0ad91d6f09bd7931481593. --- .../google/protobuf/AbstractMessageLite.java | 5 ++- .../com/google/protobuf/AllocatedBuffer.java | 5 ++- .../com/google/protobuf/ArrayDecoders.java | 6 ++- .../com/google/protobuf/BinaryWriter.java | 4 +- .../com/google/protobuf/BooleanArrayList.java | 5 ++- .../java/com/google/protobuf/ByteString.java | 6 ++- .../com/google/protobuf/CodedInputStream.java | 5 ++- .../protobuf/CodedInputStreamReader.java | 3 +- .../google/protobuf/CodedOutputStream.java | 22 ++++++--- .../protobuf/CodedOutputStreamWriter.java | 4 +- .../java/com/google/protobuf/Descriptors.java | 5 ++- .../com/google/protobuf/DoubleArrayList.java | 5 ++- .../com/google/protobuf/DynamicMessage.java | 5 ++- .../java/com/google/protobuf/FieldInfo.java | 45 ++++++++++--------- .../java/com/google/protobuf/FieldSet.java | 5 ++- .../com/google/protobuf/FloatArrayList.java | 5 ++- .../google/protobuf/GeneratedMessageV3.java | 2 + .../com/google/protobuf/IntArrayList.java | 5 ++- .../java/com/google/protobuf/Internal.java | 16 +++++++ .../com/google/protobuf/LazyFieldLite.java | 20 ++++++--- .../com/google/protobuf/LongArrayList.java | 5 ++- .../protobuf/ManifestSchemaFactory.java | 4 +- .../java/com/google/protobuf/MapField.java | 11 ++--- .../com/google/protobuf/MapFieldLite.java | 11 ++--- .../com/google/protobuf/MessageSchema.java | 9 ++-- .../com/google/protobuf/NioByteString.java | 5 ++- .../java/com/google/protobuf/Protobuf.java | 13 +++--- .../google/protobuf/RepeatedFieldBuilder.java | 11 ++--- .../protobuf/RepeatedFieldBuilderV3.java | 11 ++--- .../com/google/protobuf/RopeByteString.java | 6 +-- .../google/protobuf/SingleFieldBuilder.java | 6 +-- .../google/protobuf/SingleFieldBuilderV3.java | 6 +-- .../protobuf/StructuralMessageInfo.java | 7 +-- java/pom.xml | 21 +++++++++ 34 files changed, 194 insertions(+), 110 deletions(-) diff --git a/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java b/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java index 62a096f4e5..4e3cf42718 100644 --- a/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java +++ b/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java @@ -30,6 +30,8 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; @@ -37,7 +39,6 @@ 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 @@ -401,7 +402,7 @@ public abstract class AbstractMessageLite< * null. */ protected static void addAll(final Iterable values, final List list) { - Objects.requireNonNull(values); + checkNotNull(values); if (values instanceof LazyStringList) { // For StringOrByteStringLists, check the underlying elements to avoid // forcing conversions of ByteStrings to Strings. diff --git a/java/core/src/main/java/com/google/protobuf/AllocatedBuffer.java b/java/core/src/main/java/com/google/protobuf/AllocatedBuffer.java index 78adecaf65..a01a6c1a8b 100644 --- a/java/core/src/main/java/com/google/protobuf/AllocatedBuffer.java +++ b/java/core/src/main/java/com/google/protobuf/AllocatedBuffer.java @@ -30,8 +30,9 @@ 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 @@ -150,7 +151,7 @@ abstract class AllocatedBuffer { * returned buffer will have {@link #hasNioBuffer} == {@code true}. */ public static AllocatedBuffer wrap(final ByteBuffer buffer) { - Objects.requireNonNull(buffer, "buffer"); + checkNotNull(buffer, "buffer"); return new AllocatedBuffer() { diff --git a/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java b/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java index 041174e356..1217e112e0 100644 --- a/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java +++ b/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java @@ -34,7 +34,6 @@ import static com.google.protobuf.MessageSchema.getMutableUnknownFields; 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. @@ -65,7 +64,10 @@ final class ArrayDecoders { } Registers(ExtensionRegistryLite extensionRegistry) { - this.extensionRegistry = Objects.requireNonNull(extensionRegistry); + if (extensionRegistry == null) { + throw new NullPointerException(); + } + this.extensionRegistry = extensionRegistry; } } diff --git a/java/core/src/main/java/com/google/protobuf/BinaryWriter.java b/java/core/src/main/java/com/google/protobuf/BinaryWriter.java index 39661995ec..94259ecd32 100644 --- a/java/core/src/main/java/com/google/protobuf/BinaryWriter.java +++ b/java/core/src/main/java/com/google/protobuf/BinaryWriter.java @@ -30,6 +30,7 @@ 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; @@ -50,7 +51,6 @@ import java.nio.ByteOrder; import java.util.ArrayDeque; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Queue; /** @@ -146,7 +146,7 @@ abstract class BinaryWriter extends ByteOutput implements Writer { if (chunkSize <= 0) { throw new IllegalArgumentException("chunkSize must be > 0"); } - this.alloc = Objects.requireNonNull(alloc, "alloc"); + this.alloc = checkNotNull(alloc, "alloc"); this.chunkSize = chunkSize; } diff --git a/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java b/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java index b9adb97e59..451fce1e84 100644 --- a/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java @@ -30,10 +30,11 @@ 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; /** @@ -237,7 +238,7 @@ final class BooleanArrayList extends AbstractProtobufList public boolean addAll(Collection collection) { ensureIsMutable(); - Objects.requireNonNull(collection); + checkNotNull(collection); // We specialize when adding another BooleanArrayList to avoid boxing elements. if (!(collection instanceof BooleanArrayList)) { diff --git a/java/core/src/main/java/com/google/protobuf/ByteString.java b/java/core/src/main/java/com/google/protobuf/ByteString.java index e698f681db..68e30448cc 100644 --- a/java/core/src/main/java/com/google/protobuf/ByteString.java +++ b/java/core/src/main/java/com/google/protobuf/ByteString.java @@ -55,7 +55,6 @@ import java.util.Iterator; 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 @@ -1329,7 +1328,10 @@ public abstract class ByteString implements Iterable, Serializable { * @param bytes array to wrap */ LiteralByteString(byte[] bytes) { - this.bytes = Objects.requireNonNull(bytes); + if (bytes == null) { + throw new NullPointerException(); + } + this.bytes = bytes; } @Override diff --git a/java/core/src/main/java/com/google/protobuf/CodedInputStream.java b/java/core/src/main/java/com/google/protobuf/CodedInputStream.java index cd639686c2..0b935cb5a9 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedInputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedInputStream.java @@ -33,6 +33,7 @@ package com.google.protobuf; 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; @@ -46,7 +47,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; -import java.util.Objects; /** * Reads and decodes protocol message fields. @@ -2056,7 +2056,8 @@ public abstract class CodedInputStream { private int currentLimit = Integer.MAX_VALUE; private StreamDecoder(final InputStream input, int bufferSize) { - this.input = Objects.requireNonNull(input, "input"); + checkNotNull(input, "input"); + this.input = input; this.buffer = new byte[bufferSize]; this.bufferSize = 0; pos = 0; diff --git a/java/core/src/main/java/com/google/protobuf/CodedInputStreamReader.java b/java/core/src/main/java/com/google/protobuf/CodedInputStreamReader.java index c72c6a5e82..7658f629d3 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedInputStreamReader.java +++ b/java/core/src/main/java/com/google/protobuf/CodedInputStreamReader.java @@ -42,7 +42,6 @@ import static com.google.protobuf.WireFormat.WIRETYPE_VARINT; 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 @@ -64,7 +63,7 @@ final class CodedInputStreamReader implements Reader { } private CodedInputStreamReader(CodedInputStream input) { - this.input = Objects.requireNonNull(input, "input"); + this.input = Internal.checkNotNull(input, "input"); this.input.wrapper = this; } diff --git a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java index e79543ac55..12f2097a8f 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -42,7 +42,6 @@ import java.io.OutputStream; 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; @@ -1155,7 +1154,9 @@ public abstract class CodedOutputStream extends ByteOutput { private int position; ArrayEncoder(byte[] buffer, int offset, int length) { - Objects.requireNonNull(buffer, "buffer"); + if (buffer == null) { + throw new NullPointerException("buffer"); + } if ((offset | length | (buffer.length - (offset + length))) < 0) { throw new IllegalArgumentException( String.format( @@ -2119,11 +2120,14 @@ public abstract class CodedOutputStream extends ByteOutput { @Override public void write(byte[] value, int offset, int length) throws IOException { - Objects.requireNonNull(value, "value"); - if (offset < 0 + if (value == null + || 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)); } @@ -2392,7 +2396,10 @@ public abstract class CodedOutputStream extends ByteOutput { ByteOutputEncoder(ByteOutput out, int bufferSize) { super(bufferSize); - this.out = Objects.requireNonNull(out, "out"); + if (out == null) { + throw new NullPointerException("out"); + } + this.out = out; } @Override @@ -2703,7 +2710,10 @@ public abstract class CodedOutputStream extends ByteOutput { OutputStreamEncoder(OutputStream out, int bufferSize) { super(bufferSize); - this.out = Objects.requireNonNull(out, "out"); + if (out == null) { + throw new NullPointerException("out"); + } + this.out = out; } @Override diff --git a/java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java b/java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java index 2a4b0c4512..0d1983cb07 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java @@ -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 @@ -51,7 +51,7 @@ final class CodedOutputStreamWriter implements Writer { } private CodedOutputStreamWriter(CodedOutputStream output) { - this.output = Objects.requireNonNull(output, "output"); + this.output = checkNotNull(output, "output"); this.output.wrapper = this; } diff --git a/java/core/src/main/java/com/google/protobuf/Descriptors.java b/java/core/src/main/java/com/google/protobuf/Descriptors.java index f295739a98..7b1458426b 100644 --- a/java/core/src/main/java/com/google/protobuf/Descriptors.java +++ b/java/core/src/main/java/com/google/protobuf/Descriptors.java @@ -30,6 +30,8 @@ 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; @@ -55,7 +57,6 @@ import java.util.HashMap; 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; @@ -743,7 +744,7 @@ public final class Descriptors { /** Determines if the given field name is reserved. */ public boolean isReservedName(final String name) { - Objects.requireNonNull(name); + checkNotNull(name); for (final String reservedName : proto.getReservedNameList()) { if (reservedName.equals(name)) { return true; diff --git a/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java b/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java index d49e80ee4f..4085653437 100644 --- a/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java @@ -30,10 +30,11 @@ 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; /** @@ -237,7 +238,7 @@ final class DoubleArrayList extends AbstractProtobufList public boolean addAll(Collection collection) { ensureIsMutable(); - Objects.requireNonNull(collection); + checkNotNull(collection); // We specialize when adding another DoubleArrayList to avoid boxing elements. if (!(collection instanceof DoubleArrayList)) { diff --git a/java/core/src/main/java/com/google/protobuf/DynamicMessage.java b/java/core/src/main/java/com/google/protobuf/DynamicMessage.java index be89cfb04f..8beebba24d 100644 --- a/java/core/src/main/java/com/google/protobuf/DynamicMessage.java +++ b/java/core/src/main/java/com/google/protobuf/DynamicMessage.java @@ -30,6 +30,8 @@ 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; @@ -39,7 +41,6 @@ import java.io.InputStream; 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 @@ -623,7 +624,7 @@ public final class DynamicMessage extends AbstractMessage { /** Verifies that the value is EnumValueDescriptor and matches Enum Type. */ private void ensureSingularEnumValueDescriptor(FieldDescriptor field, Object value) { - Objects.requireNonNull(value); + checkNotNull(value); if (!(value instanceof EnumValueDescriptor)) { throw new IllegalArgumentException( "DynamicMessage should use EnumValueDescriptor to set Enum Value."); diff --git a/java/core/src/main/java/com/google/protobuf/FieldInfo.java b/java/core/src/main/java/com/google/protobuf/FieldInfo.java index 201e0e4e39..71a307a895 100644 --- a/java/core/src/main/java/com/google/protobuf/FieldInfo.java +++ b/java/core/src/main/java/com/google/protobuf/FieldInfo.java @@ -30,9 +30,10 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + import com.google.protobuf.Internal.EnumVerifier; import java.lang.reflect.Field; -import java.util.Objects; /** Information for a single field in a protobuf message class. */ @ExperimentalApi @@ -63,8 +64,8 @@ final class FieldInfo implements Comparable { public static FieldInfo forField( Field field, int fieldNumber, FieldType fieldType, boolean enforceUtf8) { checkFieldNumber(fieldNumber); - Objects.requireNonNull(field, "field"); - Objects.requireNonNull(fieldType, "fieldType"); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); if (fieldType == FieldType.MESSAGE_LIST || fieldType == FieldType.GROUP_LIST) { throw new IllegalStateException("Shouldn't be called for repeated message fields."); } @@ -88,8 +89,8 @@ final class FieldInfo implements Comparable { public static FieldInfo forPackedField( Field field, int fieldNumber, FieldType fieldType, Field cachedSizeField) { checkFieldNumber(fieldNumber); - Objects.requireNonNull(field, "field"); - Objects.requireNonNull(fieldType, "fieldType"); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); if (fieldType == FieldType.MESSAGE_LIST || fieldType == FieldType.GROUP_LIST) { throw new IllegalStateException("Shouldn't be called for repeated message fields."); } @@ -113,9 +114,9 @@ final class FieldInfo implements Comparable { public static FieldInfo forRepeatedMessageField( Field field, int fieldNumber, FieldType fieldType, Class messageClass) { checkFieldNumber(fieldNumber); - Objects.requireNonNull(field, "field"); - Objects.requireNonNull(fieldType, "fieldType"); - Objects.requireNonNull(messageClass, "messageClass"); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); + checkNotNull(messageClass, "messageClass"); return new FieldInfo( field, fieldNumber, @@ -135,7 +136,7 @@ final class FieldInfo implements Comparable { public static FieldInfo forFieldWithEnumVerifier( Field field, int fieldNumber, FieldType fieldType, EnumVerifier enumVerifier) { checkFieldNumber(fieldNumber); - Objects.requireNonNull(field, "field"); + checkNotNull(field, "field"); return new FieldInfo( field, fieldNumber, @@ -159,7 +160,7 @@ final class FieldInfo implements Comparable { EnumVerifier enumVerifier, Field cachedSizeField) { checkFieldNumber(fieldNumber); - Objects.requireNonNull(field, "field"); + checkNotNull(field, "field"); return new FieldInfo( field, fieldNumber, @@ -186,9 +187,9 @@ final class FieldInfo implements Comparable { boolean enforceUtf8, EnumVerifier enumVerifier) { checkFieldNumber(fieldNumber); - Objects.requireNonNull(field, "field"); - Objects.requireNonNull(fieldType, "fieldType"); - Objects.requireNonNull(presenceField, "presenceField"); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); + checkNotNull(presenceField, "presenceField"); if (presenceField != null && !isExactlyOneBitSet(presenceMask)) { throw new IllegalArgumentException( "presenceMask must have exactly one bit set: " + presenceMask); @@ -229,9 +230,9 @@ final class FieldInfo implements Comparable { boolean enforceUtf8, EnumVerifier enumVerifier) { checkFieldNumber(fieldNumber); - Objects.requireNonNull(fieldType, "fieldType"); - Objects.requireNonNull(oneof, "oneof"); - Objects.requireNonNull(oneofStoredType, "oneofStoredType"); + checkNotNull(fieldType, "fieldType"); + checkNotNull(oneof, "oneof"); + checkNotNull(oneofStoredType, "oneofStoredType"); if (!fieldType.isScalar()) { throw new IllegalArgumentException( "Oneof is only supported for scalar fields. Field " @@ -271,9 +272,9 @@ final class FieldInfo implements Comparable { boolean enforceUtf8, EnumVerifier enumVerifier) { checkFieldNumber(fieldNumber); - Objects.requireNonNull(field, "field"); - Objects.requireNonNull(fieldType, "fieldType"); - Objects.requireNonNull(presenceField, "presenceField"); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); + checkNotNull(presenceField, "presenceField"); if (presenceField != null && !isExactlyOneBitSet(presenceMask)) { throw new IllegalArgumentException( "presenceMask must have exactly one bit set: " + presenceMask); @@ -296,9 +297,9 @@ final class FieldInfo implements Comparable { public static FieldInfo forMapField( Field field, int fieldNumber, Object mapDefaultEntry, EnumVerifier enumVerifier) { - Objects.requireNonNull(mapDefaultEntry, "mapDefaultEntry"); + checkNotNull(mapDefaultEntry, "mapDefaultEntry"); checkFieldNumber(fieldNumber); - Objects.requireNonNull(field, "field"); + checkNotNull(field, "field"); return new FieldInfo( field, fieldNumber, @@ -488,7 +489,7 @@ final class FieldInfo implements Comparable { /** Specifies proto2 presence information. This should not be called for oneof fields. */ public Builder withPresence(Field presenceField, int presenceMask) { - this.presenceField = Objects.requireNonNull(presenceField, "presenceField"); + this.presenceField = checkNotNull(presenceField, "presenceField"); this.presenceMask = presenceMask; return this; } diff --git a/java/core/src/main/java/com/google/protobuf/FieldSet.java b/java/core/src/main/java/com/google/protobuf/FieldSet.java index e91151fa36..4853df2837 100644 --- a/java/core/src/main/java/com/google/protobuf/FieldSet.java +++ b/java/core/src/main/java/com/google/protobuf/FieldSet.java @@ -30,6 +30,8 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + import com.google.protobuf.LazyField.LazyIterator; import java.io.IOException; import java.util.ArrayList; @@ -37,7 +39,6 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Objects; /** * A class which represents an arbitrary set of fields of some message type. This is used to @@ -409,7 +410,7 @@ final class FieldSet> { private static boolean isValidType(final WireFormat.FieldType type, final Object value) { - Objects.requireNonNull(value); + checkNotNull(value); switch (type.getJavaType()) { case INT: return value instanceof Integer; diff --git a/java/core/src/main/java/com/google/protobuf/FloatArrayList.java b/java/core/src/main/java/com/google/protobuf/FloatArrayList.java index bb8f93160a..e6feba8a35 100644 --- a/java/core/src/main/java/com/google/protobuf/FloatArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/FloatArrayList.java @@ -30,10 +30,11 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + import com.google.protobuf.Internal.FloatList; import java.util.Arrays; import java.util.Collection; -import java.util.Objects; import java.util.RandomAccess; /** @@ -236,7 +237,7 @@ final class FloatArrayList extends AbstractProtobufList public boolean addAll(Collection collection) { ensureIsMutable(); - Objects.requireNonNull(collection); + checkNotNull(collection); // We specialize when adding another FloatArrayList to avoid boxing elements. if (!(collection instanceof FloatArrayList)) { diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java index 184838b80a..46427b3ddf 100644 --- a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java @@ -30,6 +30,8 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + import com.google.protobuf.Descriptors.Descriptor; import com.google.protobuf.Descriptors.EnumDescriptor; import com.google.protobuf.Descriptors.EnumValueDescriptor; diff --git a/java/core/src/main/java/com/google/protobuf/IntArrayList.java b/java/core/src/main/java/com/google/protobuf/IntArrayList.java index ed171398c1..9daeebed99 100644 --- a/java/core/src/main/java/com/google/protobuf/IntArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/IntArrayList.java @@ -30,10 +30,11 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + import com.google.protobuf.Internal.IntList; import java.util.Arrays; import java.util.Collection; -import java.util.Objects; import java.util.RandomAccess; /** @@ -236,7 +237,7 @@ final class IntArrayList extends AbstractProtobufList public boolean addAll(Collection collection) { ensureIsMutable(); - Objects.requireNonNull(collection); + checkNotNull(collection); // We specialize when adding another IntArrayList to avoid boxing elements. if (!(collection instanceof IntArrayList)) { diff --git a/java/core/src/main/java/com/google/protobuf/Internal.java b/java/core/src/main/java/com/google/protobuf/Internal.java index e62106a42e..90643b8abb 100644 --- a/java/core/src/main/java/com/google/protobuf/Internal.java +++ b/java/core/src/main/java/com/google/protobuf/Internal.java @@ -57,6 +57,22 @@ public final class Internal { static final Charset UTF_8 = Charset.forName("UTF-8"); static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); + /** Throws an appropriate {@link NullPointerException} if the given objects is {@code null}. */ + static T checkNotNull(T obj) { + if (obj == null) { + throw new NullPointerException(); + } + return obj; + } + + /** Throws an appropriate {@link NullPointerException} if the given objects is {@code null}. */ + static T checkNotNull(T obj, String message) { + if (obj == null) { + throw new NullPointerException(message); + } + return obj; + } + /** * Helper called by generated code to construct default values for string fields. * diff --git a/java/core/src/main/java/com/google/protobuf/LazyFieldLite.java b/java/core/src/main/java/com/google/protobuf/LazyFieldLite.java index fe940b9f00..6fab26fc53 100644 --- a/java/core/src/main/java/com/google/protobuf/LazyFieldLite.java +++ b/java/core/src/main/java/com/google/protobuf/LazyFieldLite.java @@ -31,7 +31,6 @@ package com.google.protobuf; import java.io.IOException; -import java.util.Objects; /** * LazyFieldLite encapsulates the logic of lazily parsing message fields. It stores the message in a @@ -117,8 +116,9 @@ public class LazyFieldLite { /** Constructs a LazyFieldLite with bytes that will be parsed lazily. */ public LazyFieldLite(ExtensionRegistryLite extensionRegistry, ByteString bytes) { - this.extensionRegistry = Objects.requireNonNull(extensionRegistry, "found null ExtensionRegistry"); - this.delayedBytes = Objects.requireNonNull(bytes, "found null ByteString"); + checkArguments(extensionRegistry, bytes); + this.extensionRegistry = extensionRegistry; + this.delayedBytes = bytes; } /** Constructs a LazyFieldLite with no contents, and no ability to parse extensions. */ @@ -340,8 +340,9 @@ public class LazyFieldLite { /** Sets this field with bytes to delay-parse. */ public void setByteString(ByteString bytes, ExtensionRegistryLite extensionRegistry) { - this.delayedBytes = Objects.requireNonNull(bytes, "found null ByteString"); - this.extensionRegistry = Objects.requireNonNull(extensionRegistry, "found null ExtensionRegistry"); + checkArguments(extensionRegistry, bytes); + this.delayedBytes = bytes; + this.extensionRegistry = extensionRegistry; this.value = null; this.memoizedBytes = null; } @@ -428,4 +429,13 @@ public class LazyFieldLite { } } } + + private static void checkArguments(ExtensionRegistryLite extensionRegistry, ByteString bytes) { + if (extensionRegistry == null) { + throw new NullPointerException("found null ExtensionRegistry"); + } + if (bytes == null) { + throw new NullPointerException("found null ByteString"); + } + } } diff --git a/java/core/src/main/java/com/google/protobuf/LongArrayList.java b/java/core/src/main/java/com/google/protobuf/LongArrayList.java index 84b1ead5cc..bda43a41bb 100644 --- a/java/core/src/main/java/com/google/protobuf/LongArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/LongArrayList.java @@ -30,10 +30,11 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + import com.google.protobuf.Internal.LongList; import java.util.Arrays; import java.util.Collection; -import java.util.Objects; import java.util.RandomAccess; /** @@ -236,7 +237,7 @@ final class LongArrayList extends AbstractProtobufList public boolean addAll(Collection collection) { ensureIsMutable(); - Objects.requireNonNull(collection); + checkNotNull(collection); // We specialize when adding another LongArrayList to avoid boxing elements. if (!(collection instanceof LongArrayList)) { diff --git a/java/core/src/main/java/com/google/protobuf/ManifestSchemaFactory.java b/java/core/src/main/java/com/google/protobuf/ManifestSchemaFactory.java index 0fc793f9ae..84ca9ae0f7 100644 --- a/java/core/src/main/java/com/google/protobuf/ManifestSchemaFactory.java +++ b/java/core/src/main/java/com/google/protobuf/ManifestSchemaFactory.java @@ -30,7 +30,7 @@ package com.google.protobuf; -import java.util.Objects; +import static com.google.protobuf.Internal.checkNotNull; /** * Dynamically generates a manifest-based (i.e. table-based) schema for a given protobuf message. @@ -45,7 +45,7 @@ final class ManifestSchemaFactory implements SchemaFactory { } private ManifestSchemaFactory(MessageInfoFactory messageInfoFactory) { - this.messageInfoFactory = Objects.requireNonNull(messageInfoFactory, "messageInfoFactory"); + this.messageInfoFactory = checkNotNull(messageInfoFactory, "messageInfoFactory"); } @Override diff --git a/java/core/src/main/java/com/google/protobuf/MapField.java b/java/core/src/main/java/com/google/protobuf/MapField.java index 5d1f52369e..f487736065 100644 --- a/java/core/src/main/java/com/google/protobuf/MapField.java +++ b/java/core/src/main/java/com/google/protobuf/MapField.java @@ -30,6 +30,8 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -37,7 +39,6 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Set; /** @@ -319,8 +320,8 @@ public class MapField implements MutabilityOracle { @Override public V put(K key, V value) { mutabilityOracle.ensureMutable(); - Objects.requireNonNull(key); - Objects.requireNonNull(value); + checkNotNull(key); + checkNotNull(value); return delegate.put(key, value); } @@ -334,8 +335,8 @@ public class MapField implements MutabilityOracle { public void putAll(Map m) { mutabilityOracle.ensureMutable(); for (K key : m.keySet()) { - Objects.requireNonNull(key); - Objects.requireNonNull(m.get(key)); + checkNotNull(key); + checkNotNull(m.get(key)); } delegate.putAll(m); } diff --git a/java/core/src/main/java/com/google/protobuf/MapFieldLite.java b/java/core/src/main/java/com/google/protobuf/MapFieldLite.java index 66f5f46566..f792ae9fc7 100644 --- a/java/core/src/main/java/com/google/protobuf/MapFieldLite.java +++ b/java/core/src/main/java/com/google/protobuf/MapFieldLite.java @@ -30,12 +30,13 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + import com.google.protobuf.Internal.EnumLite; import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; -import java.util.Objects; import java.util.Set; /** @@ -91,9 +92,9 @@ public final class MapFieldLite extends LinkedHashMap { @Override public V put(K key, V value) { ensureMutable(); - Objects.requireNonNull(key); + checkNotNull(key); - Objects.requireNonNull(value); + checkNotNull(value); return super.put(key, value); } @@ -116,8 +117,8 @@ public final class MapFieldLite extends LinkedHashMap { private static void checkForNullKeysAndValues(Map m) { for (Object key : m.keySet()) { - Objects.requireNonNull(key); - Objects.requireNonNull(m.get(key)); + checkNotNull(key); + checkNotNull(m.get(key)); } } diff --git a/java/core/src/main/java/com/google/protobuf/MessageSchema.java b/java/core/src/main/java/com/google/protobuf/MessageSchema.java index e075679b58..33c8e914b2 100644 --- a/java/core/src/main/java/com/google/protobuf/MessageSchema.java +++ b/java/core/src/main/java/com/google/protobuf/MessageSchema.java @@ -80,7 +80,6 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Objects; /** Schema used for standard messages. */ final class MessageSchema implements Schema { @@ -1177,7 +1176,9 @@ final class MessageSchema implements Schema { @Override public void mergeFrom(T message, T other) { - Objects.requireNonNull(other); + if (other == null) { + throw new NullPointerException(); + } for (int i = 0; i < buffer.length; i += INTS_PER_FIELD) { // A separate method allows for better JIT optimizations mergeSingleField(message, other, i); @@ -3849,7 +3850,9 @@ final class MessageSchema implements Schema { @Override public void mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry) throws IOException { - Objects.requireNonNull(extensionRegistry); + if (extensionRegistry == null) { + throw new NullPointerException(); + } mergeFromHelper(unknownFieldSchema, extensionSchema, message, reader, extensionRegistry); } diff --git a/java/core/src/main/java/com/google/protobuf/NioByteString.java b/java/core/src/main/java/com/google/protobuf/NioByteString.java index e7670c6b56..1e594ff878 100644 --- a/java/core/src/main/java/com/google/protobuf/NioByteString.java +++ b/java/core/src/main/java/com/google/protobuf/NioByteString.java @@ -30,6 +30,8 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + import java.io.IOException; import java.io.InputStream; import java.io.InvalidObjectException; @@ -42,14 +44,13 @@ import java.nio.InvalidMarkException; import java.nio.charset.Charset; import java.util.Collections; import java.util.List; -import java.util.Objects; /** A {@link ByteString} that wraps around a {@link ByteBuffer}. */ final class NioByteString extends ByteString.LeafByteString { private final ByteBuffer buffer; NioByteString(ByteBuffer buffer) { - Objects.requireNonNull(buffer, "buffer"); + checkNotNull(buffer, "buffer"); // Use native byte order for fast fixed32/64 operations. this.buffer = buffer.slice().order(ByteOrder.nativeOrder()); diff --git a/java/core/src/main/java/com/google/protobuf/Protobuf.java b/java/core/src/main/java/com/google/protobuf/Protobuf.java index ce099f8b96..0affac5f0a 100644 --- a/java/core/src/main/java/com/google/protobuf/Protobuf.java +++ b/java/core/src/main/java/com/google/protobuf/Protobuf.java @@ -30,8 +30,9 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + import java.io.IOException; -import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -82,7 +83,7 @@ final class Protobuf { /** Gets the schema for the given message type. */ public Schema schemaFor(Class messageType) { - Objects.requireNonNull(messageType, "messageType"); + checkNotNull(messageType, "messageType"); @SuppressWarnings("unchecked") Schema schema = (Schema) schemaCache.get(messageType); if (schema == null) { @@ -112,8 +113,8 @@ final class Protobuf { * registered. */ public Schema registerSchema(Class messageType, Schema schema) { - Objects.requireNonNull(messageType, "messageType"); - Objects.requireNonNull(schema, "schema"); + checkNotNull(messageType, "messageType"); + checkNotNull(schema, "schema"); return schemaCache.putIfAbsent(messageType, schema); } @@ -127,8 +128,8 @@ final class Protobuf { * previously. */ public Schema registerSchemaOverride(Class messageType, Schema schema) { - Objects.requireNonNull(messageType, "messageType"); - Objects.requireNonNull(schema, "schema"); + checkNotNull(messageType, "messageType"); + checkNotNull(schema, "schema"); return schemaCache.put(messageType, schema); } diff --git a/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilder.java b/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilder.java index f8a10806d7..f51436c83d 100644 --- a/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilder.java +++ b/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilder.java @@ -30,12 +30,13 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + import java.util.AbstractList; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.Objects; /** * {@code RepeatedFieldBuilder} implements a structure that a protocol message uses to hold a @@ -276,7 +277,7 @@ public class RepeatedFieldBuilder< * @return the builder */ public RepeatedFieldBuilder setMessage(int index, MType message) { - Objects.requireNonNull(message); + checkNotNull(message); ensureMutableMessageList(); messages.set(index, message); if (builders != null) { @@ -297,7 +298,7 @@ public class RepeatedFieldBuilder< * @return the builder */ public RepeatedFieldBuilder addMessage(MType message) { - Objects.requireNonNull(message); + checkNotNull(message); ensureMutableMessageList(); messages.add(message); if (builders != null) { @@ -318,7 +319,7 @@ public class RepeatedFieldBuilder< * @return the builder */ public RepeatedFieldBuilder addMessage(int index, MType message) { - Objects.requireNonNull(message); + checkNotNull(message); ensureMutableMessageList(); messages.add(index, message); if (builders != null) { @@ -339,7 +340,7 @@ public class RepeatedFieldBuilder< public RepeatedFieldBuilder addAllMessages( Iterable values) { for (final MType value : values) { - Objects.requireNonNull(value); + checkNotNull(value); } // If we can inspect the size, we can more efficiently add messages. diff --git a/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilderV3.java b/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilderV3.java index b3ff588ffc..91bc3e286d 100644 --- a/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilderV3.java +++ b/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilderV3.java @@ -30,12 +30,13 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + import java.util.AbstractList; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.Objects; /** * {@code RepeatedFieldBuilderV3} implements a structure that a protocol message uses to hold a @@ -276,7 +277,7 @@ public class RepeatedFieldBuilderV3< * @return the builder */ public RepeatedFieldBuilderV3 setMessage(int index, MType message) { - Objects.requireNonNull(message); + checkNotNull(message); ensureMutableMessageList(); messages.set(index, message); if (builders != null) { @@ -297,7 +298,7 @@ public class RepeatedFieldBuilderV3< * @return the builder */ public RepeatedFieldBuilderV3 addMessage(MType message) { - Objects.requireNonNull(message); + checkNotNull(message); ensureMutableMessageList(); messages.add(message); if (builders != null) { @@ -318,7 +319,7 @@ public class RepeatedFieldBuilderV3< * @return the builder */ public RepeatedFieldBuilderV3 addMessage(int index, MType message) { - Objects.requireNonNull(message); + checkNotNull(message); ensureMutableMessageList(); messages.add(index, message); if (builders != null) { @@ -339,7 +340,7 @@ public class RepeatedFieldBuilderV3< public RepeatedFieldBuilderV3 addAllMessages( Iterable values) { for (final MType value : values) { - Objects.requireNonNull(value); + checkNotNull(value); } // If we can inspect the size, we can more efficiently add messages. diff --git a/java/core/src/main/java/com/google/protobuf/RopeByteString.java b/java/core/src/main/java/com/google/protobuf/RopeByteString.java index 2adff63f90..f584164410 100644 --- a/java/core/src/main/java/com/google/protobuf/RopeByteString.java +++ b/java/core/src/main/java/com/google/protobuf/RopeByteString.java @@ -44,7 +44,6 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; -import java.util.Objects; /** * Class to represent {@code ByteStrings} formed by concatenation of other ByteStrings, without @@ -845,8 +844,9 @@ final class RopeByteString extends ByteString { */ @Override public int read(byte[] b, int offset, int length) { - Objects.requireNonNull(b); - if (offset < 0 || length < 0 || length > b.length - offset) { + if (b == null) { + throw new NullPointerException(); + } else if (offset < 0 || length < 0 || length > b.length - offset) { throw new IndexOutOfBoundsException(); } int bytesRead = readSkipInternal(b, offset, length); diff --git a/java/core/src/main/java/com/google/protobuf/SingleFieldBuilder.java b/java/core/src/main/java/com/google/protobuf/SingleFieldBuilder.java index f21fdbb609..acdc1de183 100644 --- a/java/core/src/main/java/com/google/protobuf/SingleFieldBuilder.java +++ b/java/core/src/main/java/com/google/protobuf/SingleFieldBuilder.java @@ -30,7 +30,7 @@ package com.google.protobuf; -import java.util.Objects; +import static com.google.protobuf.Internal.checkNotNull; /** * {@code SingleFieldBuilder} implements a structure that a protocol message uses to hold a single @@ -77,7 +77,7 @@ public class SingleFieldBuilder< private boolean isClean; public SingleFieldBuilder(MType message, GeneratedMessage.BuilderParent parent, boolean isClean) { - this.message = Objects.requireNonNull(message); + this.message = checkNotNull(message); this.parent = parent; this.isClean = isClean; } @@ -157,7 +157,7 @@ public class SingleFieldBuilder< * @return the builder */ public SingleFieldBuilder setMessage(MType message) { - this.message = Objects.requireNonNull(message); + this.message = checkNotNull(message); if (builder != null) { builder.dispose(); builder = null; diff --git a/java/core/src/main/java/com/google/protobuf/SingleFieldBuilderV3.java b/java/core/src/main/java/com/google/protobuf/SingleFieldBuilderV3.java index 22d3afa0e7..78a4a21664 100644 --- a/java/core/src/main/java/com/google/protobuf/SingleFieldBuilderV3.java +++ b/java/core/src/main/java/com/google/protobuf/SingleFieldBuilderV3.java @@ -30,7 +30,7 @@ package com.google.protobuf; -import java.util.Objects; +import static com.google.protobuf.Internal.checkNotNull; /** * {@code SingleFieldBuilderV3} implements a structure that a protocol message uses to hold a single @@ -77,7 +77,7 @@ public class SingleFieldBuilderV3< private boolean isClean; public SingleFieldBuilderV3(MType message, AbstractMessage.BuilderParent parent, boolean isClean) { - this.message = Objects.requireNonNull(message); + this.message = checkNotNull(message); this.parent = parent; this.isClean = isClean; } @@ -157,7 +157,7 @@ public class SingleFieldBuilderV3< * @return the builder */ public SingleFieldBuilderV3 setMessage(MType message) { - this.message = Objects.requireNonNull(message); + this.message = checkNotNull(message); if (builder != null) { builder.dispose(); builder = null; diff --git a/java/core/src/main/java/com/google/protobuf/StructuralMessageInfo.java b/java/core/src/main/java/com/google/protobuf/StructuralMessageInfo.java index 20208391c3..a32b1430ee 100644 --- a/java/core/src/main/java/com/google/protobuf/StructuralMessageInfo.java +++ b/java/core/src/main/java/com/google/protobuf/StructuralMessageInfo.java @@ -30,10 +30,11 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Objects; /** * Information for the layout of a protobuf message class. This describes all of the fields @@ -63,7 +64,7 @@ final class StructuralMessageInfo implements MessageInfo { this.messageSetWireFormat = messageSetWireFormat; this.checkInitialized = checkInitialized; this.fields = fields; - this.defaultInstance = (MessageLite) Objects.requireNonNull(defaultInstance, "defaultInstance"); + this.defaultInstance = (MessageLite) checkNotNull(defaultInstance, "defaultInstance"); } /** Gets the syntax for the message (e.g. PROTO2, PROTO3). */ @@ -128,7 +129,7 @@ final class StructuralMessageInfo implements MessageInfo { } public void withSyntax(ProtoSyntax syntax) { - this.syntax = Objects.requireNonNull(syntax, "syntax"); + this.syntax = checkNotNull(syntax, "syntax"); } public void withMessageSetWireFormat(boolean messageSetWireFormat) { diff --git a/java/pom.xml b/java/pom.xml index f008094848..d85e43040d 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -166,6 +166,27 @@ maven-antrun-plugin 1.8 + + org.codehaus.mojo + animal-sniffer-maven-plugin + 1.20 + + + net.sf.androidscents.signature + android-api-level-14 + 4.0_r4 + + + + + android + test + + check + + + +