diff options
author | Ulas Kirazci <ulas@google.com> | 2013-07-11 08:08:19 -0700 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2013-07-11 08:08:19 -0700 |
commit | 8a87cde979be8c858b290b62d27783ad239c98f4 (patch) | |
tree | 5ce9984cb4a0ea84a255d156b2d1430a3bdb45d1 | |
parent | 898da1a0236f67b9344527d6befe17a0062bf6f2 (diff) | |
parent | 67b4303516bb01e485aa9bad9d2f372c4b2844a3 (diff) | |
download | external_protobuf-8a87cde979be8c858b290b62d27783ad239c98f4.zip external_protobuf-8a87cde979be8c858b290b62d27783ad239c98f4.tar.gz external_protobuf-8a87cde979be8c858b290b62d27783ad239c98f4.tar.bz2 |
am 67b43035: am 8cf17a8f: Merge "Make it possible to use MessageNano.mergeFrom without casting."
* commit '67b4303516bb01e485aa9bad9d2f372c4b2844a3':
Make it possible to use MessageNano.mergeFrom without casting.
3 files changed, 13 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 5c1eb2f..8d4ec39 100644 --- a/java/src/main/java/com/google/protobuf/nano/MessageNano.java +++ b/java/src/main/java/com/google/protobuf/nano/MessageNano.java @@ -101,7 +101,7 @@ public abstract class MessageNano { * Parse {@code data} as a message of this type and merge it with the * message being built. */ - public static final MessageNano mergeFrom(MessageNano msg, final byte[] data) + public static final <T extends MessageNano> T mergeFrom(T msg, final byte[] data) throws InvalidProtocolBufferNanoException { return mergeFrom(msg, data, 0, data.length); } @@ -110,8 +110,8 @@ public abstract class MessageNano { * Parse {@code data} as a message of this type and merge it with the * message being built. */ - public static final MessageNano mergeFrom(MessageNano msg, final byte[] data, final int off, - final int len) throws InvalidProtocolBufferNanoException { + public static final <T extends MessageNano> T mergeFrom(T msg, final byte[] data, + final int off, final int len) throws InvalidProtocolBufferNanoException { try { final CodedInputByteBufferNano input = CodedInputByteBufferNano.newInstance(data, off, len); diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java index 38fafb9..19f3957 100644 --- a/java/src/test/java/com/google/protobuf/NanoTest.java +++ b/java/src/test/java/com/google/protobuf/NanoTest.java @@ -2241,6 +2241,15 @@ public class NanoTest extends TestCase { assertEquals(0, MessageNano.toByteArray(deserialized).length); } + public void testMergeFrom() throws Exception { + SimpleMessageNano message = new SimpleMessageNano(); + message.d = 123; + byte[] bytes = MessageNano.toByteArray(message); + + SimpleMessageNano newMessage = MessageNano.mergeFrom(new SimpleMessageNano(), bytes); + assertEquals(message.d, newMessage.d); + } + private <T> List<T> list(T first, T... remaining) { List<T> list = new ArrayList<T>(); list.add(first); diff --git a/src/google/protobuf/compiler/javanano/javanano_message.cc b/src/google/protobuf/compiler/javanano/javanano_message.cc index 2740779..11fb452 100644 --- a/src/google/protobuf/compiler/javanano/javanano_message.cc +++ b/src/google/protobuf/compiler/javanano/javanano_message.cc @@ -362,7 +362,7 @@ GenerateParseFromMethods(io::Printer* printer) { printer->Print( "public $static$ $classname$ parseFrom(byte[] data)\n" " throws com.google.protobuf.nano.InvalidProtocolBufferNanoException {\n" - " return ($classname$) com.google.protobuf.nano.MessageNano.mergeFrom(new $classname$(), data);\n" + " return com.google.protobuf.nano.MessageNano.mergeFrom(new $classname$(), data);\n" "}\n" "\n" "public $static$ $classname$ parseFrom(\n" |