Commit 10040938 authored by Max Cai's avatar Max Cai Committed by Gerrit Code Review

Merge "Make generated code more aligned with Google Java style."

parents 665d99f4 9a93c5f5
...@@ -69,6 +69,7 @@ EnumGenerator::~EnumGenerator() {} ...@@ -69,6 +69,7 @@ EnumGenerator::~EnumGenerator() {}
void EnumGenerator::Generate(io::Printer* printer) { void EnumGenerator::Generate(io::Printer* printer) {
printer->Print( printer->Print(
"\n"
"// enum $classname$\n", "// enum $classname$\n",
"classname", descriptor_->name()); "classname", descriptor_->name());
...@@ -102,7 +103,6 @@ void EnumGenerator::Generate(io::Printer* printer) { ...@@ -102,7 +103,6 @@ void EnumGenerator::Generate(io::Printer* printer) {
printer->Outdent(); printer->Outdent();
printer->Print("}\n"); printer->Print("}\n");
} }
printer->Print("\n");
} }
} // namespace javanano } // namespace javanano
......
...@@ -111,11 +111,11 @@ GenerateClearCode(io::Printer* printer) const { ...@@ -111,11 +111,11 @@ GenerateClearCode(io::Printer* printer) const {
void EnumFieldGenerator:: void EnumFieldGenerator::
GenerateMergingCode(io::Printer* printer) const { GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
" this.$name$ = input.readInt32();\n"); "this.$name$ = input.readInt32();\n");
if (params_.generate_has()) { if (params_.generate_has()) {
printer->Print(variables_, printer->Print(variables_,
" has$capitalized_name$ = true;\n"); "has$capitalized_name$ = true;\n");
} }
} }
...@@ -281,8 +281,8 @@ GenerateMergingCode(io::Printer* printer) const { ...@@ -281,8 +281,8 @@ GenerateMergingCode(io::Printer* printer) const {
"input.popLimit(limit);\n"); "input.popLimit(limit);\n");
} else { } else {
printer->Print(variables_, printer->Print(variables_,
"int arrayLength = com.google.protobuf.nano.WireFormatNano.\n" "int arrayLength = com.google.protobuf.nano.WireFormatNano\n"
" getRepeatedFieldArrayLength(input, $tag$);\n" " .getRepeatedFieldArrayLength(input, $tag$);\n"
"int i = this.$name$ == null ? 0 : this.$name$.length;\n" "int i = this.$name$ == null ? 0 : this.$name$.length;\n"
"int[] newArray = new int[i + arrayLength];\n" "int[] newArray = new int[i + arrayLength];\n"
"if (i != 0) {\n" "if (i != 0) {\n"
......
...@@ -77,13 +77,15 @@ ExtensionGenerator::~ExtensionGenerator() {} ...@@ -77,13 +77,15 @@ ExtensionGenerator::~ExtensionGenerator() {}
void ExtensionGenerator::Generate(io::Printer* printer) const { void ExtensionGenerator::Generate(io::Printer* printer) const {
if (descriptor_->is_repeated()) { if (descriptor_->is_repeated()) {
printer->Print(variables_, printer->Print(variables_,
"// Extends $extends$\n" "\n"
"// extends $extends$\n"
"public static final com.google.protobuf.nano.Extension<java.util.List<$type$>> $name$ = \n" "public static final com.google.protobuf.nano.Extension<java.util.List<$type$>> $name$ = \n"
" com.google.protobuf.nano.Extension.createRepeated($number$,\n" " com.google.protobuf.nano.Extension.createRepeated($number$,\n"
" new com.google.protobuf.nano.Extension.TypeLiteral<java.util.List<$type$>>(){});\n"); " new com.google.protobuf.nano.Extension.TypeLiteral<java.util.List<$type$>>(){});\n");
} else { } else {
printer->Print(variables_, printer->Print(variables_,
"// Extends $extends$\n" "\n"
"// extends $extends$\n"
"public static final com.google.protobuf.nano.Extension<$type$> $name$ =\n" "public static final com.google.protobuf.nano.Extension<$type$> $name$ =\n"
" com.google.protobuf.nano.Extension.create($number$,\n" " com.google.protobuf.nano.Extension.create($number$,\n"
" new com.google.protobuf.nano.Extension.TypeLiteral<$type$>(){});\n"); " new com.google.protobuf.nano.Extension.TypeLiteral<$type$>(){});\n");
......
...@@ -163,17 +163,22 @@ void FileGenerator::Generate(io::Printer* printer) { ...@@ -163,17 +163,22 @@ void FileGenerator::Generate(io::Printer* printer) {
// We don't import anything because we refer to all classes by their // We don't import anything because we refer to all classes by their
// fully-qualified names in the generated source. // fully-qualified names in the generated source.
printer->Print( printer->Print(
"// Generated by the protocol buffer compiler. DO NOT EDIT!\n" "// Generated by the protocol buffer compiler. DO NOT EDIT!\n");
"\n");
if (!java_package_.empty()) { if (!java_package_.empty()) {
printer->Print( printer->Print(
"package $package$;\n" "\n"
"\n", "package $package$;\n",
"package", java_package_); "package", java_package_);
} }
// Note: constants (from enums, emitted in the loop below) may have the same names as constants
// in the nested classes. This causes Java warnings, but is not fatal, so we suppress those
// warnings here in the top-most class declaration.
printer->Print( printer->Print(
"\n"
"@SuppressWarnings(\"hiding\")\n"
"public final class $classname$ {\n" "public final class $classname$ {\n"
" \n"
" private $classname$() {}\n", " private $classname$() {}\n",
"classname", classname_); "classname", classname_);
printer->Indent(); printer->Indent();
...@@ -223,12 +228,11 @@ static void GenerateSibling(const string& package_dir, ...@@ -223,12 +228,11 @@ static void GenerateSibling(const string& package_dir,
io::Printer printer(output.get(), '$'); io::Printer printer(output.get(), '$');
printer.Print( printer.Print(
"// Generated by the protocol buffer compiler. DO NOT EDIT!\n" "// Generated by the protocol buffer compiler. DO NOT EDIT!\n");
"\n");
if (!java_package.empty()) { if (!java_package.empty()) {
printer.Print( printer.Print(
"package $package$;\n" "\n"
"\n", "package $package$;\n",
"package", java_package); "package", java_package);
} }
......
...@@ -121,37 +121,47 @@ void MessageGenerator::GenerateStaticVariableInitializers( ...@@ -121,37 +121,47 @@ void MessageGenerator::GenerateStaticVariableInitializers(
} }
void MessageGenerator::Generate(io::Printer* printer) { void MessageGenerator::Generate(io::Printer* printer) {
const string& file_name = descriptor_->file()->name();
bool is_own_file =
params_.java_multiple_files(file_name)
&& descriptor_->containing_type() == NULL;
if (!params_.store_unknown_fields() && if (!params_.store_unknown_fields() &&
(descriptor_->extension_count() != 0 || descriptor_->extension_range_count() != 0)) { (descriptor_->extension_count() != 0 || descriptor_->extension_range_count() != 0)) {
GOOGLE_LOG(FATAL) << "Extensions are only supported in NANO_RUNTIME if the " GOOGLE_LOG(FATAL) << "Extensions are only supported in NANO_RUNTIME if the "
"'store_unknown_fields' generator option is 'true'\n"; "'store_unknown_fields' generator option is 'true'\n";
} }
// Note: Fields (which will be emitted in the loop, below) may have the same names as fields in const string& file_name = descriptor_->file()->name();
// the inner or outer class. This causes Java warnings, but is not fatal, so we suppress those bool is_own_file =
// warnings here in the class declaration. params_.java_multiple_files(file_name)
printer->Print( && descriptor_->containing_type() == NULL;
"@SuppressWarnings(\"hiding\")\n"
"public $modifiers$final class $classname$ extends\n" if (is_own_file) {
" com.google.protobuf.nano.MessageNano {\n", // Note: constants (from enums and fields requiring stored defaults, emitted in the loop below)
"modifiers", is_own_file ? "" : "static ", // may have the same names as constants in the nested classes. This causes Java warnings, but
"classname", descriptor_->name()); // is not fatal, so we suppress those warnings here in the top-most class declaration.
printer->Print(
"\n"
"@SuppressWarnings(\"hiding\")\n"
"public final class $classname$ extends\n"
" com.google.protobuf.nano.MessageNano {\n",
"classname", descriptor_->name());
} else {
printer->Print(
"\n"
"public static final class $classname$ extends\n"
" com.google.protobuf.nano.MessageNano {\n",
"classname", descriptor_->name());
}
printer->Indent(); printer->Indent();
printer->Print( printer->Print(
"public static final $classname$ EMPTY_ARRAY[] = {};\n" "\n"
"public static final $classname$[] EMPTY_ARRAY = {};\n"
"\n"
"public $classname$() {\n" "public $classname$() {\n"
" clear();\n" " clear();\n"
"}\n" "}\n",
"\n",
"classname", descriptor_->name()); "classname", descriptor_->name());
if (params_.store_unknown_fields()) { if (params_.store_unknown_fields()) {
printer->Print( printer->Print(
"\n"
"private java.util.List<com.google.protobuf.nano.UnknownFieldData>\n" "private java.util.List<com.google.protobuf.nano.UnknownFieldData>\n"
" unknownFieldData;\n"); " unknownFieldData;\n");
} }
...@@ -171,16 +181,19 @@ void MessageGenerator::Generate(io::Printer* printer) { ...@@ -171,16 +181,19 @@ void MessageGenerator::Generate(io::Printer* printer) {
// Integers for bit fields // Integers for bit fields
int totalInts = (field_generators_.total_bits() + 31) / 32; int totalInts = (field_generators_.total_bits() + 31) / 32;
for (int i = 0; i < totalInts; i++) { if (totalInts > 0) {
printer->Print("private int $bit_field_name$;\n", printer->Print("\n");
"bit_field_name", GetBitFieldName(i)); for (int i = 0; i < totalInts; i++) {
printer->Print("private int $bit_field_name$;\n",
"bit_field_name", GetBitFieldName(i));
}
} }
// Fields // Fields
for (int i = 0; i < descriptor_->field_count(); i++) { for (int i = 0; i < descriptor_->field_count(); i++) {
printer->Print("\n");
PrintFieldComment(printer, descriptor_->field(i)); PrintFieldComment(printer, descriptor_->field(i));
field_generators_.get(descriptor_->field(i)).GenerateMembers(printer); field_generators_.get(descriptor_->field(i)).GenerateMembers(printer);
printer->Print("\n");
} }
GenerateClear(printer); GenerateClear(printer);
...@@ -189,25 +202,27 @@ void MessageGenerator::Generate(io::Printer* printer) { ...@@ -189,25 +202,27 @@ void MessageGenerator::Generate(io::Printer* printer) {
if (params_.store_unknown_fields() if (params_.store_unknown_fields()
&& descriptor_->extension_range_count() > 0) { && descriptor_->extension_range_count() > 0) {
printer->Print( printer->Print(
"\n"
"public <T> T getExtension(com.google.protobuf.nano.Extension<T> extension) {\n" "public <T> T getExtension(com.google.protobuf.nano.Extension<T> extension) {\n"
" return com.google.protobuf.nano.WireFormatNano.getExtension(\n" " return com.google.protobuf.nano.WireFormatNano.getExtension(\n"
" extension, unknownFieldData);\n" " extension, unknownFieldData);\n"
"}\n\n" "}\n"
"\n"
"public <T> void setExtension(com.google.protobuf.nano.Extension<T> extension, T value) {\n" "public <T> void setExtension(com.google.protobuf.nano.Extension<T> extension, T value) {\n"
" if (unknownFieldData == null) {\n" " if (unknownFieldData == null) {\n"
" unknownFieldData = \n" " unknownFieldData =\n"
" new java.util.ArrayList<com.google.protobuf.nano.UnknownFieldData>();\n" " new java.util.ArrayList<com.google.protobuf.nano.UnknownFieldData>();\n"
" }\n" " }\n"
" com.google.protobuf.nano.WireFormatNano.setExtension(\n" " com.google.protobuf.nano.WireFormatNano.setExtension(\n"
" extension, value, unknownFieldData);\n" " extension, value, unknownFieldData);\n"
"}\n\n"); "}\n");
} }
GenerateMessageSerializationMethods(printer); GenerateMessageSerializationMethods(printer);
GenerateMergeFromMethods(printer); GenerateMergeFromMethods(printer);
GenerateParseFromMethods(printer); GenerateParseFromMethods(printer);
printer->Outdent(); printer->Outdent();
printer->Print("}\n\n"); printer->Print("}\n");
} }
// =================================================================== // ===================================================================
...@@ -220,11 +235,13 @@ GenerateMessageSerializationMethods(io::Printer* printer) { ...@@ -220,11 +235,13 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
// writeTo only throws an exception if it contains one or more fields to write // writeTo only throws an exception if it contains one or more fields to write
if (descriptor_->field_count() > 0 || params_.store_unknown_fields()) { if (descriptor_->field_count() > 0 || params_.store_unknown_fields()) {
printer->Print( printer->Print(
"\n"
"@Override\n" "@Override\n"
"public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output)\n" "public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output)\n"
" throws java.io.IOException {\n"); " throws java.io.IOException {\n");
} else { } else {
printer->Print( printer->Print(
"\n"
"@Override\n" "@Override\n"
"public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output) {\n"); "public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output) {\n");
} }
...@@ -274,8 +291,7 @@ GenerateMessageSerializationMethods(io::Printer* printer) { ...@@ -274,8 +291,7 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
printer->Print( printer->Print(
" cachedSize = size;\n" " cachedSize = size;\n"
" return size;\n" " return size;\n"
"}\n" "}\n");
"\n");
} }
void MessageGenerator::GenerateMergeFromMethods(io::Printer* printer) { void MessageGenerator::GenerateMergeFromMethods(io::Printer* printer) {
...@@ -283,9 +299,10 @@ void MessageGenerator::GenerateMergeFromMethods(io::Printer* printer) { ...@@ -283,9 +299,10 @@ void MessageGenerator::GenerateMergeFromMethods(io::Printer* printer) {
SortFieldsByNumber(descriptor_)); SortFieldsByNumber(descriptor_));
printer->Print( printer->Print(
"\n"
"@Override\n" "@Override\n"
"public $classname$ mergeFrom(\n" "public $classname$ mergeFrom(\n"
" com.google.protobuf.nano.CodedInputByteBufferNano input)\n" " com.google.protobuf.nano.CodedInputByteBufferNano input)\n"
" throws java.io.IOException {\n", " throws java.io.IOException {\n",
"classname", descriptor_->name()); "classname", descriptor_->name());
...@@ -350,8 +367,7 @@ void MessageGenerator::GenerateMergeFromMethods(io::Printer* printer) { ...@@ -350,8 +367,7 @@ void MessageGenerator::GenerateMergeFromMethods(io::Printer* printer) {
printer->Print( printer->Print(
" }\n" // switch (tag) " }\n" // switch (tag)
" }\n" // while (true) " }\n" // while (true)
"}\n" "}\n");
"\n");
} }
void MessageGenerator:: void MessageGenerator::
...@@ -360,6 +376,7 @@ GenerateParseFromMethods(io::Printer* printer) { ...@@ -360,6 +376,7 @@ GenerateParseFromMethods(io::Printer* printer) {
// because they need to be generated even for messages that are optimized // because they need to be generated even for messages that are optimized
// for code size. // for code size.
printer->Print( printer->Print(
"\n"
"public static $classname$ parseFrom(byte[] data)\n" "public static $classname$ parseFrom(byte[] data)\n"
" throws com.google.protobuf.nano.InvalidProtocolBufferNanoException {\n" " throws com.google.protobuf.nano.InvalidProtocolBufferNanoException {\n"
" return com.google.protobuf.nano.MessageNano.mergeFrom(new $classname$(), data);\n" " return com.google.protobuf.nano.MessageNano.mergeFrom(new $classname$(), data);\n"
...@@ -369,8 +386,7 @@ GenerateParseFromMethods(io::Printer* printer) { ...@@ -369,8 +386,7 @@ GenerateParseFromMethods(io::Printer* printer) {
" com.google.protobuf.nano.CodedInputByteBufferNano input)\n" " com.google.protobuf.nano.CodedInputByteBufferNano input)\n"
" throws java.io.IOException {\n" " throws java.io.IOException {\n"
" return new $classname$().mergeFrom(input);\n" " return new $classname$().mergeFrom(input);\n"
"}\n" "}\n",
"\n",
"classname", descriptor_->name()); "classname", descriptor_->name());
} }
...@@ -381,7 +397,8 @@ void MessageGenerator::GenerateSerializeOneField( ...@@ -381,7 +397,8 @@ void MessageGenerator::GenerateSerializeOneField(
void MessageGenerator::GenerateClear(io::Printer* printer) { void MessageGenerator::GenerateClear(io::Printer* printer) {
printer->Print( printer->Print(
"public final $classname$ clear() {\n", "\n"
"public $classname$ clear() {\n",
"classname", descriptor_->name()); "classname", descriptor_->name());
printer->Indent(); printer->Indent();
...@@ -407,8 +424,7 @@ void MessageGenerator::GenerateClear(io::Printer* printer) { ...@@ -407,8 +424,7 @@ void MessageGenerator::GenerateClear(io::Printer* printer) {
printer->Print( printer->Print(
" cachedSize = -1;\n" " cachedSize = -1;\n"
" return this;\n" " return this;\n"
"}\n" "}\n");
"\n");
} }
// =================================================================== // ===================================================================
......
...@@ -98,7 +98,7 @@ GenerateMergingCode(io::Printer* printer) const { ...@@ -98,7 +98,7 @@ GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"if (this.$name$ == null) {\n" "if (this.$name$ == null) {\n"
" this.$name$ = new $type$();\n" " this.$name$ = new $type$();\n"
"}"); "}\n");
if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) { if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
printer->Print(variables_, printer->Print(variables_,
...@@ -173,7 +173,7 @@ GenerateMergingCode(io::Printer* printer) const { ...@@ -173,7 +173,7 @@ GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"if (!has$capitalized_name$()) {\n" "if (!has$capitalized_name$()) {\n"
" set$capitalized_name$(new $type$());\n" " set$capitalized_name$(new $type$());\n"
"}"); "}\n");
if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) { if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
printer->Print(variables_, printer->Print(variables_,
...@@ -231,10 +231,11 @@ void RepeatedMessageFieldGenerator:: ...@@ -231,10 +231,11 @@ void RepeatedMessageFieldGenerator::
GenerateMergingCode(io::Printer* printer) const { GenerateMergingCode(io::Printer* printer) const {
// First, figure out the length of the array, then parse. // First, figure out the length of the array, then parse.
printer->Print(variables_, printer->Print(variables_,
"int arrayLength = com.google.protobuf.nano.WireFormatNano" "int arrayLength = com.google.protobuf.nano.WireFormatNano\n"
" .getRepeatedFieldArrayLength(input, $tag$);\n" " .getRepeatedFieldArrayLength(input, $tag$);\n"
"int i = this.$name$ == null ? 0 : this.$name$.length;\n" "int i = this.$name$ == null ? 0 : this.$name$.length;\n"
"$type$[] newArray = new $type$[i + arrayLength];\n" "$type$[] newArray =\n"
" new $type$[i + arrayLength];\n"
"if (i != 0) {\n" "if (i != 0) {\n"
" java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n" " java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\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