Merge "Feature request: set() and clear() accessors return this"

pull/91/head
Max Cai 11 years ago committed by Gerrit Code Review
commit 461d4ac87a
  1. 6
      java/src/test/java/com/google/protobuf/NanoTest.java
  2. 13
      src/google/protobuf/compiler/javanano/javanano_enum_field.cc
  3. 14
      src/google/protobuf/compiler/javanano/javanano_message_field.cc
  4. 17
      src/google/protobuf/compiler/javanano/javanano_primitive_field.cc

@ -2322,6 +2322,12 @@ public class NanoTest extends TestCase {
assertFalse(msg.hasOptionalString()); assertFalse(msg.hasOptionalString());
assertFalse(msg.hasDefaultString()); assertFalse(msg.hasDefaultString());
assertFalse(msg.hasBitFieldCheck()); assertFalse(msg.hasBitFieldCheck());
// Test set() and clear() returns itself (compiles = success)
msg.clear()
.setOptionalInt32(3)
.clearDefaultBytes()
.setOptionalString("4");
} }
public void testNanoWithAccessorsParseFrom() throws Exception { public void testNanoWithAccessorsParseFrom() throws Exception {

@ -182,16 +182,18 @@ GenerateMembers(io::Printer* printer) const {
"public int get$capitalized_name$() {\n" "public int get$capitalized_name$() {\n"
" return $name$_;\n" " return $name$_;\n"
"}\n" "}\n"
"public void set$capitalized_name$(int value) {\n" "public $message_name$ set$capitalized_name$(int value) {\n"
" $name$_ = value;\n" " $name$_ = value;\n"
" $set_has$;\n" " $set_has$;\n"
" return this;\n"
"}\n" "}\n"
"public boolean has$capitalized_name$() {\n" "public boolean has$capitalized_name$() {\n"
" return $get_has$;\n" " return $get_has$;\n"
"}\n" "}\n"
"public void clear$capitalized_name$() {\n" "public $message_name$ clear$capitalized_name$() {\n"
" $name$_ = $default$;\n" " $name$_ = $default$;\n"
" $clear_has$;\n" " $clear_has$;\n"
" return this;\n"
"}\n"); "}\n");
} }
@ -204,13 +206,14 @@ GenerateClearCode(io::Printer* printer) const {
void AccessorEnumFieldGenerator:: void AccessorEnumFieldGenerator::
GenerateMergingCode(io::Printer* printer) const { GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"set$capitalized_name$(input.readInt32());\n"); "$name$_ = input.readInt32();\n"
"$set_has$;\n");
} }
void AccessorEnumFieldGenerator:: void AccessorEnumFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const { GenerateSerializationCode(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"if (has$capitalized_name$()) {\n" "if ($get_has$) {\n"
" output.writeInt32($number$, $name$_);\n" " output.writeInt32($number$, $name$_);\n"
"}\n"); "}\n");
} }
@ -218,7 +221,7 @@ GenerateSerializationCode(io::Printer* printer) const {
void AccessorEnumFieldGenerator:: void AccessorEnumFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const { GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"if (has$capitalized_name$()) {\n" "if ($get_has$) {\n"
" size += com.google.protobuf.nano.CodedOutputByteBufferNano\n" " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .computeInt32Size($number$, $name$_);\n" " .computeInt32Size($number$, $name$_);\n"
"}\n"); "}\n");

@ -148,17 +148,19 @@ GenerateMembers(io::Printer* printer) const {
"public $type$ get$capitalized_name$() {\n" "public $type$ get$capitalized_name$() {\n"
" return $name$_;\n" " return $name$_;\n"
"}\n" "}\n"
"public void set$capitalized_name$($type$ value) {\n" "public $message_name$ set$capitalized_name$($type$ value) {\n"
" if (value == null) {\n" " if (value == null) {\n"
" throw new java.lang.NullPointerException();\n" " throw new java.lang.NullPointerException();\n"
" }\n" " }\n"
" $name$_ = value;\n" " $name$_ = value;\n"
" return this;\n"
"}\n" "}\n"
"public boolean has$capitalized_name$() {\n" "public boolean has$capitalized_name$() {\n"
" return $name$_ != null;\n" " return $name$_ != null;\n"
"}\n" "}\n"
"public void clear$capitalized_name$() {\n" "public $message_name$ clear$capitalized_name$() {\n"
" $name$_ = null;\n" " $name$_ = null;\n"
" return this;"
"}\n"); "}\n");
} }
@ -171,8 +173,8 @@ GenerateClearCode(io::Printer* printer) const {
void AccessorMessageFieldGenerator:: void AccessorMessageFieldGenerator::
GenerateMergingCode(io::Printer* printer) const { GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"if (!has$capitalized_name$()) {\n" "if ($name$_ == null) {\n"
" set$capitalized_name$(new $type$());\n" " $name$_ = new $type$();\n"
"}\n"); "}\n");
if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) { if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
@ -187,7 +189,7 @@ GenerateMergingCode(io::Printer* printer) const {
void AccessorMessageFieldGenerator:: void AccessorMessageFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const { GenerateSerializationCode(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"if (has$capitalized_name$()) {\n" "if ($name$_ != null) {\n"
" output.write$group_or_message$($number$, $name$_);\n" " output.write$group_or_message$($number$, $name$_);\n"
"}\n"); "}\n");
} }
@ -195,7 +197,7 @@ GenerateSerializationCode(io::Printer* printer) const {
void AccessorMessageFieldGenerator:: void AccessorMessageFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const { GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"if (has$capitalized_name$()) {\n" "if ($name$_ != null) {\n"
" size += com.google.protobuf.nano.CodedOutputByteBufferNano\n" " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .compute$group_or_message$Size($number$, $name$_);\n" " .compute$group_or_message$Size($number$, $name$_);\n"
"}\n"); "}\n");

@ -431,21 +431,25 @@ GenerateMembers(io::Printer* printer) const {
"public $type$ get$capitalized_name$() {\n" "public $type$ get$capitalized_name$() {\n"
" return $name$_;\n" " return $name$_;\n"
"}\n" "}\n"
"public void set$capitalized_name$($type$ value) {\n"); "public $message_name$ set$capitalized_name$($type$ value) {\n");
if (IsReferenceType(GetJavaType(descriptor_))) { if (IsReferenceType(GetJavaType(descriptor_))) {
printer->Print(variables_, printer->Print(variables_,
" if (value == null) throw new java.lang.NullPointerException();\n"); " if (value == null) {\n"
" throw new java.lang.NullPointerException();\n"
" }\n");
} }
printer->Print(variables_, printer->Print(variables_,
" $name$_ = value;\n" " $name$_ = value;\n"
" $set_has$;\n" " $set_has$;\n"
" return this;\n"
"}\n" "}\n"
"public boolean has$capitalized_name$() {\n" "public boolean has$capitalized_name$() {\n"
" return $get_has$;\n" " return $get_has$;\n"
"}\n" "}\n"
"public void clear$capitalized_name$() {\n" "public $message_name$ clear$capitalized_name$() {\n"
" $name$_ = $default_copy_if_needed$;\n" " $name$_ = $default_copy_if_needed$;\n"
" $clear_has$;\n" " $clear_has$;\n"
" return this;\n"
"}\n"); "}\n");
} }
@ -458,13 +462,14 @@ GenerateClearCode(io::Printer* printer) const {
void AccessorPrimitiveFieldGenerator:: void AccessorPrimitiveFieldGenerator::
GenerateMergingCode(io::Printer* printer) const { GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"set$capitalized_name$(input.read$capitalized_type$());\n"); "$name$_ = input.read$capitalized_type$();\n"
"$set_has$;\n");
} }
void AccessorPrimitiveFieldGenerator:: void AccessorPrimitiveFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const { GenerateSerializationCode(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"if (has$capitalized_name$()) {\n" "if ($get_has$) {\n"
" output.write$capitalized_type$($number$, $name$_);\n" " output.write$capitalized_type$($number$, $name$_);\n"
"}\n"); "}\n");
} }
@ -472,7 +477,7 @@ GenerateSerializationCode(io::Printer* printer) const {
void AccessorPrimitiveFieldGenerator:: void AccessorPrimitiveFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const { GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"if (has$capitalized_name$()) {\n" "if ($get_has$) {\n"
" size += com.google.protobuf.nano.CodedOutputByteBufferNano\n" " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .compute$capitalized_type$Size($number$, $name$_);\n" " .compute$capitalized_type$Size($number$, $name$_);\n"
"}\n"); "}\n");

Loading…
Cancel
Save