Improved generation for C\# files

committer: Jon Skeet <skeet@pobox.com>
pull/288/head
Jon Skeet 17 years ago committed by unknown
parent baa2bf54c2
commit 70ff861764
  1. 115
      src/google/protobuf/compiler/csharp/csharp_enum.cc
  2. 10
      src/google/protobuf/compiler/csharp/csharp_enum_field.cc
  3. 8
      src/google/protobuf/compiler/csharp/csharp_extension.cc
  4. 144
      src/google/protobuf/compiler/csharp/csharp_file.cc
  5. 12
      src/google/protobuf/compiler/csharp/csharp_helpers.cc
  6. 148
      src/google/protobuf/compiler/csharp/csharp_message.cc
  7. 8
      src/google/protobuf/compiler/csharp/csharp_message_field.cc
  8. 42
      src/google/protobuf/compiler/csharp/csharp_primitive_field.cc
  9. 40
      src/google/protobuf/compiler/csharp/csharp_service.cc
  10. 1
      src/google/protobuf/compiler/main.cc

@ -58,8 +58,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
descriptor_->containing_type() == NULL &&
descriptor_->file()->options().csharp_multiple_files();
printer->Print(
"public $static$ enum $classname$ {\r\n",
"static", is_own_file ? "" : "static",
"public enum $classname$ {\r\n",
"classname", descriptor_->name());
printer->Indent();
@ -68,16 +67,18 @@ void EnumGenerator::Generate(io::Printer* printer) {
vars["name"] = canonical_values_[i]->name();
vars["index"] = SimpleItoa(canonical_values_[i]->index());
vars["number"] = SimpleItoa(canonical_values_[i]->number());
// TODO(jonskeet): Change CONSTANT_CASE into PascalCase
printer->Print(vars,
"$name$($index$, $number$),\r\n");
"[pb::DescriptorIndex($index$)]\r\n"
"$name$ = $number$,\r\n");
}
printer->Print(
";\r\n"
"\r\n");
printer->Outdent();
printer->Print("}\r\n\r\n");
// -----------------------------------------------------------------
// TODO(jonskeet): Think about aliases!
/*
for (int i = 0; i < aliases_.size(); i++) {
map<string, string> vars;
vars["classname"] = descriptor_->name();
@ -85,103 +86,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
vars["canonical_name"] = aliases_[i].canonical_value->name();
printer->Print(vars,
"public static final $classname$ $name$ = $canonical_name$;\r\n");
}
// -----------------------------------------------------------------
printer->Print(
"\r\n"
"public final int getNumber() { return value; }\r\n"
"\r\n"
"public static $classname$ valueOf(int value) {\r\n"
" switch (value) {\r\n",
"classname", descriptor_->name());
printer->Indent();
printer->Indent();
for (int i = 0; i < canonical_values_.size(); i++) {
printer->Print(
"case $number$: return $name$;\r\n",
"name", canonical_values_[i]->name(),
"number", SimpleItoa(canonical_values_[i]->number()));
}
printer->Outdent();
printer->Outdent();
printer->Print(
" default: return null;\r\n"
" }\r\n"
"}\r\n"
"\r\n");
// -----------------------------------------------------------------
// Reflection
printer->Print(
"public final com.google.protobuf.Descriptors.EnumValueDescriptor\r\n"
" getValueDescriptor() {\r\n"
" return getDescriptor().getValues().get(index);\r\n"
"}\r\n"
"public final com.google.protobuf.Descriptors.EnumDescriptor\r\n"
" getDescriptorForType() {\r\n"
" return getDescriptor();\r\n"
"}\r\n"
"public static final com.google.protobuf.Descriptors.EnumDescriptor\r\n"
" getDescriptor() {\r\n");
// TODO(kenton): Cache statically? Note that we can't access descriptors
// at module init time because it wouldn't work with descriptor.proto, but
// we can cache the value the first time getDescriptor() is called.
if (descriptor_->containing_type() == NULL) {
printer->Print(
" return $file$.getDescriptor().getEnumTypes().get($index$);\r\n",
"file", ClassName(descriptor_->file()),
"index", SimpleItoa(descriptor_->index()));
} else {
printer->Print(
" return $parent$.getDescriptor().getEnumTypes().get($index$);\r\n",
"parent", ClassName(descriptor_->containing_type()),
"index", SimpleItoa(descriptor_->index()));
}
printer->Print(
"}\r\n"
"\r\n"
"private static final $classname$[] VALUES = {\r\n"
" ",
"classname", descriptor_->name());
for (int i = 0; i < descriptor_->value_count(); i++) {
printer->Print("$name$, ",
"name", descriptor_->value(i)->name());
}
printer->Print(
"\r\n"
"};\r\n"
"public static $classname$ valueOf(\r\n"
" com.google.protobuf.Descriptors.EnumValueDescriptor desc) {\r\n"
" if (desc.getType() != getDescriptor()) {\r\n"
" throw new java.lang.IllegalArgumentException(\r\n"
" \"EnumValueDescriptor is not for this type.\");\r\n"
" }\r\n"
" return VALUES[desc.getIndex()];\r\n"
"}\r\n",
"classname", descriptor_->name());
// -----------------------------------------------------------------
printer->Print(
"private final int index;\r\n"
"private final int value;\r\n"
"private $classname$(int index, int value) {\r\n"
" this.index = index;\r\n"
" this.value = value;\r\n"
"}\r\n",
"classname", descriptor_->name());
printer->Outdent();
printer->Print("}\r\n\r\n");
}*/
}
} // namespace csharp

@ -132,7 +132,7 @@ void EnumFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
"if (has$capitalized_name$()) {\r\n"
" size += com.google.protobuf.CodedOutputStream\r\n"
" size += pb::CodedOutputStream\r\n"
" .computeEnumSize($number$, get$capitalized_name$().getNumber());\r\n"
"}\r\n");
}
@ -172,7 +172,7 @@ GenerateBuilderMembers(io::Printer* printer) const {
// could hold on to the returned list and modify it after the message
// has been built, thus mutating the message which is supposed to be
// immutable.
"public java.util.List<$type$> get$capitalized_name$List() {\r\n"
"public global::System.Collections.Generic::IList<$type$> get$capitalized_name$List() {\r\n"
" return java.util.Collections.unmodifiableList(result.$name$_);\r\n"
"}\r\n"
"public int get$capitalized_name$Count() {\r\n"
@ -192,8 +192,8 @@ GenerateBuilderMembers(io::Printer* printer) const {
" result.$name$_.add(value);\r\n"
" return this;\r\n"
"}\r\n"
"public Builder addAll$capitalized_name$(\r\n"
" java.lang.Iterable<? extends $type$> values) {\r\n"
"public Builder addAll$capitalized_name$<T>(\r\n"
" global::System.Collections.Generic.IEnumerable<T> values) where T : $type$ {\r\n"
" if (result.$name$_.isEmpty()) {\r\n"
" result.$name$_ = new java.util.ArrayList<$type$>();\r\n"
" }\r\n"
@ -250,7 +250,7 @@ void RepeatedEnumFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
"for ($type$ element : get$capitalized_name$List()) {\r\n"
" size += com.google.protobuf.CodedOutputStream\r\n"
" size += pb::CodedOutputStream\r\n"
" .computeEnumSize($number$, element.getNumber());\r\n"
"}\r\n");
}

@ -57,20 +57,20 @@ void ExtensionGenerator::Generate(io::Printer* printer) {
if (descriptor_->is_repeated()) {
printer->Print(vars,
"public static final\r\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\r\n"
" pb::GeneratedMessage.GeneratedExtension<\r\n"
" $containing_type$,\r\n"
" java.util.List<$type$>> $name$ =\r\n"
" com.google.protobuf.GeneratedMessage\r\n"
" pb::GeneratedMessage\r\n"
" .newRepeatedGeneratedExtension(\r\n"
" getDescriptor().getExtensions().get($index$),\r\n"
" $type$.class);\r\n");
} else {
printer->Print(vars,
"public static final\r\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\r\n"
" pb::GeneratedMessage.GeneratedExtension<\r\n"
" $containing_type$,\r\n"
" $type$> $name$ =\r\n"
" com.google.protobuf.GeneratedMessage.newGeneratedExtension(\r\n"
" pb::GeneratedMessage.newGeneratedExtension(\r\n"
" getDescriptor().getExtensions().get($index$),\r\n"
" $type$.class);\r\n");
}

@ -69,7 +69,7 @@ bool FileGenerator::Validate(string* error) {
if (found_conflict) {
error->assign(file_->name());
error->append(
": Cannot generate Java output because the file's outer class name, \"");
": Cannot generate C# output because the file's top-level class name, \"");
error->append(classname_);
error->append(
"\", matches the name of one of the types declared inside it. "
@ -81,67 +81,25 @@ bool FileGenerator::Validate(string* error) {
return true;
}
// Shameless copy of CEscape, but using backslash-u encoding
// Still won't really work for anything non-ASCII.
static int UnicodeEscapeInternal(const char* src, int src_len, char* dest,
int dest_len) {
const char* src_end = src + src_len;
int used = 0;
for (; src < src_end; src++) {
if (dest_len - used < 2) // Need space for two letter escape
return -1;
bool is_hex_escape = false;
switch (*src) {
case '\n': dest[used++] = '\\'; dest[used++] = 'n'; break;
case '\r': dest[used++] = '\\'; dest[used++] = 'r'; break;
case '\t': dest[used++] = '\\'; dest[used++] = 't'; break;
case '\"': dest[used++] = '\\'; dest[used++] = '\"'; break;
case '\'': dest[used++] = '\\'; dest[used++] = '\''; break;
case '\\': dest[used++] = '\\'; dest[used++] = '\\'; break;
default:
if (*src < 0x20 || *src > 0x7E) {
if (dest_len - used < 6) // need space for 6 letter escape
return -1;
sprintf(dest + used, "\\u00%02x", static_cast<uint8>(*src));
used += 6;
} else {
dest[used++] = *src; break;
}
}
}
if (dest_len - used < 1) // make sure that there is room for \0
return -1;
dest[used] = '\0'; // doesn't count towards return value though
return used;
}
string UnicodeEscape(const string& src) {
const int dest_length = src.size() * 6 + 1; // Maximum possible expansion
scoped_array<char> dest(new char[dest_length]);
const int len = UnicodeEscapeInternal(src.data(), src.size(),
dest.get(), dest_length);
GOOGLE_DCHECK_GE(len, 0);
return string(dest.get(), len);
}
void FileGenerator::Generate(io::Printer* printer) {
// We don't import anything because we refer to all classes by their
// fully-qualified names in the generated source.
printer->Print(
"// Generated by the protocol buffer compiler. DO NOT EDIT!\r\n"
"\r\n");
// Namespace alias to avoid lines getting horribly long
printer->Print("using pb = global::Google.ProtocolBuffers;\r\n\r\n");
if (!csharp_namespace_.empty()) {
printer->Print(
"package $package$;\r\n"
"namespace $namespace$ {\r\n"
"\r\n",
"package", csharp_namespace_);
"namespace", csharp_namespace_);
printer->Indent();
}
printer->Print(
"public final class $classname$ {\r\n"
"public sealed class $classname$ {\r\n"
" private $classname$() {}\r\n",
"classname", classname_);
printer->Indent();
@ -149,69 +107,50 @@ void FileGenerator::Generate(io::Printer* printer) {
// -----------------------------------------------------------------
// Embed the descriptor. We simply serialize the entire FileDescriptorProto
// and embed it as a string literal, which is parsed and built into real
// descriptors at initialization time. We unfortunately have to put it in
// a string literal, not a byte array, because apparently using a literal
// byte array causes the Java compiler to generate *instructions* to
// initialize each and every byte of the array, e.g. as if you typed:
// b[0] = 123; b[1] = 456; b[2] = 789;
// This makes huge bytecode files and can easily hit the compiler's internal
// code size limits (error "code to large"). String literals are apparently
// embedded raw, which is what we want.
// and embed it as a byte array, which is parsed and built into real
// descriptors at initialization time.
FileDescriptorProto file_proto;
file_->CopyTo(&file_proto);
string file_data;
file_proto.SerializeToString(&file_data);
printer->Print(
"public static com.google.protobuf.Descriptors.FileDescriptor\r\n"
" getDescriptor() {\r\n"
" return descriptor;\r\n"
"public static pb::Descriptors.FileDescriptor Descriptor {\r\n"
" get { return descriptor; }\r\n"
"}\r\n"
"private static final com.google.protobuf.Descriptors.FileDescriptor\r\n"
" descriptor = buildDescriptor();\r\n"
"private static\r\n"
" com.google.protobuf.Descriptors.FileDescriptor\r\n"
" buildDescriptor() {\r\n"
" string descriptorData =\r\n");
"private static readonly pb::Descriptors.FileDescriptor descriptor = pb::Descriptors.FileDescriptor.InternalBuildGeneratedFileFrom (\r\n"
" new byte[] {");
printer->Indent();
printer->Indent();
printer->Indent();
printer->Indent();
// Only write 40 bytes per line.
static const int kBytesPerLine = 40;
for (int i = 0; i < file_data.size(); i += kBytesPerLine) {
if (i > 0) printer->Print(" +\r\n");
printer->Print("\"$data$\"",
"data", UnicodeEscape(file_data.substr(i, kBytesPerLine)));
}
printer->Print(";\r\n");
// Only write 20 bytes per line.
static const int kBytesPerLine = 20;
char buffer[3];
for (int i = 0; i < file_data.size(); i++) {
if ((i % kBytesPerLine) == 0) {
printer->Print("\r\n");
}
// TODO(jonskeet): There must be a better way of doing this!
sprintf(buffer, "%02x", static_cast<uint8>(file_data[i]));
printer->Print("0x$val$, ", "val", buffer);
}
printer->Outdent();
printer->Print(
"try {\r\n"
" return com.google.protobuf.Descriptors.FileDescriptor\r\n"
" .internalBuildGeneratedFileFrom(descriptorData,\r\n"
" new com.google.protobuf.Descriptors.FileDescriptor[] {\r\n");
printer->Outdent();
printer->Print("\r\n}, new pb::Descriptors.FileDescriptor[] {\r\n");
for (int i = 0; i < file_->dependency_count(); i++) {
printer->Print(
" $dependency$.getDescriptor(),\r\n",
"dependency", ClassName(file_->dependency(i)));
}
printer->Print(
" });\r\n"
"} catch (Exception e) {\r\n"
" throw new RuntimeException(\r\n"
" \"Failed to parse protocol buffer descriptor for \" +\r\n"
" \"\\\"$filename$\\\".\", e);\r\n"
"}\r\n",
"filename", file_->name());
printer->Print("});\r\n");
printer->Outdent();
printer->Print(
"}\r\n"
"\r\n");
printer->Outdent();
printer->Print("\r\n");
// -----------------------------------------------------------------
@ -241,6 +180,10 @@ void FileGenerator::Generate(io::Printer* printer) {
printer->Outdent();
printer->Print("}\r\n");
if (!csharp_namespace_.empty()) {
printer->Outdent();
printer->Print("}\r\n");
}
}
template<typename GeneratorClass, typename DescriptorClass>
@ -259,14 +202,23 @@ static void GenerateSibling(const string& package_dir,
printer.Print(
"// Generated by the protocol buffer compiler. DO NOT EDIT!\r\n"
"\r\n");
// Namespace alias to avoid lines getting horribly long
printer.Print("using pb = global::Google.ProtocolBuffers;\r\n\r\n");
if (!csharp_namespace.empty()) {
printer.Print(
"package $package$;\r\n"
"namespace $namespace$ {\r\n"
"\r\n",
"package", csharp_namespace);
"namespace", csharp_namespace);
printer.Indent();
}
GeneratorClass(descriptor).Generate(&printer);
if (!csharp_namespace.empty()) {
printer.Outdent();
printer.Print("}\r\n");
}
}
void FileGenerator::GenerateSiblings(const string& package_dir,

@ -206,13 +206,13 @@ JavaType GetJavaType(FieldDescriptor::Type field_type) {
const char* BoxedPrimitiveTypeName(JavaType type) {
switch (type) {
case JAVATYPE_INT : return "java.lang.Integer";
case JAVATYPE_LONG : return "java.lang.Long";
case JAVATYPE_FLOAT : return "java.lang.Float";
case JAVATYPE_DOUBLE : return "java.lang.Double";
case JAVATYPE_BOOLEAN: return "java.lang.Boolean";
case JAVATYPE_INT : return "int";
case JAVATYPE_LONG : return "long";
case JAVATYPE_FLOAT : return "float";
case JAVATYPE_DOUBLE : return "double";
case JAVATYPE_BOOLEAN: return "bool";
case JAVATYPE_STRING : return "string";
case JAVATYPE_BYTES : return "com.google.protobuf.ByteString";
case JAVATYPE_BYTES : return "pb::ByteString";
case JAVATYPE_ENUM : return NULL;
case JAVATYPE_MESSAGE: return NULL;

@ -140,7 +140,7 @@ MessageGenerator::MessageGenerator(const Descriptor* descriptor)
MessageGenerator::~MessageGenerator() {}
void MessageGenerator::GenerateStaticVariables(io::Printer* printer) {
// Because descriptor.proto (com.google.protobuf.DescriptorProtos) is
// Because descriptor.proto (Google.ProtocolBuffers.DescriptorProtos) is
// used in the construction of descriptors, we have a tricky bootstrapping
// problem. To help control static initialization order, we make sure all
// descriptors and other static data that depends on them are members of
@ -155,9 +155,9 @@ void MessageGenerator::GenerateStaticVariables(io::Printer* printer) {
vars["parent"] = UniqueFileScopeIdentifier(descriptor_->containing_type());
}
if (descriptor_->file()->options().csharp_multiple_files()) {
// We can only make these package-private since the classes that use them
// We can only make these assembly-private since the classes that use them
// are in separate files.
vars["private"] = "";
vars["private"] = "internal ";
} else {
vars["private"] = "private ";
}
@ -165,12 +165,12 @@ void MessageGenerator::GenerateStaticVariables(io::Printer* printer) {
// The descriptor for this type.
if (descriptor_->containing_type() == NULL) {
printer->Print(vars,
"$private$static final com.google.protobuf.Descriptors.Descriptor\r\n"
"$private$static readonly pb::Descriptors.Descriptor {\r\n"
" internal_$identifier$_descriptor =\r\n"
" getDescriptor().getMessageTypes().get($index$);\r\n");
} else {
printer->Print(vars,
"$private$static final com.google.protobuf.Descriptors.Descriptor\r\n"
"$private$static readonly pb::Descriptors.Descriptor {\r\n"
" internal_$identifier$_descriptor =\r\n"
" internal_$parent$_descriptor.getNestedTypes().get($index$);\r\n");
}
@ -178,9 +178,9 @@ void MessageGenerator::GenerateStaticVariables(io::Printer* printer) {
// And the FieldAccessorTable.
printer->Print(vars,
"$private$static\r\n"
" com.google.protobuf.GeneratedMessage.FieldAccessorTable\r\n"
" pb::GeneratedMessage.FieldAccessorTable\r\n"
" internal_$identifier$_fieldAccessorTable = new\r\n"
" com.google.protobuf.GeneratedMessage.FieldAccessorTable(\r\n"
" pb::GeneratedMessage.FieldAccessorTable(\r\n"
" internal_$identifier$_descriptor,\r\n"
" new string[] { ");
for (int i = 0; i < descriptor_->field_count(); i++) {
@ -209,16 +209,11 @@ void MessageGenerator::Generate(io::Printer* printer) {
if (descriptor_->extension_range_count() > 0) {
printer->Print(
"public $static$ final class $classname$ extends\r\n"
" com.google.protobuf.GeneratedMessage.ExtendableMessage<\r\n"
" $classname$> {\r\n",
"static", is_own_file ? "" : "static",
"public sealed class $classname$ : pb::GeneratedMessage.ExtendableMessage<$classname$> {\r\n",
"classname", descriptor_->name());
} else {
printer->Print(
"public $static$ final class $classname$ extends\r\n"
" com.google.protobuf.GeneratedMessage {\r\n",
"static", is_own_file ? "" : "static",
"public sealed class $classname$ : pb::GeneratedMessage {\r\n",
"classname", descriptor_->name());
}
printer->Indent();
@ -226,25 +221,23 @@ void MessageGenerator::Generate(io::Printer* printer) {
"// Use $classname$.newBuilder() to construct.\r\n"
"private $classname$() {}\r\n"
"\r\n"
"private static final $classname$ defaultInstance = new $classname$();\r\n"
"public static $classname$ getDefaultInstance() {\r\n"
" return defaultInstance;\r\n"
"private static readonly $classname$ defaultInstance = new $classname$();\r\n"
"public static $classname$ DefaultInstance {\r\n"
" get { return defaultInstance; }\r\n"
"}\r\n"
"\r\n"
"public $classname$ getDefaultInstanceForType() {\r\n"
" return defaultInstance;\r\n"
"public $classname$ DefaultInstanceForType {\r\n"
" get { return defaultInstance; }\r\n"
"}\r\n"
"\r\n",
"classname", descriptor_->name());
printer->Print(
"public static final com.google.protobuf.Descriptors.Descriptor\r\n"
" getDescriptor() {\r\n"
" return $fileclass$.internal_$identifier$_descriptor;\r\n"
"public static pb::Descriptors.Descriptor Descriptor {\r\n"
" get { return $fileclass$.internal_$identifier$_descriptor; }\r\n"
"}\r\n"
"\r\n"
"protected com.google.protobuf.GeneratedMessage.FieldAccessorTable\r\n"
" internalGetFieldAccessorTable() {\r\n"
" return $fileclass$.internal_$identifier$_fieldAccessorTable;\r\n"
"protected pb::GeneratedMessage.FieldAccessorTable InternalGetFieldAccessorTable {\r\n"
" get { return $fileclass$.internal_$identifier$_fieldAccessorTable; }\r\n"
"}\r\n"
"\r\n",
"fileclass", ClassName(descriptor_->file()),
@ -293,15 +286,12 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
sort(sorted_extensions.begin(), sorted_extensions.end(),
ExtensionRangeOrdering());
printer->Print(
"public void writeTo(com.google.protobuf.CodedOutputStream output)\r\n"
" throws java.io.IOException {\r\n");
printer->Print("public void writeTo(pb::CodedOutputStream output) {\r\n");
printer->Indent();
if (descriptor_->extension_range_count() > 0) {
printer->Print(
"com.google.protobuf.GeneratedMessage.ExtendableMessage\r\n"
" .ExtensionWriter extensionWriter = newExtensionWriter();\r\n");
"pb::GeneratedMessage.ExtendableMessage.ExtensionWriter extensionWriter = NewExtensionWriter();\r\n");
}
// Merge the fields and the extension ranges, both sorted by field number.
@ -321,10 +311,10 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
if (descriptor_->options().message_set_wire_format()) {
printer->Print(
"getUnknownFields().writeAsMessageSetTo(output);\r\n");
"UnknownFields.WriteAsMessageSetTo(output);\r\n");
} else {
printer->Print(
"getUnknownFields().writeTo(output);\r\n");
"UnknownFields.WriteTo(output);\r\n");
}
printer->Outdent();
@ -332,34 +322,38 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
"}\r\n"
"\r\n"
"private int memoizedSerializedSize = -1;\r\n"
"public int getSerializedSize() {\r\n"
" int size = memoizedSerializedSize;\r\n"
" if (size != -1) return size;\r\n"
"\r\n"
" size = 0;\r\n");
"public int SerializedSize {\r\n");
printer->Indent();
printer->Print("get {\r\n");
printer->Indent();
printer->Print(
"int size = memoizedSerializedSize;\r\n"
"if (size != -1) return size;\r\n"
"\r\n"
"size = 0;\r\n");
for (int i = 0; i < descriptor_->field_count(); i++) {
field_generators_.get(sorted_fields[i]).GenerateSerializedSizeCode(printer);
}
if (descriptor_->extension_range_count() > 0) {
printer->Print(
"size += extensionsSerializedSize();\r\n");
"size += ExtensionsSerializedSize;\r\n");
}
if (descriptor_->options().message_set_wire_format()) {
printer->Print(
"size += getUnknownFields().getSerializedSizeAsMessageSet();\r\n");
"size += UnknownFields.SerializedSizeAsMessageSet;\r\n");
} else {
printer->Print(
"size += getUnknownFields().getSerializedSize();\r\n");
"size += UnknownFieldsSerializedSize;\r\n");
}
printer->Outdent();
printer->Outdent();
printer->Print(
" memoizedSerializedSize = size;\r\n"
" return size;\r\n"
" memoizedSerializedSize = size;\r\n"
" return size;\r\n"
" }\r\n"
"}\r\n"
"\r\n");
}
@ -370,49 +364,36 @@ GenerateParseFromMethods(io::Printer* printer) {
// because they need to be generated even for messages that are optimized
// for code size.
printer->Print(
"public static $classname$ parseFrom(\r\n"
" com.google.protobuf.ByteString data)\r\n"
" throws com.google.protobuf.InvalidProtocolBufferException {\r\n"
"public static $classname$ parseFrom(pb::ByteString data) {\r\n"
" return newBuilder().mergeFrom(data).buildParsed();\r\n"
"}\r\n"
"public static $classname$ parseFrom(\r\n"
" com.google.protobuf.ByteString data,\r\n"
" com.google.protobuf.ExtensionRegistry extensionRegistry)\r\n"
" throws com.google.protobuf.InvalidProtocolBufferException {\r\n"
"public static $classname$ parseFrom(pb::ByteString data,\r\n"
" pb::ExtensionRegistry extensionRegistry) {\r\n"
" return newBuilder().mergeFrom(data, extensionRegistry)\r\n"
" .buildParsed();\r\n"
"}\r\n"
"public static $classname$ parseFrom(byte[] data)\r\n"
" throws com.google.protobuf.InvalidProtocolBufferException {\r\n"
"public static $classname$ parseFrom(byte[] data) {\r\n"
" return newBuilder().mergeFrom(data).buildParsed();\r\n"
"}\r\n"
"public static $classname$ parseFrom(\r\n"
" byte[] data,\r\n"
" com.google.protobuf.ExtensionRegistry extensionRegistry)\r\n"
" throws com.google.protobuf.InvalidProtocolBufferException {\r\n"
"public static $classname$ parseFrom(byte[] data,\r\n"
" pb::ExtensionRegistry extensionRegistry) {\r\n"
" return newBuilder().mergeFrom(data, extensionRegistry)\r\n"
" .buildParsed();\r\n"
"}\r\n"
"public static $classname$ parseFrom(java.io.InputStream input)\r\n"
" throws java.io.IOException {\r\n"
"public static $classname$ parseFrom(global::System.IO.Stream input) {\r\n"
" return newBuilder().mergeFrom(input).buildParsed();\r\n"
"}\r\n"
"public static $classname$ parseFrom(\r\n"
" java.io.InputStream input,\r\n"
" com.google.protobuf.ExtensionRegistry extensionRegistry)\r\n"
" throws java.io.IOException {\r\n"
" global::System.IO.Stream input,\r\n"
" pb::ExtensionRegistry extensionRegistry) {\r\n"
" return newBuilder().mergeFrom(input, extensionRegistry)\r\n"
" .buildParsed();\r\n"
"}\r\n"
"public static $classname$ parseFrom(\r\n"
" com.google.protobuf.CodedInputStream input)\r\n"
" throws java.io.IOException {\r\n"
"public static $classname$ parseFrom(pb::CodedInputStream input) {\r\n"
" return newBuilder().mergeFrom(input).buildParsed();\r\n"
"}\r\n"
"public static $classname$ parseFrom(\r\n"
" com.google.protobuf.CodedInputStream input,\r\n"
" com.google.protobuf.ExtensionRegistry extensionRegistry)\r\n"
" throws java.io.IOException {\r\n"
"public static $classname$ parseFrom(pb::CodedInputStream input,\r\n"
" pb::ExtensionRegistry extensionRegistry) {\r\n"
" return newBuilder().mergeFrom(input, extensionRegistry)\r\n"
" .buildParsed();\r\n"
"}\r\n"
@ -446,14 +427,12 @@ void MessageGenerator::GenerateBuilder(io::Printer* printer) {
if (descriptor_->extension_range_count() > 0) {
printer->Print(
"public static final class Builder extends\r\n"
" com.google.protobuf.GeneratedMessage.ExtendableBuilder<\r\n"
"public sealed class Builder : pb::GeneratedMessage.ExtendableBuilder<\r\n"
" $classname$, Builder> {\r\n",
"classname", ClassName(descriptor_));
} else {
printer->Print(
"public static final class Builder extends\r\n"
" com.google.protobuf.GeneratedMessage.Builder<Builder> {\r\n",
"public sealed class Builder : pb::GeneratedMessage.Builder<Builder> {\r\n",
"classname", ClassName(descriptor_));
}
printer->Indent();
@ -499,7 +478,7 @@ void MessageGenerator::GenerateCommonBuilderMethods(io::Printer* printer) {
" return new Builder().mergeFrom(result);\r\n"
"}\r\n"
"\r\n"
"public com.google.protobuf.Descriptors.Descriptor\r\n"
"public pb::Descriptors.Descriptor\r\n"
" getDescriptorForType() {\r\n"
" return $classname$.getDescriptor();\r\n"
"}\r\n"
@ -515,16 +494,15 @@ void MessageGenerator::GenerateCommonBuilderMethods(io::Printer* printer) {
printer->Print(
"public $classname$ build() {\r\n"
" if (!isInitialized()) {\r\n"
" throw new com.google.protobuf.UninitializedMessageException(\r\n"
" throw new pb::UninitializedMessageException(\r\n"
" result);\r\n"
" }\r\n"
" return buildPartial();\r\n"
"}\r\n"
"\r\n"
"private $classname$ buildParsed()\r\n"
" throws com.google.protobuf.InvalidProtocolBufferException {\r\n"
"private $classname$ buildParsed() {\r\n"
" if (!isInitialized()) {\r\n"
" throw new com.google.protobuf.UninitializedMessageException(\r\n"
" throw new pb::UninitializedMessageException(\r\n"
" result).asInvalidProtocolBufferException();\r\n"
" }\r\n"
" return buildPartial();\r\n"
@ -551,7 +529,7 @@ void MessageGenerator::GenerateCommonBuilderMethods(io::Printer* printer) {
if (descriptor_->file()->options().optimize_for() == FileOptions::SPEED) {
printer->Print(
"public Builder mergeFrom(com.google.protobuf.Message other) {\r\n"
"public Builder mergeFrom(pb::Message other) {\r\n"
" if (other instanceof $classname$) {\r\n"
" return mergeFrom(($classname$)other);\r\n"
" } else {\r\n"
@ -588,21 +566,19 @@ void MessageGenerator::GenerateBuilderParsingMethods(io::Printer* printer) {
printer->Print(
"public Builder mergeFrom(\r\n"
" com.google.protobuf.CodedInputStream input)\r\n"
" throws java.io.IOException {\r\n"
" pb::CodedInputStream input) {\r\n"
" return mergeFrom(input,\r\n"
" com.google.protobuf.ExtensionRegistry.getEmptyRegistry());\r\n"
" pb::ExtensionRegistry.getEmptyRegistry());\r\n"
"}\r\n"
"\r\n"
"public Builder mergeFrom(\r\n"
" com.google.protobuf.CodedInputStream input,\r\n"
" com.google.protobuf.ExtensionRegistry extensionRegistry)\r\n"
" throws java.io.IOException {\r\n");
" pb::CodedInputStream input,\r\n"
" pb::ExtensionRegistry extensionRegistry) {\r\n");
printer->Indent();
printer->Print(
"com.google.protobuf.UnknownFieldSet.Builder unknownFields =\r\n"
" com.google.protobuf.UnknownFieldSet.newBuilder(\r\n"
"pb::UnknownFieldSet.Builder unknownFields =\r\n"
" pb::UnknownFieldSet.newBuilder(\r\n"
" this.getUnknownFields());\r\n"
"while (true) {\r\n");
printer->Indent();

@ -153,7 +153,7 @@ void MessageFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
"if (has$capitalized_name$()) {\r\n"
" size += com.google.protobuf.CodedOutputStream\r\n"
" size += pb::CodedOutputStream\r\n"
" .compute$group_or_message$Size($number$, get$capitalized_name$());\r\n"
"}\r\n");
}
@ -225,8 +225,8 @@ GenerateBuilderMembers(io::Printer* printer) const {
" result.$name$_.add(builderForValue.build());\r\n"
" return this;\r\n"
"}\r\n"
"public Builder addAll$capitalized_name$(\r\n"
" java.lang.Iterable<? extends $type$> values) {\r\n"
"public Builder addAll$capitalized_name$<T>(\r\n"
" global::System.Collections.Generic.IEnumerable<T> values) where T : $type$ {\r\n"
" if (result.$name$_.isEmpty()) {\r\n"
" result.$name$_ = new java.util.ArrayList<$type$>();\r\n"
" }\r\n"
@ -288,7 +288,7 @@ void RepeatedMessageFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
"for ($type$ element : get$capitalized_name$List()) {\r\n"
" size += com.google.protobuf.CodedOutputStream\r\n"
" size += pb::CodedOutputStream\r\n"
" .compute$group_or_message$Size($number$, element);\r\n"
"}\r\n");
}

@ -44,7 +44,7 @@ const char* PrimitiveTypeName(JavaType type) {
case JAVATYPE_DOUBLE : return "double";
case JAVATYPE_BOOLEAN: return "boolean";
case JAVATYPE_STRING : return "string";
case JAVATYPE_BYTES : return "com.google.protobuf.ByteString";
case JAVATYPE_BYTES : return "pb::ByteString";
case JAVATYPE_ENUM : return NULL;
case JAVATYPE_MESSAGE: return NULL;
@ -128,7 +128,7 @@ string DefaultValue(const FieldDescriptor* field) {
}
if (isBytes && !field->has_default_value()) {
return "com.google.protobuf.ByteString.EMPTY";
return "pb::ByteString.Empty";
}
// Escaping strings correctly for Java and generating efficient
@ -136,7 +136,7 @@ string DefaultValue(const FieldDescriptor* field) {
// whole problem by just grabbing the default value from the descriptor.
return strings::Substitute(
"(($0) $1.getDescriptor().getFields().get($2).getDefaultValue())",
isBytes ? "com.google.protobuf.ByteString" : "string",
isBytes ? "pb::ByteString" : "string",
ClassName(field->containing_type()), field->index());
}
@ -181,27 +181,33 @@ PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {}
void PrimitiveFieldGenerator::
GenerateMembers(io::Printer* printer) const {
printer->Print(variables_,
"private boolean has$capitalized_name$;\r\n"
"private bool has$capitalized_name$;\r\n"
"private $type$ $name$_ = $default$;\r\n"
"public boolean has$capitalized_name$() { return has$capitalized_name$; }\r\n"
"public $type$ get$capitalized_name$() { return $name$_; }\r\n");
"public boolean Has$capitalized_name$ {\r\n"
" get { return has$capitalized_name$; }\r\n"
"}\r\n"
"public $type$ $capitalized_name$ {\r\n"
" get { return $name$_; }\r\n"
"}\r\n");
}
void PrimitiveFieldGenerator::
GenerateBuilderMembers(io::Printer* printer) const {
printer->Print(variables_,
"public boolean has$capitalized_name$() {\r\n"
" return result.has$capitalized_name$();\r\n"
"}\r\n"
"public $type$ get$capitalized_name$() {\r\n"
" return result.get$capitalized_name$();\r\n"
"public boolean Has$capitalized_name$ {\r\n"
" get { return result.Has$capitalized_name$; }\r\n"
"}\r\n"
"public Builder set$capitalized_name$($type$ value) {\r\n"
" result.has$capitalized_name$ = true;\r\n"
" result.$name$_ = value;\r\n"
" return this;\r\n"
// TODO(jonskeet): Consider whether this is really the right pattern,
// or whether we want a method returning a Builder. This allows for
// object initializers.
"public $type$ $capitalized_name$ {\r\n"
" get { return result.$capitalized_name$; }\r\n"
" set {\r\n"
" result.has$capitalized_name$ = true;\r\n"
" result.$name$_ = value;\r\n"
" }\r\n"
"}\r\n"
"public Builder clear$capitalized_name$() {\r\n"
"public Builder Clear$capitalized_name$() {\r\n"
" result.has$capitalized_name$ = false;\r\n"
" result.$name$_ = $default$;\r\n"
" return this;\r\n"
@ -239,7 +245,7 @@ void PrimitiveFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
"if (has$capitalized_name$()) {\r\n"
" size += com.google.protobuf.CodedOutputStream\r\n"
" size += pb::CodedOutputStream\r\n"
" .compute$capitalized_type$Size($number$, get$capitalized_name$());\r\n"
"}\r\n");
}
@ -351,7 +357,7 @@ void RepeatedPrimitiveFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
"for ($type$ element : get$capitalized_name$List()) {\r\n"
" size += com.google.protobuf.CodedOutputStream\r\n"
" size += pb::CodedOutputStream\r\n"
" .compute$capitalized_type$Size($number$, element);\r\n"
"}\r\n");
}

@ -39,7 +39,7 @@ void ServiceGenerator::Generate(io::Printer* printer) {
bool is_own_file = descriptor_->file()->options().csharp_multiple_files();
printer->Print(
"public $static$ abstract class $classname$\r\n"
" implements com.google.protobuf.Service {\r\n",
" implements pb::Service {\r\n",
"static", is_own_file ? "" : "static",
"classname", descriptor_->name());
printer->Indent();
@ -53,20 +53,20 @@ void ServiceGenerator::Generate(io::Printer* printer) {
vars["output"] = ClassName(method->output_type());
printer->Print(vars,
"public abstract void $name$(\r\n"
" com.google.protobuf.RpcController controller,\r\n"
" pb::RpcController controller,\r\n"
" $input$ request,\r\n"
" com.google.protobuf.RpcCallback<$output$> done);\r\n");
" pb::RpcCallback<$output$> done);\r\n");
}
// Generate getDescriptor() and getDescriptorForType().
printer->Print(
"\r\n"
"public static final\r\n"
" com.google.protobuf.Descriptors.ServiceDescriptor\r\n"
" pb::Descriptors.ServiceDescriptor\r\n"
" getDescriptor() {\r\n"
" return $file$.getDescriptor().getServices().get($index$);\r\n"
"}\r\n"
"public final com.google.protobuf.Descriptors.ServiceDescriptor\r\n"
"public final pb::Descriptors.ServiceDescriptor\r\n"
" getDescriptorForType() {\r\n"
" return getDescriptor();\r\n"
"}\r\n",
@ -87,11 +87,11 @@ void ServiceGenerator::GenerateCallMethod(io::Printer* printer) {
printer->Print(
"\r\n"
"public final void callMethod(\r\n"
" com.google.protobuf.Descriptors.MethodDescriptor method,\r\n"
" com.google.protobuf.RpcController controller,\r\n"
" com.google.protobuf.Message request,\r\n"
" com.google.protobuf.RpcCallback<\r\n"
" com.google.protobuf.Message> done) {\r\n"
" pb::Descriptors.MethodDescriptor method,\r\n"
" pb::RpcController controller,\r\n"
" pb::Message request,\r\n"
" pb::RpcCallback<\r\n"
" pb::Message> done) {\r\n"
" if (method.getService() != getDescriptor()) {\r\n"
" throw new java.lang.IllegalArgumentException(\r\n"
" \"Service.callMethod() given method descriptor for wrong \" +\r\n"
@ -111,7 +111,7 @@ void ServiceGenerator::GenerateCallMethod(io::Printer* printer) {
printer->Print(vars,
"case $index$:\r\n"
" this.$method$(controller, ($input$)request,\r\n"
" com.google.protobuf.RpcUtil.<$output$>specializeCallback(\r\n"
" pb::RpcUtil.<$output$>specializeCallback(\r\n"
" done));\r\n"
" return;\r\n");
}
@ -132,9 +132,9 @@ void ServiceGenerator::GenerateCallMethod(io::Printer* printer) {
void ServiceGenerator::GenerateGetPrototype(RequestOrResponse which,
io::Printer* printer) {
printer->Print(
"public final com.google.protobuf.Message\r\n"
"public final pb::Message\r\n"
" get$request_or_response$Prototype(\r\n"
" com.google.protobuf.Descriptors.MethodDescriptor method) {\r\n"
" pb::Descriptors.MethodDescriptor method) {\r\n"
" if (method.getService() != getDescriptor()) {\r\n"
" throw new java.lang.IllegalArgumentException(\r\n"
" \"Service.get$request_or_response$Prototype() given method \" +\r\n"
@ -172,7 +172,7 @@ void ServiceGenerator::GenerateGetPrototype(RequestOrResponse which,
void ServiceGenerator::GenerateStub(io::Printer* printer) {
printer->Print(
"public static Stub newStub(\r\n"
" com.google.protobuf.RpcChannel channel) {\r\n"
" pb::RpcChannel channel) {\r\n"
" return new Stub(channel);\r\n"
"}\r\n"
"\r\n"
@ -181,13 +181,13 @@ void ServiceGenerator::GenerateStub(io::Printer* printer) {
printer->Indent();
printer->Print(
"private Stub(com.google.protobuf.RpcChannel channel) {\r\n"
"private Stub(pb::RpcChannel channel) {\r\n"
" this.channel = channel;\r\n"
"}\r\n"
"\r\n"
"private final com.google.protobuf.RpcChannel channel;\r\n"
"private final pb::RpcChannel channel;\r\n"
"\r\n"
"public com.google.protobuf.RpcChannel getChannel() {\r\n"
"public pb::RpcChannel getChannel() {\r\n"
" return channel;\r\n"
"}\r\n");
@ -201,15 +201,15 @@ void ServiceGenerator::GenerateStub(io::Printer* printer) {
printer->Print(vars,
"\r\n"
"public void $method$(\r\n"
" com.google.protobuf.RpcController controller,\r\n"
" pb::RpcController controller,\r\n"
" $input$ request,\r\n"
" com.google.protobuf.RpcCallback<$output$> done) {\r\n"
" pb::RpcCallback<$output$> done) {\r\n"
" channel.callMethod(\r\n"
" getDescriptor().getMethods().get($index$),\r\n"
" controller,\r\n"
" request,\r\n"
" $output$.getDefaultInstance(),\r\n"
" com.google.protobuf.RpcUtil.generalizeCallback(\r\n"
" pb::RpcUtil.generalizeCallback(\r\n"
" done,\r\n"
" $output$.class,\r\n"
" $output$.getDefaultInstance()));\r\n"

@ -36,7 +36,6 @@ int main(int argc, char* argv[]) {
cli.RegisterGenerator("--java_out", &java_generator,
"Generate Java source file.");
// Proto2 Python
google::protobuf::compiler::python::Generator py_generator;
cli.RegisterGenerator("--python_out", &py_generator,

Loading…
Cancel
Save