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