diff --git a/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java b/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java index ef30749325..adf5a2abbf 100644 --- a/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java +++ b/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java @@ -151,7 +151,7 @@ public class ProtoCaliperBenchmark { } } - @SuppressWarnings("IgnoredPureGetter") + @SuppressWarnings({"IgnoredPureGetter", "CheckReturnValue"}) @Benchmark void serializeToByteArray(int reps) throws IOException { if (sampleMessageList.size() == 0) { diff --git a/java/core/src/main/java/com/google/protobuf/MessageLite.java b/java/core/src/main/java/com/google/protobuf/MessageLite.java index f9b2f6653f..8c172eef8c 100644 --- a/java/core/src/main/java/com/google/protobuf/MessageLite.java +++ b/java/core/src/main/java/com/google/protobuf/MessageLite.java @@ -59,6 +59,7 @@ import java.io.OutputStream; * * @author kenton@google.com Kenton Varda */ +@CheckReturnValue public interface MessageLite extends MessageLiteOrBuilder { /** @@ -138,6 +139,7 @@ public interface MessageLite extends MessageLiteOrBuilder { /** Abstract interface implemented by Protocol Message builders. */ interface Builder extends MessageLiteOrBuilder, Cloneable { /** Resets all fields to their default values. */ + @CanIgnoreReturnValue Builder clear(); /** @@ -181,11 +183,12 @@ public interface MessageLite extends MessageLiteOrBuilder { *
Note: The caller should call {@link CodedInputStream#checkLastTagWas(int)} after calling * this to verify that the last tag seen was the appropriate end-group tag, or zero for EOF. * - * @throws InvalidProtocolBufferException the bytes read are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according + * to the protobuf wire format specification. The data is corrupt, incomplete, or was never + * a protobuf in the first place. * @throws IOException an I/O error reading from the stream */ + @CanIgnoreReturnValue Builder mergeFrom(CodedInputStream input) throws IOException; /** @@ -193,11 +196,12 @@ public interface MessageLite extends MessageLiteOrBuilder { * that you want to be able to parse must be registered in {@code extensionRegistry}. Extensions * not in the registry will be treated as unknown fields. * - * @throws InvalidProtocolBufferException the bytes read are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according + * to the protobuf wire format specification. The data is corrupt, incomplete, or was never + * a protobuf in the first place. * @throws IOException an I/O error reading from the stream */ + @CanIgnoreReturnValue Builder mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws IOException; @@ -209,10 +213,11 @@ public interface MessageLite extends MessageLiteOrBuilder { * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. * * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * according to the protobuf wire format specification. The data is corrupt, incomplete, or + * was never a protobuf in the first place. * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(ByteString data) throws InvalidProtocolBufferException; /** @@ -220,10 +225,11 @@ public interface MessageLite extends MessageLiteOrBuilder { * is just a small wrapper around {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. * * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * according to the protobuf wire format specification. The data is corrupt, incomplete, or + * was never a protobuf in the first place. * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(ByteString data, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; @@ -232,10 +238,11 @@ public interface MessageLite extends MessageLiteOrBuilder { * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. * * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * according to the protobuf wire format specification. The data is corrupt, incomplete, or + * was never a protobuf in the first place. * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(byte[] data) throws InvalidProtocolBufferException; /** @@ -243,10 +250,11 @@ public interface MessageLite extends MessageLiteOrBuilder { * is just a small wrapper around {@link #mergeFrom(CodedInputStream)}. * * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * according to the protobuf wire format specification. The data is corrupt, incomplete, or + * was never a protobuf in the first place. * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(byte[] data, int off, int len) throws InvalidProtocolBufferException; /** @@ -254,10 +262,11 @@ public interface MessageLite extends MessageLiteOrBuilder { * is just a small wrapper around {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. * * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * according to the protobuf wire format specification. The data is corrupt, incomplete, or + * was never a protobuf in the first place. * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(byte[] data, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; @@ -266,10 +275,11 @@ public interface MessageLite extends MessageLiteOrBuilder { * is just a small wrapper around {@link #mergeFrom(CodedInputStream,ExtensionRegistryLite)}. * * @throws InvalidProtocolBufferException the bytes in data are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * according to the protobuf wire format specification. The data is corrupt, incomplete, or + * was never a protobuf in the first place. * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException; @@ -283,12 +293,13 @@ public interface MessageLite extends MessageLiteOrBuilder { * *
Despite usually reading the entire input, this does not close the stream. * - * @throws InvalidProtocolBufferException the bytes read are not syntactically correct - * according to the protobuf wire format specification. The data is corrupt, incomplete, - * or was never a protobuf in the first place. + * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according + * to the protobuf wire format specification. The data is corrupt, incomplete, or was never + * a protobuf in the first place. * @throws IOException an I/O error reading from the stream * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(InputStream input) throws IOException; /** @@ -298,6 +309,7 @@ public interface MessageLite extends MessageLiteOrBuilder { * * @return this */ + @CanIgnoreReturnValue Builder mergeFrom(InputStream input, ExtensionRegistryLite extensionRegistry) throws IOException; @@ -317,6 +329,7 @@ public interface MessageLite extends MessageLiteOrBuilder { * *
This is equivalent to the {@code Message::MergeFrom} method in C++.
*/
+ @CanIgnoreReturnValue
Builder mergeFrom(MessageLite other);
/**
@@ -326,11 +339,12 @@ public interface MessageLite extends MessageLiteOrBuilder {
*
* @return true if successful, or false if the stream is at EOF when the method starts. Any
* other error (including reaching EOF during parsing) causes an exception to be thrown.
- * @throws InvalidProtocolBufferException the bytes read are not syntactically correct
- * according to the protobuf wire format specification. The data is corrupt, incomplete,
- * or was never a protobuf in the first place.
+ * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according
+ * to the protobuf wire format specification. The data is corrupt, incomplete, or was never
+ * a protobuf in the first place.
* @throws IOException an I/O error reading from the stream
*/
+ @CanIgnoreReturnValue // TODO(kak): should this be @CheckReturnValue instead?
boolean mergeDelimitedFrom(InputStream input) throws IOException;
/**
@@ -338,11 +352,12 @@ public interface MessageLite extends MessageLiteOrBuilder {
*
* @return true if successful, or false if the stream is at EOF when the method starts. Any
* other error (including reaching EOF during parsing) causes an exception to be thrown.
- * @throws InvalidProtocolBufferException the bytes read are not syntactically correct
- * according to the protobuf wire format specification. The data is corrupt, incomplete,
- * or was never a protobuf in the first place.
+ * @throws InvalidProtocolBufferException the bytes read are not syntactically correct according
+ * to the protobuf wire format specification. The data is corrupt, incomplete, or was never
+ * a protobuf in the first place.
* @throws IOException an I/O error reading from the stream
*/
+ @CanIgnoreReturnValue // TODO(kak): should this be @CheckReturnValue instead?
boolean mergeDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry)
throws IOException;
}
diff --git a/java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java b/java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java
index 7a5ef3ed51..927b95fcc2 100644
--- a/java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java
+++ b/java/core/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java
@@ -36,6 +36,7 @@ package com.google.protobuf;
*
* @author jonp@google.com (Jon Perlow)
*/
+@CheckReturnValue
public interface MessageLiteOrBuilder {
/**
* Get an instance of the type with no fields set. Because no fields are set, all getters for
diff --git a/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java b/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java
index 55864afa72..f4c4baf4a2 100644
--- a/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java
+++ b/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java
@@ -1529,7 +1529,7 @@ public class GeneratedMessageTest {
assertThat(builder.getFooInt()).isEqualTo(123);
TestOneof2 message = builder.buildPartial();
assertThat(message.hasFooInt()).isTrue();
- assertThat(123).isEqualTo(message.getFooInt());
+ assertThat(message.getFooInt()).isEqualTo(123);
assertThat(builder.clearFooInt().hasFooInt()).isFalse();
TestOneof2 message2 = builder.build();
diff --git a/java/core/src/test/java/com/google/protobuf/TestUtil.java b/java/core/src/test/java/com/google/protobuf/TestUtil.java
index 46a4d17d61..71a540a0c4 100644
--- a/java/core/src/test/java/com/google/protobuf/TestUtil.java
+++ b/java/core/src/test/java/com/google/protobuf/TestUtil.java
@@ -235,6 +235,7 @@ import protobuf_unittest.UnittestProto.TestRequired;
import protobuf_unittest.UnittestProto.TestUnpackedTypes;
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
@@ -3842,7 +3843,11 @@ public final class TestUtil {
private static ByteString readBytesFromResource(String name) {
try {
- return ByteString.readFrom(TestUtil.class.getResourceAsStream(name));
+ InputStream in = TestUtil.class.getResourceAsStream(name);
+ if (in == null) { //
+ throw new RuntimeException("Tests data file " + name + " is missing.");
+ }
+ return ByteString.readFrom(in);
} catch (IOException e) {
throw new RuntimeException(e);
}
diff --git a/src/google/protobuf/arena.h b/src/google/protobuf/arena.h
index 3b5f16c389..0f9f81366f 100644
--- a/src/google/protobuf/arena.h
+++ b/src/google/protobuf/arena.h
@@ -315,6 +315,15 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
static_cast