From a9c4e082e053b40730e8091da2cc076ba3fadcb6 Mon Sep 17 00:00:00 2001 From: Brian Duff Date: Fri, 28 Jun 2013 17:23:51 -0700 Subject: [PATCH] Make it possible to use MessageNano.mergeFrom without casting. You can now do: MyMessage foo = MessageNano.mergeFrom(new MyMessage(), bytes); without having to cast the message returned from mergeFrom. Change-Id: Ibb2ad327f75855d45352ad304c7f054f20dd29c9 --- .../main/java/com/google/protobuf/nano/MessageNano.java | 6 +++--- java/src/test/java/com/google/protobuf/NanoTest.java | 9 +++++++++ .../protobuf/compiler/javanano/javanano_message.cc | 2 +- 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 5c1eb2ff5e..8d4ec39e77 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 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 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 38fafb9e4c..19f39575c7 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 List list(T first, T... remaining) { List list = new ArrayList(); list.add(first); diff --git a/src/google/protobuf/compiler/javanano/javanano_message.cc b/src/google/protobuf/compiler/javanano/javanano_message.cc index 27407794ad..11fb452603 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"