Commit c72d3d51 authored by Jisi Liu's avatar Jisi Liu

Oneof message level elements (consts, case getter/clear) and messsage type field oneof support.

parent 7794a98f
......@@ -170,7 +170,7 @@ message TestAllTypesNano {
oneof oneof_field {
uint32 oneof_uint32 = 111;
// NestedMessage oneof_nested_message = 112;
NestedMessage oneof_nested_message = 112;
// string oneof_string = 123;
// bytes oneof_bytes = 124;
fixed64 oneof_fixed64 = 115;
......
......@@ -159,11 +159,13 @@ void SetCommonOneofVariables(const FieldDescriptor* descriptor,
(*variables)["oneof_index"] =
SimpleItoa(descriptor->containing_oneof()->index());
(*variables)["set_oneof_case"] =
(*variables)["oneof_name"] + "Case_ = " + SimpleItoa(descriptor->number());
"this." + (*variables)["oneof_name"] +
"Case_ = " + SimpleItoa(descriptor->number());
(*variables)["clear_oneof_case"] =
(*variables)["oneof_name"] + "Case_ = 0";
"this." + (*variables)["oneof_name"] + "Case_ = 0";
(*variables)["has_oneof_case"] =
(*variables)["oneof_name"] + "Case_ == " + SimpleItoa(descriptor->number());
"this." + (*variables)["oneof_name"] + "Case_ == " +
SimpleItoa(descriptor->number());
}
} // namespace javanano
......
......@@ -167,16 +167,36 @@ void MessageGenerator::Generate(io::Printer* printer) {
// oneof
map<string, string> vars;
vars["message_name"] = descriptor_->name();
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
vars["oneof_name"] = UnderscoresToCamelCase(descriptor_->oneof_decl(i));
const OneofDescriptor* oneof_desc = descriptor_->oneof_decl(i);
vars["oneof_name"] = UnderscoresToCamelCase(oneof_desc);
vars["oneof_capitalized_name"] =
UnderscoresToCapitalizedCamelCase(descriptor_->oneof_decl(i));
vars["oneof_index"] = SimpleItoa(descriptor_->oneof_decl(i)->index());
UnderscoresToCapitalizedCamelCase(oneof_desc);
vars["oneof_index"] = SimpleItoa(oneof_desc->index());
// Oneof Constants
for (int j = 0; j < oneof_desc->field_count(); j++) {
const FieldDescriptor* field = oneof_desc->field(j);
vars["number"] = SimpleItoa(field->number());
vars["cap_field_name"] = ToUpper(field->name());
printer->Print(vars,
"public static final int $cap_field_name$_FIELD_NUMBER = $number$;\n");
}
// oneofCase_ and oneof_
printer->Print(vars,
"private int $oneof_name$Case_ = 0;\n"
"private java.lang.Object $oneof_name$_;\n");
// OneofCase enum
printer->Print(vars,
"public int get$oneof_capitalized_name$Case() {\n"
" return this.$oneof_name$Case_;\n"
"}\n");
// Oneof clear
printer->Print(vars,
"public $message_name$ clear$oneof_capitalized_name$() {\n"
" this.$oneof_name$Case_ = 0;\n"
" this.$oneof_name$_ = null;\n"
" return this;\n"
"}\n");
}
// Lazy initialization of otherwise static final fields can help prevent the
......
......@@ -157,6 +157,69 @@ MessageOneofFieldGenerator::MessageOneofFieldGenerator(
MessageOneofFieldGenerator::~MessageOneofFieldGenerator() {}
void MessageOneofFieldGenerator::
GenerateMembers(io::Printer* printer, bool /* unused lazy_init */) const {
printer->Print(variables_,
"public boolean has$capitalized_name$() {\n"
" return $has_oneof_case$;\n"
"}\n"
"public $type$ get$capitalized_name$() {\n"
" if ($has_oneof_case$) {\n"
" return ($type$) this.$oneof_name$_;\n"
" }\n"
" return null;\n"
"}\n"
"public $message_name$ set$capitalized_name$($type$ value) {\n"
" if (value == null) { throw new java.lang.NullPointerException(); }\n"
" $set_oneof_case$;\n"
" this.$oneof_name$_ = value;\n"
" return this;\n"
"}\n");
}
void MessageOneofFieldGenerator::
GenerateClearCode(io::Printer* printer) const {
// No clear method for oneof fields.
}
void MessageOneofFieldGenerator::
GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_,
"if (!($has_oneof_case$)) {\n"
" this.$oneof_name$_ = new $type$();\n"
"}\n"
"input.readMessage(\n"
" (com.google.protobuf.nano.MessageNano) this.$oneof_name$_);\n"
"$set_oneof_case$;\n");
}
void MessageOneofFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const {
printer->Print(variables_,
"if ($has_oneof_case$) {\n"
" output.writeMessage($number$,\n"
" (com.google.protobuf.nano.MessageNano) this.$oneof_name$_);\n"
"}\n");
}
void MessageOneofFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
"if ($has_oneof_case$) {\n"
" size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .computeMessageSize($number$,\n"
" (com.google.protobuf.nano.MessageNano) this.$oneof_name$_);\n"
"}\n");
}
void MessageOneofFieldGenerator::
GenerateEqualsCode(io::Printer* printer) const {
}
void MessageOneofFieldGenerator::
GenerateHashCodeCode(io::Printer* printer) const {
}
// ===================================================================
RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator(
......
......@@ -73,6 +73,14 @@ class MessageOneofFieldGenerator : public FieldGenerator {
~MessageOneofFieldGenerator();
// implements FieldGenerator ---------------------------------------
void GenerateMembers(io::Printer* printer, bool lazy_init) const;
void GenerateClearCode(io::Printer* printer) const;
void GenerateMergingCode(io::Printer* printer) const;
void GenerateSerializationCode(io::Printer* printer) const;
void GenerateSerializedSizeCode(io::Printer* printer) const;
void GenerateEqualsCode(io::Printer* printer) const;
void GenerateHashCodeCode(io::Printer* printer) const;
private:
const FieldDescriptor* descriptor_;
map<string, string> variables_;
......
......@@ -723,18 +723,13 @@ void PrimitiveOneofFieldGenerator::GenerateMembers(
"}\n"
"public $type$ get$capitalized_name$() {\n"
" if ($has_oneof_case$) {\n"
" return ($type$) ($boxed_type$) $oneof_name$_;\n"
" return ($type$) ($boxed_type$) this.$oneof_name$_;\n"
" }\n"
" return $default$;\n"
"}\n"
"public $message_name$ set$capitalized_name$($type$ value) {\n"
" $set_oneof_case$;\n"
" $oneof_name$_ = value;\n"
" return this;\n"
"}\n"
"public $message_name$ clear$capitalized_name$() {\n"
" $clear_oneof_case$;\n"
" $oneof_name$_ = null;\n"
" this.$oneof_name$_ = value;\n"
" return this;\n"
"}\n");
}
......@@ -747,7 +742,7 @@ void PrimitiveOneofFieldGenerator::GenerateClearCode(
void PrimitiveOneofFieldGenerator::GenerateMergingCode(
io::Printer* printer) const {
printer->Print(variables_,
"$oneof_name$_ = input.read$capitalized_type$();\n"
"this.$oneof_name$_ = input.read$capitalized_type$();\n"
"$set_oneof_case$;\n");
}
......@@ -756,7 +751,7 @@ void PrimitiveOneofFieldGenerator::GenerateSerializationCode(
printer->Print(variables_,
"if ($has_oneof_case$) {\n"
" output.write$capitalized_type$(\n"
" $number$, ($boxed_type$) $oneof_name$_);\n"
" $number$, ($boxed_type$) this.$oneof_name$_);\n"
"}\n");
}
......@@ -766,7 +761,7 @@ void PrimitiveOneofFieldGenerator::GenerateSerializedSizeCode(
"if ($has_oneof_case$) {\n"
" size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .compute$capitalized_type$Size(\n"
" $number$, ($boxed_type$) $oneof_name$_);\n"
" $number$, ($boxed_type$) this.$oneof_name$_);\n"
"}\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