summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Duff <bduff@google.com>2015-01-09 22:31:42 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2015-01-09 22:31:43 +0000
commitc7c25812eb19d080087b71e08bfe35aff9f21433 (patch)
tree55c02e666ed6f56c3ac9c337cd63e3ac2d6d66e8
parent25794277fd65aabe917fc6c01b3162e243740634 (diff)
parent5537d6fe4af105eb769bacc530e8d5d9e0b027cb (diff)
downloadexternal_protobuf-c7c25812eb19d080087b71e08bfe35aff9f21433.zip
external_protobuf-c7c25812eb19d080087b71e08bfe35aff9f21433.tar.gz
external_protobuf-c7c25812eb19d080087b71e08bfe35aff9f21433.tar.bz2
Merge "When no clear() is generated, still initialize fields."
-rw-r--r--java/pom.xml6
-rw-r--r--java/src/test/java/com/google/protobuf/NanoTest.java6
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_message.cc32
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_message.h1
4 files changed, 33 insertions, 12 deletions
diff --git a/java/pom.xml b/java/pom.xml
index a673f2d..6c7191c 100644
--- a/java/pom.xml
+++ b/java/pom.xml
@@ -211,6 +211,12 @@
<arg value="--proto_path=src/test/java" />
<arg value="../src/google/protobuf/unittest_reference_types_nano.proto" />
</exec>
+ <exec executable="../src/protoc">
+ <arg value="--javanano_out=optional_field_style=reftypes_compat_mode,generate_equals=true,java_outer_classname=google/protobuf/unittest_reference_types_nano.proto|NanoReferenceTypesCompat:target/generated-test-sources" />
+ <arg value="--proto_path=../src" />
+ <arg value="--proto_path=src/test/java" />
+ <arg value="../src/google/protobuf/unittest_reference_types_nano.proto" />
+ </exec>
</tasks>
<testSourceRoot>target/generated-test-sources</testSourceRoot>
<!--testSourceRoot>target/generated-test-sources/opt-space</testSourceRoot-->
diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java
index 8d364c1..4c3b416 100644
--- a/java/src/test/java/com/google/protobuf/NanoTest.java
+++ b/java/src/test/java/com/google/protobuf/NanoTest.java
@@ -49,6 +49,7 @@ import com.google.protobuf.nano.NanoHasOuterClass.TestAllTypesNanoHas;
import com.google.protobuf.nano.NanoOuterClass;
import com.google.protobuf.nano.NanoOuterClass.TestAllTypesNano;
import com.google.protobuf.nano.NanoReferenceTypes;
+import com.google.protobuf.nano.NanoReferenceTypesCompat;
import com.google.protobuf.nano.NanoRepeatedPackables;
import com.google.protobuf.nano.PackedExtensions;
import com.google.protobuf.nano.RepeatedExtensions;
@@ -3793,6 +3794,11 @@ public class NanoTest extends TestCase {
assertTrue(Arrays.equals(new boolean[] {false, true, false, true}, nonPacked.bools));
}
+ public void testRepeatedFieldInitializedInReftypesCompatMode() {
+ NanoReferenceTypesCompat.TestAllTypesNano proto = new NanoReferenceTypesCompat.TestAllTypesNano();
+ assertNotNull(proto.repeatedString);
+ }
+
private void assertRepeatedPackablesEqual(
NanoRepeatedPackables.NonPacked nonPacked, NanoRepeatedPackables.Packed packed) {
// Not using MessageNano.equals() -- that belongs to a separate test.
diff --git a/src/google/protobuf/compiler/javanano/javanano_message.cc b/src/google/protobuf/compiler/javanano/javanano_message.cc
index 03c7e18..dcfb870 100644
--- a/src/google/protobuf/compiler/javanano/javanano_message.cc
+++ b/src/google/protobuf/compiler/javanano/javanano_message.cc
@@ -256,14 +256,18 @@ void MessageGenerator::Generate(io::Printer* printer) {
}
printer->Print("}\n");
} else {
+ printer->Print(
+ "\n"
+ "public $classname$() {\n",
+ "classname", descriptor_->name());
if (params_.generate_clear()) {
- printer->Print(
- "\n"
- "public $classname$() {\n"
- " clear();\n"
- "}\n",
- "classname", descriptor_->name());
+ printer->Print(" clear();\n");
+ } else {
+ printer->Indent();
+ GenerateFieldInitializers(printer);
+ printer->Outdent();
}
+ printer->Print("}\n");
}
// Other methods in this class
@@ -462,6 +466,15 @@ void MessageGenerator::GenerateClear(io::Printer* printer) {
"classname", descriptor_->name());
printer->Indent();
+ GenerateFieldInitializers(printer);
+
+ printer->Outdent();
+ printer->Print(
+ " return this;\n"
+ "}\n");
+}
+
+void MessageGenerator::GenerateFieldInitializers(io::Printer* printer) {
// Clear bit fields.
int totalInts = (field_generators_.total_bits() + 31) / 32;
for (int i = 0; i < totalInts; i++) {
@@ -479,12 +492,7 @@ void MessageGenerator::GenerateClear(io::Printer* printer) {
if (params_.store_unknown_fields()) {
printer->Print("unknownFieldData = null;\n");
}
-
- printer->Outdent();
- printer->Print(
- " cachedSize = -1;\n"
- " return this;\n"
- "}\n");
+ printer->Print("cachedSize = -1;\n");
}
void MessageGenerator::GenerateClone(io::Printer* printer) {
diff --git a/src/google/protobuf/compiler/javanano/javanano_message.h b/src/google/protobuf/compiler/javanano/javanano_message.h
index 870f3bb..1bb6557 100644
--- a/src/google/protobuf/compiler/javanano/javanano_message.h
+++ b/src/google/protobuf/compiler/javanano/javanano_message.h
@@ -77,6 +77,7 @@ class MessageGenerator {
const FieldDescriptor* field);
void GenerateClear(io::Printer* printer);
+ void GenerateFieldInitializers(io::Printer* printer);
void GenerateEquals(io::Printer* printer);
void GenerateHashCode(io::Printer* printer);
void GenerateClone(io::Printer* printer);