diff options
author | Wink Saville <wink@google.com> | 2014-05-07 23:41:28 +0000 |
---|---|---|
committer | Wink Saville <wink@google.com> | 2014-05-07 16:59:46 -0700 |
commit | d1439291d0a2d6558903205676d41410c319587e (patch) | |
tree | 6931963b83b155148dd0bacd7d806665a62b36d8 /java | |
parent | 67ebd315682b27dc0d2233eabe23bd8cbb481378 (diff) | |
download | external_protobuf-d1439291d0a2d6558903205676d41410c319587e.zip external_protobuf-d1439291d0a2d6558903205676d41410c319587e.tar.gz external_protobuf-d1439291d0a2d6558903205676d41410c319587e.tar.bz2 |
Revert "Merge commit 'e887563a' into fix-merge-conclict"
This reverts commit f65ee55561e2a5f53be6db2ce03e518e249c9e80.
Change-Id: Ia6fed4d96fb4c076b2b51e1d758f3e15d37bffc0
Diffstat (limited to 'java')
-rw-r--r-- | java/src/main/java/com/google/protobuf/nano/MessageNano.java | 17 | ||||
-rw-r--r-- | java/src/test/java/com/google/protobuf/NanoTest.java | 26 |
2 files changed, 39 insertions, 4 deletions
diff --git a/java/src/main/java/com/google/protobuf/nano/MessageNano.java b/java/src/main/java/com/google/protobuf/nano/MessageNano.java index 82dc6cc..d6288c9 100644 --- a/java/src/main/java/com/google/protobuf/nano/MessageNano.java +++ b/java/src/main/java/com/google/protobuf/nano/MessageNano.java @@ -38,7 +38,7 @@ import java.io.IOException; * @author wink@google.com Wink Saville */ public abstract class MessageNano { - protected int cachedSize = -1; + protected volatile int cachedSize = -1; /** * Get the number of bytes required to encode this message. @@ -61,9 +61,18 @@ public abstract class MessageNano { * using getCachedSize(). */ public int getSerializedSize() { - // This is overridden if the generated message has serialized fields. - cachedSize = 0; - return 0; + int size = computeSerializedSize(); + cachedSize = size; + return size; + } + + /** + * Computes the number of bytes required to encode this message. This does not update the + * cached size. + */ + protected int computeSerializedSize() { + // This is overridden if the generated message has serialized fields. + return 0; } /** diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java index 9987cac..00e2597 100644 --- a/java/src/test/java/com/google/protobuf/NanoTest.java +++ b/java/src/test/java/com/google/protobuf/NanoTest.java @@ -105,6 +105,12 @@ public class NanoTest extends TestCase { assertEquals(456, newMsg.d); assertEquals(2, msg.nestedMsg.bb); assertEquals(SimpleMessageNano.BAR, msg.defaultNestedEnum); + + msg.nestedMsg = null; + assertTrue(msgSerializedSize != msg.getSerializedSize()); + + msg.clear(); + assertEquals(0, msg.getSerializedSize()); } public void testRecursiveMessageNano() throws Exception { @@ -143,6 +149,12 @@ public class NanoTest extends TestCase { assertEquals(3, newMsg.repeatedRecursiveMessageNano[0].id); } + public void testMessageNoFields() { + SingleMessageNano msg = new SingleMessageNano(); + assertEquals(0, msg.getSerializedSize()); + assertEquals(0, MessageNano.toByteArray(msg).length); + } + public void testNanoRequiredInt32() throws Exception { TestAllTypesNano msg = new TestAllTypesNano(); msg.id = 123; @@ -2898,6 +2910,20 @@ public class NanoTest extends TestCase { assertTrue(Arrays.equals(enums, message.getExtension(RepeatedExtensions.repeatedEnum))); } + public void testNullExtensions() throws Exception { + // Check that clearing the extension on an empty message is a no-op. + Extensions.ExtendableMessage message = new Extensions.ExtendableMessage(); + message.setExtension(SingularExtensions.someMessage, null); + assertEquals(0, MessageNano.toByteArray(message).length); + + // Check that the message is empty after setting and clearing an extension. + AnotherMessage another = new AnotherMessage(); + message.setExtension(SingularExtensions.someMessage, another); + assertTrue(MessageNano.toByteArray(message).length > 0); + message.setExtension(SingularExtensions.someMessage, null); + assertEquals(0, MessageNano.toByteArray(message).length); + } + public void testUnknownFields() throws Exception { // Check that we roundtrip (serialize and deserialize) unrecognized fields. AnotherMessage message = new AnotherMessage(); |