aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/compiler/java/java_enum_field_lite.cc
diff options
context:
space:
mode:
authorGravatar Jisi Liu <jisi.liu@gmail.com>2016-04-28 14:34:59 -0700
committerGravatar Jisi Liu <jisi.liu@gmail.com>2016-04-28 14:34:59 -0700
commitcf14183bcd5485b4a71541599ddce0b35eb71352 (patch)
tree12f6e5eb731d7a70cdac4cdafc8b3131629413e2 /src/google/protobuf/compiler/java/java_enum_field_lite.cc
parentf00300d7f04f1c38a7d70e271f9232b94dd0e326 (diff)
Down integrate from Google internal.
Diffstat (limited to 'src/google/protobuf/compiler/java/java_enum_field_lite.cc')
-rw-r--r--src/google/protobuf/compiler/java/java_enum_field_lite.cc91
1 files changed, 44 insertions, 47 deletions
diff --git a/src/google/protobuf/compiler/java/java_enum_field_lite.cc b/src/google/protobuf/compiler/java/java_enum_field_lite.cc
index 5b98637b..908d6db4 100644
--- a/src/google/protobuf/compiler/java/java_enum_field_lite.cc
+++ b/src/google/protobuf/compiler/java/java_enum_field_lite.cc
@@ -262,17 +262,15 @@ GenerateInitializationCode(io::Printer* printer) const {
}
void ImmutableEnumFieldLiteGenerator::
-GenerateMergingCode(io::Printer* printer) const {
+GenerateVisitCode(io::Printer* printer) const {
if (SupportFieldPresence(descriptor_->file())) {
printer->Print(variables_,
- "if (other.has$capitalized_name$()) {\n"
- " set$capitalized_name$(other.get$capitalized_name$());\n"
- "}\n");
+ "$name$_ = visitor.visitInt(has$capitalized_name$(), $name$_,\n"
+ " other.has$capitalized_name$(), other.$name$_);\n");
} else if (SupportUnknownEnumValue(descriptor_->file())) {
printer->Print(variables_,
- "if (other.$name$_ != $default_number$) {\n"
- " set$capitalized_name$Value(other.get$capitalized_name$Value());\n"
- "}\n");
+ "$name$_ = visitor.visitInt($name$_ != $default_number$, $name$_,"
+ " other.$name$_ != $default_number$, other.$name$_);\n");
} else {
GOOGLE_LOG(FATAL) << "Can't reach here.";
}
@@ -466,14 +464,10 @@ GenerateBuilderMembers(io::Printer* printer) const {
}
void ImmutableEnumOneofFieldLiteGenerator::
-GenerateMergingCode(io::Printer* printer) const {
- if (SupportUnknownEnumValue(descriptor_->file())) {
- printer->Print(variables_,
- "set$capitalized_name$Value(other.get$capitalized_name$Value());\n");
- } else {
- printer->Print(variables_,
- "set$capitalized_name$(other.get$capitalized_name$());\n");
- }
+GenerateVisitCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "$oneof_name$_ = visitor.visitOneofInt(\n"
+ " $has_oneof_case_message$, $oneof_name$_, other.$oneof_name$_);\n");
}
void ImmutableEnumOneofFieldLiteGenerator::
@@ -645,7 +639,8 @@ GenerateMembers(io::Printer* printer) const {
printer->Print(variables_,
"private void ensure$capitalized_name$IsMutable() {\n"
" if (!$is_mutable$) {\n"
- " $name$_ = newIntList($name$_);\n"
+ " $name$_ =\n"
+ " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n"
" }\n"
"}\n");
WriteFieldDocComment(printer, descriptor_);
@@ -805,21 +800,9 @@ GenerateInitializationCode(io::Printer* printer) const {
}
void RepeatedImmutableEnumFieldLiteGenerator::
-GenerateMergingCode(io::Printer* printer) const {
- // The code below does two optimizations:
- // 1. If the other list is empty, there's nothing to do. This ensures we
- // don't allocate a new array if we already have an immutable one.
- // 2. If the other list is non-empty and our current list is empty, we can
- // reuse the other list which is guaranteed to be immutable.
- printer->Print(variables_,
- "if (!other.$name$_.isEmpty()) {\n"
- " if ($name$_.isEmpty()) {\n"
- " $name$_ = other.$name$_;\n"
- " } else {\n"
- " ensure$capitalized_name$IsMutable();\n"
- " $name$_.addAll(other.$name$_);\n"
- " }\n"
- "}\n");
+GenerateVisitCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "$name$_= visitor.visitIntList($name$_, other.$name$_);\n");
}
void RepeatedImmutableEnumFieldLiteGenerator::
@@ -831,27 +814,23 @@ GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const {
void RepeatedImmutableEnumFieldLiteGenerator::
GenerateParsingCode(io::Printer* printer) const {
// Read and store the enum
+ printer->Print(variables_,
+ "if (!$is_mutable$) {\n"
+ " $name$_ =\n"
+ " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n"
+ "}\n");
+
if (SupportUnknownEnumValue(descriptor_->file())) {
printer->Print(variables_,
- "int rawValue = input.readEnum();\n"
- "if (!$is_mutable$) {\n"
- " $name$_ = newIntList();\n"
- "}\n"
- "$name$_.addInt(rawValue);\n");
+ "$name$_.addInt(input.readEnum());\n");
} else {
printer->Print(variables_,
"int rawValue = input.readEnum();\n"
"$type$ value = $type$.forNumber(rawValue);\n"
- "if (value == null) {\n");
- if (PreserveUnknownFields(descriptor_->containing_type())) {
- printer->Print(variables_,
- " super.mergeVarintField($number$, rawValue);\n");
- }
- printer->Print(variables_,
+ "if (value == null) {\n"
+ // We store the unknown value in unknown fields.
+ " super.mergeVarintField($number$, rawValue);\n"
"} else {\n"
- " if (!$is_mutable$) {\n"
- " $name$_ = newIntList();\n"
- " }\n"
" $name$_.addInt(rawValue);\n"
"}\n");
}
@@ -859,7 +838,11 @@ GenerateParsingCode(io::Printer* printer) const {
void RepeatedImmutableEnumFieldLiteGenerator::
GenerateParsingCodeFromPacked(io::Printer* printer) const {
- // Wrap GenerateParsingCode's contents with a while loop.
+ printer->Print(variables_,
+ "if (!$is_mutable$) {\n"
+ " $name$_ =\n"
+ " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n"
+ "}\n");
printer->Print(variables_,
"int length = input.readRawVarint32();\n"
@@ -867,7 +850,21 @@ GenerateParsingCodeFromPacked(io::Printer* printer) const {
"while(input.getBytesUntilLimit() > 0) {\n");
printer->Indent();
- GenerateParsingCode(printer);
+ // Read and store the enum
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ printer->Print(variables_,
+ "$name$_.addInt(input.readEnum());\n");
+ } else {
+ printer->Print(variables_,
+ "int rawValue = input.readEnum();\n"
+ "$type$ value = $type$.forNumber(rawValue);\n"
+ "if (value == null) {\n"
+ // We store the unknown value in unknown fields.
+ " super.mergeVarintField($number$, rawValue);\n"
+ "} else {\n"
+ " $name$_.addInt(rawValue);\n"
+ "}\n");
+ }
printer->Outdent();
printer->Print(variables_,