diff options
Diffstat (limited to 'src/google/protobuf/compiler/java/java_shared_code_generator.cc')
-rw-r--r-- | src/google/protobuf/compiler/java/java_shared_code_generator.cc | 92 |
1 files changed, 53 insertions, 39 deletions
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 70177367..0cec20b9 100644 --- a/src/google/protobuf/compiler/java/java_shared_code_generator.cc +++ b/src/google/protobuf/compiler/java/java_shared_code_generator.cc @@ -33,16 +33,13 @@ #include <google/protobuf/compiler/java/java_shared_code_generator.h> #include <memory> -#ifndef _SHARED_PTR_H -#include <google/protobuf/stubs/shared_ptr.h> -#endif #include <google/protobuf/compiler/java/java_helpers.h> #include <google/protobuf/compiler/java/java_name_resolver.h> #include <google/protobuf/compiler/code_generator.h> +#include <google/protobuf/descriptor.pb.h> #include <google/protobuf/io/printer.h> #include <google/protobuf/io/zero_copy_stream.h> -#include <google/protobuf/descriptor.pb.h> #include <google/protobuf/descriptor.h> #include <google/protobuf/stubs/strutil.h> @@ -51,43 +48,53 @@ namespace protobuf { namespace compiler { namespace java { -SharedCodeGenerator::SharedCodeGenerator(const FileDescriptor* file) - : name_resolver_(new ClassNameResolver), file_(file) { -} +SharedCodeGenerator::SharedCodeGenerator(const FileDescriptor* file, + const Options& options) + : name_resolver_(new ClassNameResolver), file_(file), options_(options) {} SharedCodeGenerator::~SharedCodeGenerator() { } void SharedCodeGenerator::Generate(GeneratorContext* context, - vector<string>* file_list) { + std::vector<string>* file_list, + std::vector<string>* annotation_file_list) { string java_package = FileJavaPackage(file_); string package_dir = JavaPackageToDir(java_package); - if (HasDescriptorMethods(file_)) { + if (HasDescriptorMethods(file_, options_.enforce_lite)) { // Generate descriptors. string classname = name_resolver_->GetDescriptorClassName(file_); string filename = package_dir + classname + ".java"; file_list->push_back(filename); - google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename)); - google::protobuf::scoped_ptr<io::Printer> printer(new io::Printer(output.get(), '$')); - + std::unique_ptr<io::ZeroCopyOutputStream> output(context->Open(filename)); + GeneratedCodeInfo annotations; + io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector( + &annotations); + std::unique_ptr<io::Printer> printer( + new io::Printer(output.get(), '$', + options_.annotate_code ? &annotation_collector : NULL)); + string info_relative_path = classname + ".java.pb.meta"; + string info_full_path = filename + ".pb.meta"; printer->Print( - "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" - "// source: $filename$\n" - "\n", - "filename", file_->name()); + "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" + "// source: $filename$\n" + "\n", + "filename", file_->name()); if (!java_package.empty()) { printer->Print( "package $package$;\n" "\n", "package", java_package); } + PrintGeneratedAnnotation(printer.get(), '$', + options_.annotate_code ? info_relative_path : ""); printer->Print( - "public final class $classname$ {\n" - " public static com.google.protobuf.Descriptors.FileDescriptor\n" - " descriptor;\n" - " static {\n", - "classname", classname); + "public final class $classname$ {\n" + " public static com.google.protobuf.Descriptors.FileDescriptor\n" + " descriptor;\n" + " static {\n", + "classname", classname); + printer->Annotate("classname", file_->name()); printer->Indent(); printer->Indent(); GenerateDescriptors(printer.get()); @@ -97,12 +104,18 @@ void SharedCodeGenerator::Generate(GeneratorContext* context, " }\n" "}\n"); + if (options_.annotate_code) { + std::unique_ptr<io::ZeroCopyOutputStream> info_output( + context->Open(info_full_path)); + annotations.SerializeToZeroCopyStream(info_output.get()); + annotation_file_list->push_back(info_full_path); + } + printer.reset(); output.reset(); } } - void SharedCodeGenerator::GenerateDescriptors(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 @@ -117,7 +130,6 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { FileDescriptorProto file_proto; file_->CopyTo(&file_proto); - string file_data; file_proto.SerializeToString(&file_data); @@ -125,13 +137,16 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { "java.lang.String[] descriptorData = {\n"); printer->Indent(); - // Only write 40 bytes per line. + // Limit the number of bytes per line. static const int kBytesPerLine = 40; + // Limit the number of lines per string part. + static const int kLinesPerPart = 400; + // Every block of bytes, start a new string literal, in order to avoid the + // 64k length limit. Note that this value needs to be <64k. + static const int kBytesPerPart = kBytesPerLine * kLinesPerPart; for (int i = 0; i < file_data.size(); i += kBytesPerLine) { if (i > 0) { - // Every 400 lines, start a new string literal, in order to avoid the - // 64k length limit. - if (i % 400 == 0) { + if (i % kBytesPerPart == 0) { printer->Print(",\n"); } else { printer->Print(" +\n"); @@ -164,15 +179,19 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { // ----------------------------------------------------------------- // Find out all dependencies. - vector<pair<string, string> > dependencies; + std::vector<std::pair<string, string> > dependencies; for (int i = 0; i < file_->dependency_count(); i++) { - if (ShouldIncludeDependency(file_->dependency(i))) { - string filename = file_->dependency(i)->name(); - string classname = FileJavaPackage(file_->dependency(i)) + "." + - name_resolver_->GetDescriptorClassName( - file_->dependency(i)); - dependencies.push_back(std::make_pair(filename, classname)); + string filename = file_->dependency(i)->name(); + string package = FileJavaPackage(file_->dependency(i)); + string classname = name_resolver_->GetDescriptorClassName( + file_->dependency(i)); + string full_name; + if (package.empty()) { + full_name = classname; + } else { + full_name = package + "." + classname; } + dependencies.push_back(std::make_pair(filename, full_name)); } // ----------------------------------------------------------------- @@ -194,11 +213,6 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { " }, assigner);\n"); } -bool SharedCodeGenerator::ShouldIncludeDependency( - const FileDescriptor* descriptor) { - return true; -} - } // namespace java } // namespace compiler } // namespace protobuf |