Commit a9c4e082 authored by Brian Duff's avatar Brian Duff

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
parent 499f47ff
...@@ -101,7 +101,7 @@ public abstract class MessageNano { ...@@ -101,7 +101,7 @@ public abstract class MessageNano {
* Parse {@code data} as a message of this type and merge it with the * Parse {@code data} as a message of this type and merge it with the
* message being built. * 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 { throws InvalidProtocolBufferNanoException {
return mergeFrom(msg, data, 0, data.length); return mergeFrom(msg, data, 0, data.length);
} }
...@@ -110,8 +110,8 @@ public abstract class MessageNano { ...@@ -110,8 +110,8 @@ public abstract class MessageNano {
* Parse {@code data} as a message of this type and merge it with the * Parse {@code data} as a message of this type and merge it with the
* message being built. * message being built.
*/ */
public static final MessageNano mergeFrom(MessageNano msg, final byte[] data, final int off, public static final <T extends MessageNano> T mergeFrom(T msg, final byte[] data,
final int len) throws InvalidProtocolBufferNanoException { final int off, final int len) throws InvalidProtocolBufferNanoException {
try { try {
final CodedInputByteBufferNano input = final CodedInputByteBufferNano input =
CodedInputByteBufferNano.newInstance(data, off, len); CodedInputByteBufferNano.newInstance(data, off, len);
......
...@@ -2241,6 +2241,15 @@ public class NanoTest extends TestCase { ...@@ -2241,6 +2241,15 @@ public class NanoTest extends TestCase {
assertEquals(0, MessageNano.toByteArray(deserialized).length); 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) { private <T> List<T> list(T first, T... remaining) {
List<T> list = new ArrayList<T>(); List<T> list = new ArrayList<T>();
list.add(first); list.add(first);
......
...@@ -362,7 +362,7 @@ GenerateParseFromMethods(io::Printer* printer) { ...@@ -362,7 +362,7 @@ GenerateParseFromMethods(io::Printer* printer) {
printer->Print( printer->Print(
"public $static$ $classname$ parseFrom(byte[] data)\n" "public $static$ $classname$ parseFrom(byte[] data)\n"
" throws com.google.protobuf.nano.InvalidProtocolBufferNanoException {\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"
"\n" "\n"
"public $static$ $classname$ parseFrom(\n" "public $static$ $classname$ parseFrom(\n"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment