From 68f5cc14880478dacfad4f148f15498d84c1669b Mon Sep 17 00:00:00 2001 From: Ryan Case Date: Mon, 4 Jul 2016 14:46:11 -0400 Subject: [PATCH] Use NameAllocator in Message.Builder.build() Use the NameAllocator when inserting string literals, don't use the field name directly. --- .../com/squareup/wire/java/JavaGenerator.java | 5 +++-- .../squareup/wire/java/JavaGeneratorTest.java | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/wire-java-generator/src/main/java/com/squareup/wire/java/JavaGenerator.java b/wire-java-generator/src/main/java/com/squareup/wire/java/JavaGenerator.java index 0e6498b2a5..69f435c1db 100644 --- a/wire-java-generator/src/main/java/com/squareup/wire/java/JavaGenerator.java +++ b/wire-java-generator/src/main/java/com/squareup/wire/java/JavaGenerator.java @@ -1355,9 +1355,10 @@ private MethodSpec builderBuild( for (int i = 0; i < requiredFields.size(); i++) { Field requiredField = requiredFields.get(i); if (i > 0) conditionals.add("\n|| "); - conditionals.add("$L == null", requiredField.name()); + conditionals.add("$L == null", nameAllocator.get(requiredField)); if (i > 0) missingArgs.add(",\n"); - missingArgs.add("$1L, $1S", requiredField.name()); + missingArgs.add("$1L, $2S", nameAllocator.get(requiredField), + requiredField.name()); } result.beginControlFlow("if ($L)", conditionals.add("$]").build()) diff --git a/wire-java-generator/src/test/java/com/squareup/wire/java/JavaGeneratorTest.java b/wire-java-generator/src/test/java/com/squareup/wire/java/JavaGeneratorTest.java index ef7551fc3c..909a047c7f 100644 --- a/wire-java-generator/src/test/java/com/squareup/wire/java/JavaGeneratorTest.java +++ b/wire-java-generator/src/test/java/com/squareup/wire/java/JavaGeneratorTest.java @@ -48,6 +48,26 @@ public final class JavaGeneratorTest { assertThat(JavaGenerator.sanitizeJavadoc(input)).isEqualTo(expected); } + @Test public void generateTypeUsesNameAllocatorInMessageBuilderBuild() { + Schema schema = new SchemaBuilder() + .add("message.proto", "" + + "message Message {\n" + + " required float long = 1;\n" + + "}\n") + .build(); + MessageType message = (MessageType) schema.getType("Message"); + JavaGenerator javaGenerator = JavaGenerator.get(schema); + TypeSpec typeSpec = javaGenerator.generateType(message); + assertThat(JavaFile.builder("com.squareup.message", typeSpec).build().toString()).contains("" + + " @Override\n" + + " public Message build() {\n" + + " if (long_ == null) {\n" + + " throw Internal.missingRequiredFields(long_, \"long\");\n" + + " }\n" + + " return new Message(long_, super.buildUnknownFields());\n" + + " }\n"); + } + @Test public void generateProtoFields() { Schema schema = new SchemaBuilder() .add("message.proto", ""