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 | 50 |
1 files changed, 33 insertions, 17 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 74253c3f..52893721 100644 --- a/src/google/protobuf/compiler/java/java_shared_code_generator.cc +++ b/src/google/protobuf/compiler/java/java_shared_code_generator.cc @@ -51,44 +51,53 @@ namespace protobuf { namespace compiler { namespace java { -SharedCodeGenerator::SharedCodeGenerator(const FileDescriptor* file) - : name_resolver_(new ClassNameResolver), - enforce_lite_(false), - 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) { + vector<string>* file_list, + vector<string>* annotation_file_list) { string java_package = FileJavaPackage(file_); string package_dir = JavaPackageToDir(java_package); - if (HasDescriptorMethods(file_, enforce_lite_)) { + 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(), '$')); - + GeneratedCodeInfo annotations; + io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector( + &annotations); + google::protobuf::scoped_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()); @@ -98,6 +107,13 @@ void SharedCodeGenerator::Generate(GeneratorContext* context, " }\n" "}\n"); + if (options_.annotate_code) { + google::protobuf::scoped_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(); } |