Swap nano from Parser to message factory

This reduces the amount of logic built into the generated code. If we
swap to an alternative form of decoding we should have greater ability
to adapt the existing API to make use of the new one. Previously most
changes would require duplicating all the nano marshalling code.
diff --git a/compiler/src/java_plugin/cpp/java_generator.cpp b/compiler/src/java_plugin/cpp/java_generator.cpp
index 9daf24c..28fcc37 100644
--- a/compiler/src/java_plugin/cpp/java_generator.cpp
+++ b/compiler/src/java_plugin/cpp/java_generator.cpp
@@ -113,19 +113,17 @@
           "        generateFullMethodName(\n"
           "            \"$Package$$service_name$\", \"$method_name$\"),\n"
           "        $NanoUtils$.<$input_type$>marshaller(\n"
-          "            new io.grpc.protobuf.nano.Parser<$input_type$>() {\n"
+          "            new io.grpc.protobuf.nano.MessageNanoFactory<$input_type$>() {\n"
           "                @Override\n"
-          "                public $input_type$ parse("
-          "$CodedInputByteBufferNano$ input) throws IOException {\n"
-          "                    return $input_type$.parseFrom(input);\n"
+          "                public $input_type$ newInstance() {\n"
+          "                    return new $input_type$();\n"
           "                }\n"
           "        }),\n"
           "        $NanoUtils$.<$output_type$>marshaller(\n"
-          "            new io.grpc.protobuf.nano.Parser<$output_type$>() {\n"
+          "            new io.grpc.protobuf.nano.MessageNanoFactory<$output_type$>() {\n"
           "                @Override\n"
-          "                public $output_type$ parse("
-          "$CodedInputByteBufferNano$ input) throws IOException {\n"
-          "                    return $output_type$.parseFrom(input);\n"
+          "                public $output_type$ newInstance() {\n"
+          "                    return new $output_type$();\n"
           "                }\n"
           "        }));\n");
     } else {
@@ -622,8 +620,6 @@
   vars["Immutable"] = "javax.annotation.concurrent.Immutable";
   vars["ListenableFuture"] =
       "com.google.common.util.concurrent.ListenableFuture";
-  vars["CodedInputByteBufferNano"] = 
-      "com.google.protobuf.nano.CodedInputByteBufferNano";
   vars["ExperimentalApi"] = "io.grpc.ExperimentalApi";
 
   Printer printer(out, '$');
diff --git a/compiler/src/test/golden/TestServiceNano.java.txt b/compiler/src/test/golden/TestServiceNano.java.txt
index 5c05e98..4e631f2 100644
--- a/compiler/src/test/golden/TestServiceNano.java.txt
+++ b/compiler/src/test/golden/TestServiceNano.java.txt
@@ -29,17 +29,17 @@
           generateFullMethodName(
               "grpc.testing.TestService", "UnaryCall"),
           io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.SimpleRequest>marshaller(
-              new io.grpc.protobuf.nano.Parser<io.grpc.testing.integration.nano.Test.SimpleRequest>() {
+              new io.grpc.protobuf.nano.MessageNanoFactory<io.grpc.testing.integration.nano.Test.SimpleRequest>() {
                   @Override
-                  public io.grpc.testing.integration.nano.Test.SimpleRequest parse(com.google.protobuf.nano.CodedInputByteBufferNano input) throws IOException {
-                      return io.grpc.testing.integration.nano.Test.SimpleRequest.parseFrom(input);
+                  public io.grpc.testing.integration.nano.Test.SimpleRequest newInstance() {
+                      return new io.grpc.testing.integration.nano.Test.SimpleRequest();
                   }
           }),
           io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.SimpleResponse>marshaller(
-              new io.grpc.protobuf.nano.Parser<io.grpc.testing.integration.nano.Test.SimpleResponse>() {
+              new io.grpc.protobuf.nano.MessageNanoFactory<io.grpc.testing.integration.nano.Test.SimpleResponse>() {
                   @Override
-                  public io.grpc.testing.integration.nano.Test.SimpleResponse parse(com.google.protobuf.nano.CodedInputByteBufferNano input) throws IOException {
-                      return io.grpc.testing.integration.nano.Test.SimpleResponse.parseFrom(input);
+                  public io.grpc.testing.integration.nano.Test.SimpleResponse newInstance() {
+                      return new io.grpc.testing.integration.nano.Test.SimpleResponse();
                   }
           }));
   @io.grpc.ExperimentalApi
@@ -50,17 +50,17 @@
           generateFullMethodName(
               "grpc.testing.TestService", "StreamingOutputCall"),
           io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>marshaller(
-              new io.grpc.protobuf.nano.Parser<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>() {
+              new io.grpc.protobuf.nano.MessageNanoFactory<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>() {
                   @Override
-                  public io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest parse(com.google.protobuf.nano.CodedInputByteBufferNano input) throws IOException {
-                      return io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest.parseFrom(input);
+                  public io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest newInstance() {
+                      return new io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest();
                   }
           }),
           io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse>marshaller(
-              new io.grpc.protobuf.nano.Parser<io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse>() {
+              new io.grpc.protobuf.nano.MessageNanoFactory<io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse>() {
                   @Override
-                  public io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse parse(com.google.protobuf.nano.CodedInputByteBufferNano input) throws IOException {
-                      return io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse.parseFrom(input);
+                  public io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse newInstance() {
+                      return new io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse();
                   }
           }));
   @io.grpc.ExperimentalApi
@@ -71,17 +71,17 @@
           generateFullMethodName(
               "grpc.testing.TestService", "StreamingInputCall"),
           io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingInputCallRequest>marshaller(
-              new io.grpc.protobuf.nano.Parser<io.grpc.testing.integration.nano.Test.StreamingInputCallRequest>() {
+              new io.grpc.protobuf.nano.MessageNanoFactory<io.grpc.testing.integration.nano.Test.StreamingInputCallRequest>() {
                   @Override
-                  public io.grpc.testing.integration.nano.Test.StreamingInputCallRequest parse(com.google.protobuf.nano.CodedInputByteBufferNano input) throws IOException {
-                      return io.grpc.testing.integration.nano.Test.StreamingInputCallRequest.parseFrom(input);
+                  public io.grpc.testing.integration.nano.Test.StreamingInputCallRequest newInstance() {
+                      return new io.grpc.testing.integration.nano.Test.StreamingInputCallRequest();
                   }
           }),
           io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingInputCallResponse>marshaller(
-              new io.grpc.protobuf.nano.Parser<io.grpc.testing.integration.nano.Test.StreamingInputCallResponse>() {
+              new io.grpc.protobuf.nano.MessageNanoFactory<io.grpc.testing.integration.nano.Test.StreamingInputCallResponse>() {
                   @Override
-                  public io.grpc.testing.integration.nano.Test.StreamingInputCallResponse parse(com.google.protobuf.nano.CodedInputByteBufferNano input) throws IOException {
-                      return io.grpc.testing.integration.nano.Test.StreamingInputCallResponse.parseFrom(input);
+                  public io.grpc.testing.integration.nano.Test.StreamingInputCallResponse newInstance() {
+                      return new io.grpc.testing.integration.nano.Test.StreamingInputCallResponse();
                   }
           }));
   @io.grpc.ExperimentalApi
@@ -92,17 +92,17 @@
           generateFullMethodName(
               "grpc.testing.TestService", "FullBidiCall"),
           io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>marshaller(
-              new io.grpc.protobuf.nano.Parser<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>() {
+              new io.grpc.protobuf.nano.MessageNanoFactory<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>() {
                   @Override
-                  public io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest parse(com.google.protobuf.nano.CodedInputByteBufferNano input) throws IOException {
-                      return io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest.parseFrom(input);
+                  public io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest newInstance() {
+                      return new io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest();
                   }
           }),
           io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse>marshaller(
-              new io.grpc.protobuf.nano.Parser<io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse>() {
+              new io.grpc.protobuf.nano.MessageNanoFactory<io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse>() {
                   @Override
-                  public io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse parse(com.google.protobuf.nano.CodedInputByteBufferNano input) throws IOException {
-                      return io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse.parseFrom(input);
+                  public io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse newInstance() {
+                      return new io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse();
                   }
           }));
   @io.grpc.ExperimentalApi
@@ -113,17 +113,17 @@
           generateFullMethodName(
               "grpc.testing.TestService", "HalfBidiCall"),
           io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>marshaller(
-              new io.grpc.protobuf.nano.Parser<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>() {
+              new io.grpc.protobuf.nano.MessageNanoFactory<io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest>() {
                   @Override
-                  public io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest parse(com.google.protobuf.nano.CodedInputByteBufferNano input) throws IOException {
-                      return io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest.parseFrom(input);
+                  public io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest newInstance() {
+                      return new io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest();
                   }
           }),
           io.grpc.protobuf.nano.NanoUtils.<io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse>marshaller(
-              new io.grpc.protobuf.nano.Parser<io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse>() {
+              new io.grpc.protobuf.nano.MessageNanoFactory<io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse>() {
                   @Override
-                  public io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse parse(com.google.protobuf.nano.CodedInputByteBufferNano input) throws IOException {
-                      return io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse.parseFrom(input);
+                  public io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse newInstance() {
+                      return new io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse();
                   }
           }));
 
diff --git a/protobuf-nano/src/main/java/io/grpc/protobuf/nano/Parser.java b/protobuf-nano/src/main/java/io/grpc/protobuf/nano/MessageNanoFactory.java
similarity index 87%
rename from protobuf-nano/src/main/java/io/grpc/protobuf/nano/Parser.java
rename to protobuf-nano/src/main/java/io/grpc/protobuf/nano/MessageNanoFactory.java
index a82e59f..76892c9 100644
--- a/protobuf-nano/src/main/java/io/grpc/protobuf/nano/Parser.java
+++ b/protobuf-nano/src/main/java/io/grpc/protobuf/nano/MessageNanoFactory.java
@@ -31,16 +31,13 @@
 
 package io.grpc.protobuf.nano;
 
-import com.google.protobuf.nano.CodedInputByteBufferNano;
 import com.google.protobuf.nano.MessageNano;
 
-import java.io.IOException;
-
 /**
- * Parser for parsing nano proto messages.
+ * Produce new message instances. Used by a marshaller to deserialize incoming messages.
  *
  * <p>Should be implemented by generated code.
  */
-public interface Parser<T extends MessageNano> {
-  T parse(CodedInputByteBufferNano input) throws IOException;
+public interface MessageNanoFactory<T extends MessageNano> {
+  T newInstance();
 }
diff --git a/protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java b/protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java
index 9375fc8..43c4b10 100644
--- a/protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java
+++ b/protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java
@@ -49,7 +49,8 @@
   private NanoUtils() {}
 
   /** Adapt {@code parser} to a {@code Marshaller}. */
-  public static <T extends MessageNano> Marshaller<T> marshaller(final Parser<T> parser) {
+  public static <T extends MessageNano> Marshaller<T> marshaller(
+      final MessageNanoFactory<T> factory) {
     return new Marshaller<T>() {
       @Override
       public InputStream stream(T value) {
@@ -63,7 +64,9 @@
           CodedInputByteBufferNano input =
               CodedInputByteBufferNano.newInstance(ByteStreams.toByteArray(stream));
           input.setSizeLimit(Integer.MAX_VALUE);
-          return parser.parse(input);
+          T message = factory.newInstance();
+          message.mergeFrom(input);
+          return message;
         } catch (IOException ipbe) {
           throw Status.INTERNAL.withDescription("Failed parsing nano proto message").withCause(ipbe)
               .asRuntimeException();