diff options
Diffstat (limited to 'src/google/protobuf/compiler/java')
10 files changed, 122 insertions, 77 deletions
diff --git a/src/google/protobuf/compiler/java/java_context.h b/src/google/protobuf/compiler/java/java_context.h index 7f1ce1f9..5b595d07 100644 --- a/src/google/protobuf/compiler/java/java_context.h +++ b/src/google/protobuf/compiler/java/java_context.h @@ -84,7 +84,7 @@ class Context { void InitializeFieldGeneratorInfoForFields( const vector<const FieldDescriptor*>& fields); - scoped_ptr<ClassNameResolver> name_resolver_; + google::protobuf::scoped_ptr<ClassNameResolver> name_resolver_; map<const FieldDescriptor*, FieldGeneratorInfo> field_generator_info_map_; map<const OneofDescriptor*, OneofGeneratorInfo> oneof_generator_info_map_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Context); diff --git a/src/google/protobuf/compiler/java/java_field.cc b/src/google/protobuf/compiler/java/java_field.cc index 49635bb4..af9978e2 100644 --- a/src/google/protobuf/compiler/java/java_field.cc +++ b/src/google/protobuf/compiler/java/java_field.cc @@ -159,7 +159,7 @@ template <> FieldGeneratorMap<ImmutableFieldGenerator>::FieldGeneratorMap( const Descriptor* descriptor, Context* context) : descriptor_(descriptor), - field_generators_(new scoped_ptr< + field_generators_(new google::protobuf::scoped_ptr< ImmutableFieldGenerator>[descriptor->field_count()]) { // Construct all the FieldGenerators and assign them bit indices for their diff --git a/src/google/protobuf/compiler/java/java_field.h b/src/google/protobuf/compiler/java/java_field.h index 5bc5634e..e8d6f3a2 100644 --- a/src/google/protobuf/compiler/java/java_field.h +++ b/src/google/protobuf/compiler/java/java_field.h @@ -108,7 +108,7 @@ class FieldGeneratorMap { const Descriptor* descriptor_; Context* context_; ClassNameResolver* name_resolver_; - scoped_array<scoped_ptr<FieldGeneratorType> > field_generators_; + google::protobuf::scoped_array<google::protobuf::scoped_ptr<FieldGeneratorType> > field_generators_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap); }; diff --git a/src/google/protobuf/compiler/java/java_file.cc b/src/google/protobuf/compiler/java/java_file.cc index 1ac945cd..f1e3cf67 100644 --- a/src/google/protobuf/compiler/java/java_file.cc +++ b/src/google/protobuf/compiler/java/java_file.cc @@ -121,7 +121,7 @@ void CollectExtensions(const FileDescriptorProto& file_proto, "descriptor.proto is not in the transitive dependencies. " "This normally should not happen. Please report a bug."; DynamicMessageFactory factory; - scoped_ptr<Message> dynamic_file_proto( + google::protobuf::scoped_ptr<Message> dynamic_file_proto( factory.GetPrototype(file_proto_desc)->New()); GOOGLE_CHECK(dynamic_file_proto.get() != NULL); GOOGLE_CHECK(dynamic_file_proto->ParseFromString(file_data)); @@ -146,9 +146,9 @@ FileGenerator::FileGenerator(const FileDescriptor* file, bool immutable_api) : file_(file), java_package_(FileJavaPackage(file, immutable_api)), message_generators_( - new scoped_ptr<MessageGenerator>[file->message_type_count()]), + new google::protobuf::scoped_ptr<MessageGenerator>[file->message_type_count()]), extension_generators_( - new scoped_ptr<ExtensionGenerator>[file->extension_count()]), + new google::protobuf::scoped_ptr<ExtensionGenerator>[file->extension_count()]), context_(new Context(file)), name_resolver_(context_->GetNameResolver()), immutable_api_(immutable_api) { @@ -240,7 +240,7 @@ void FileGenerator::Generate(io::Printer* printer) { } if (HasGenericServices(file_)) { for (int i = 0; i < file_->service_count(); i++) { - scoped_ptr<ServiceGenerator> generator( + google::protobuf::scoped_ptr<ServiceGenerator> generator( generator_factory_->NewServiceGenerator(file_->service(i))); generator->Generate(printer); } @@ -340,7 +340,7 @@ void FileGenerator::GenerateDescriptorInitializationCodeForImmutable( "com.google.protobuf.ExtensionRegistry registry =\n" " com.google.protobuf.ExtensionRegistry.newInstance();\n"); for (int i = 0; i < extensions.size(); i++) { - scoped_ptr<ExtensionGenerator> generator( + google::protobuf::scoped_ptr<ExtensionGenerator> generator( generator_factory_->NewExtensionGenerator(extensions[i])); generator->GenerateRegistrationCode(printer); } @@ -444,7 +444,7 @@ static void GenerateSibling(const string& package_dir, string filename = package_dir + descriptor->name() + name_suffix + ".java"; file_list->push_back(filename); - scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename)); + google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename)); io::Printer printer(output.get(), '$'); printer.Print( @@ -492,7 +492,7 @@ void FileGenerator::GenerateSiblings(const string& package_dir, } if (HasGenericServices(file_)) { for (int i = 0; i < file_->service_count(); i++) { - scoped_ptr<ServiceGenerator> generator( + google::protobuf::scoped_ptr<ServiceGenerator> generator( generator_factory_->NewServiceGenerator(file_->service(i))); GenerateSibling<ServiceGenerator>(package_dir, java_package_, file_->service(i), diff --git a/src/google/protobuf/compiler/java/java_file.h b/src/google/protobuf/compiler/java/java_file.h index 0b2230a0..080b3424 100644 --- a/src/google/protobuf/compiler/java/java_file.h +++ b/src/google/protobuf/compiler/java/java_file.h @@ -99,10 +99,10 @@ class FileGenerator { string java_package_; string classname_; - scoped_array<scoped_ptr<MessageGenerator> > message_generators_; - scoped_array<scoped_ptr<ExtensionGenerator> > extension_generators_; - scoped_ptr<GeneratorFactory> generator_factory_; - scoped_ptr<Context> context_; + google::protobuf::scoped_array<google::protobuf::scoped_ptr<MessageGenerator> > message_generators_; + google::protobuf::scoped_array<google::protobuf::scoped_ptr<ExtensionGenerator> > extension_generators_; + google::protobuf::scoped_ptr<GeneratorFactory> generator_factory_; + google::protobuf::scoped_ptr<Context> context_; ClassNameResolver* name_resolver_; bool immutable_api_; diff --git a/src/google/protobuf/compiler/java/java_generator.cc b/src/google/protobuf/compiler/java/java_generator.cc index a743ce22..6c6f7286 100644 --- a/src/google/protobuf/compiler/java/java_generator.cc +++ b/src/google/protobuf/compiler/java/java_generator.cc @@ -130,7 +130,7 @@ bool JavaGenerator::Generate(const FileDescriptor* file, all_files.push_back(java_filename); // Generate main java file. - scoped_ptr<io::ZeroCopyOutputStream> output( + google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output( context->Open(java_filename)); io::Printer printer(output.get(), '$'); file_generator->Generate(&printer); @@ -148,7 +148,7 @@ bool JavaGenerator::Generate(const FileDescriptor* file, if (!output_list_file.empty()) { // Generate output list. This is just a simple text file placed in a // deterministic location which lists the .java files being generated. - scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output( + google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output( context->Open(output_list_file)); io::Printer srclist_printer(srclist_raw_output.get(), '$'); for (int i = 0; i < all_files.size(); i++) { diff --git a/src/google/protobuf/compiler/java/java_message.cc b/src/google/protobuf/compiler/java/java_message.cc index dd1ad6a6..70f379bc 100644 --- a/src/google/protobuf/compiler/java/java_message.cc +++ b/src/google/protobuf/compiler/java/java_message.cc @@ -70,6 +70,13 @@ bool GenerateHasBits(const Descriptor* descriptor) { return SupportFieldPresence(descriptor->file()) || HasRepeatedFields(descriptor); } + +string MapValueImmutableClassdName(const Descriptor* descriptor, + ClassNameResolver* name_resolver) { + const FieldDescriptor* value_field = descriptor->FindFieldByName("value"); + GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, value_field->type()); + return name_resolver->GetImmutableClassName(value_field->message_type()); +} } // namespace // =================================================================== @@ -323,18 +330,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { printer->Outdent(); printer->Print( "}\n" - "\n" - "private static final $classname$ defaultInstance;\n" - "public static $classname$ getDefaultInstance() {\n" - " return defaultInstance;\n" - "}\n" - "\n" - "public $classname$ getDefaultInstanceForType() {\n" - " return defaultInstance;\n" - "}\n" - "\n", - "classname", descriptor_->name(), - "lite", variables["lite"]); + "\n"); if (HasDescriptorMethods(descriptor_)) { printer->Print( @@ -479,18 +475,25 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { GenerateParseFromMethods(printer); GenerateBuilder(printer); - // Carefully initialize the default instance in such a way that it doesn't - // conflict with other initialization. printer->Print( "\n" - "static {\n" - " defaultInstance = new $classname$();\n" - "}\n" - "\n" "// @@protoc_insertion_point(class_scope:$full_name$)\n", - "classname", descriptor_->name(), "full_name", descriptor_->full_name()); + // Carefully initialize the default instance in such a way that it doesn't + // conflict with other initialization. + printer->Print("private static final $classname$ defaultInstance =\n" + " new $classname$();\n" + "public static $classname$ getDefaultInstance() {\n" + " return defaultInstance;\n" + "}\n" + "\n" + "public $classname$ getDefaultInstanceForType() {\n" + " return defaultInstance;\n" + "}\n" + "\n", + "classname", descriptor_->name()); + // Extensions must be declared after the defaultInstance is initialized // because the defaultInstance is used by the extension to lazily retrieve // the outer class's FileDescriptor. @@ -508,7 +511,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { void ImmutableMessageGenerator:: GenerateMessageSerializationMethods(io::Printer* printer) { - scoped_array<const FieldDescriptor*> sorted_fields( + google::protobuf::scoped_array<const FieldDescriptor * > sorted_fields( SortFieldsByNumber(descriptor_)); vector<const Descriptor::ExtensionRange*> sorted_extensions; @@ -913,22 +916,33 @@ GenerateDescriptorMethods(io::Printer* printer) { void ImmutableMessageGenerator:: GenerateCommonBuilderMethods(io::Printer* printer) { - printer->Print( - "// Construct using $classname$.newBuilder()\n" - "private Builder() {\n" - " maybeForceBuilderInitialization();\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); - if (HasDescriptorMethods(descriptor_)) { printer->Print( + "// Construct using $classname$.newBuilder()\n" + "private Builder() {\n" + " maybeForceBuilderInitialization();\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); + + printer->Print( "private Builder(\n" " com.google.protobuf.GeneratedMessage.BuilderParent parent) {\n" " super(parent);\n" " maybeForceBuilderInitialization();\n" "}\n", "classname", name_resolver_->GetImmutableClassName(descriptor_)); + } else { + // LITE runtime passes along the default instance to implement + // getDefaultInstanceForType() at the GneratedMessageLite level. + printer->Print( + "// Construct using $classname$.newBuilder()\n" + "private Builder() {\n" + " super(defaultInstance);\n" + " maybeForceBuilderInitialization();\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); } @@ -994,25 +1008,33 @@ GenerateCommonBuilderMethods(io::Printer* printer) { "\n", "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), "identifier", UniqueFileScopeIdentifier(descriptor_)); + + // LITE runtime implements this in GeneratedMessageLite. + printer->Print( + "public $classname$ getDefaultInstanceForType() {\n" + " return $classname$.getDefaultInstance();\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); } - printer->Print( - "public $classname$ getDefaultInstanceForType() {\n" - " return $classname$.getDefaultInstance();\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); // ----------------------------------------------------------------- + if (HasDescriptorMethods(descriptor_)) { + // LITE implements build in GeneratedMessageLite to save methods. + printer->Print( + "public $classname$ build() {\n" + " $classname$ result = buildPartial();\n" + " if (!result.isInitialized()) {\n" + " throw newUninitializedMessageException(result);\n" + " }\n" + " return result;\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); + } + printer->Print( - "public $classname$ build() {\n" - " $classname$ result = buildPartial();\n" - " if (!result.isInitialized()) {\n" - " throw newUninitializedMessageException(result);\n" - " }\n" - " return result;\n" - "}\n" - "\n" "public $classname$ buildPartial() {\n" " $classname$ result = new $classname$(this);\n", "classname", name_resolver_->GetImmutableClassName(descriptor_)); @@ -1255,17 +1277,31 @@ void ImmutableMessageGenerator::GenerateIsInitialized( "memoize", memoization ? "memoizedIsInitialized = 0;" : ""); break; case FieldDescriptor::LABEL_REPEATED: - printer->Print( - "for (int i = 0; i < get$name$Count(); i++) {\n" - " if (!get$name$(i).isInitialized()) {\n" - " $memoize$\n" - " return false;\n" - " }\n" - "}\n", - "type", name_resolver_->GetImmutableClassName( - field->message_type()), - "name", info->capitalized_name, - "memoize", memoization ? "memoizedIsInitialized = 0;" : ""); + if (IsMapEntry(field->message_type())) { + printer->Print( + "for ($type$ item : get$name$().values()) {\n" + " if (!item.isInitialized()) {\n" + " $memoize$\n" + " return false;\n" + " }\n" + "}\n", + "type", MapValueImmutableClassdName(field->message_type(), + name_resolver_), + "name", info->capitalized_name, + "memoize", memoization ? "memoizedIsInitialized = 0;" : ""); + } else { + printer->Print( + "for (int i = 0; i < get$name$Count(); i++) {\n" + " if (!get$name$(i).isInitialized()) {\n" + " $memoize$\n" + " return false;\n" + " }\n" + "}\n", + "type", name_resolver_->GetImmutableClassName( + field->message_type()), + "name", info->capitalized_name, + "memoize", memoization ? "memoizedIsInitialized = 0;" : ""); + } break; } } @@ -1440,7 +1476,7 @@ GenerateExtensionRegistrationCode(io::Printer* printer) { // =================================================================== void ImmutableMessageGenerator:: GenerateParsingConstructor(io::Printer* printer) { - scoped_array<const FieldDescriptor*> sorted_fields( + google::protobuf::scoped_array<const FieldDescriptor * > sorted_fields( SortFieldsByNumber(descriptor_)); printer->Print( @@ -1596,9 +1632,18 @@ GenerateParsingConstructor(io::Printer* printer) { printer->Print("this.unknownFields = unknownFields.build();\n"); } - // Make extensions immutable. - printer->Print( - "makeExtensionsImmutable();\n"); + if (!HasDescriptorMethods(descriptor_)) { + // LITE runtime uses a static method to reduce method count. + if (descriptor_->extension_range_count() > 0) { + // Make extensions immutable. + printer->Print( + "makeExtensionsImmutable(extensions);\n"); + } + } else { + // Make extensions immutable. + printer->Print( + "makeExtensionsImmutable();\n"); + } printer->Outdent(); printer->Outdent(); diff --git a/src/google/protobuf/compiler/java/java_plugin_unittest.cc b/src/google/protobuf/compiler/java/java_plugin_unittest.cc index 45aa8ffe..fe527623 100644 --- a/src/google/protobuf/compiler/java/java_plugin_unittest.cc +++ b/src/google/protobuf/compiler/java/java_plugin_unittest.cc @@ -75,7 +75,7 @@ class TestGenerator : public CodeGenerator { void TryInsert(const string& filename, const string& insertion_point, GeneratorContext* context) const { - scoped_ptr<io::ZeroCopyOutputStream> output( + google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output( context->OpenForInsert(filename, insertion_point)); io::Printer printer(output.get(), '$'); printer.Print("// inserted $name$\n", "name", insertion_point); diff --git a/src/google/protobuf/compiler/java/java_shared_code_generator.cc b/src/google/protobuf/compiler/java/java_shared_code_generator.cc index f8723f2a..2e61ea8a 100644 --- a/src/google/protobuf/compiler/java/java_shared_code_generator.cc +++ b/src/google/protobuf/compiler/java/java_shared_code_generator.cc @@ -68,8 +68,8 @@ void SharedCodeGenerator::Generate(GeneratorContext* context, string classname = name_resolver_->GetDescriptorClassName(file_); string filename = package_dir + classname + ".java"; file_list->push_back(filename); - scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename)); - scoped_ptr<io::Printer> printer(new io::Printer(output.get(), '$')); + google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename)); + google::protobuf::scoped_ptr<io::Printer> printer(new io::Printer(output.get(), '$')); printer->Print( "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" diff --git a/src/google/protobuf/compiler/java/java_shared_code_generator.h b/src/google/protobuf/compiler/java/java_shared_code_generator.h index fdbe2ce8..38a32fc2 100644 --- a/src/google/protobuf/compiler/java/java_shared_code_generator.h +++ b/src/google/protobuf/compiler/java/java_shared_code_generator.h @@ -80,7 +80,7 @@ class SharedCodeGenerator { // improve compatibility with version 1 of protocol buffers. bool ShouldIncludeDependency(const FileDescriptor* descriptor); - scoped_ptr<ClassNameResolver> name_resolver_; + google::protobuf::scoped_ptr<ClassNameResolver> name_resolver_; const FileDescriptor* file_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SharedCodeGenerator); }; |