| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 #include <google/protobuf/compiler/java/java_generator.h> | 35 #include <google/protobuf/compiler/java/java_generator.h> |
| 36 | 36 |
| 37 #include <memory> | 37 #include <memory> |
| 38 #ifndef _SHARED_PTR_H | 38 #ifndef _SHARED_PTR_H |
| 39 #include <google/protobuf/stubs/shared_ptr.h> | 39 #include <google/protobuf/stubs/shared_ptr.h> |
| 40 #endif | 40 #endif |
| 41 | 41 |
| 42 #include <google/protobuf/compiler/java/java_file.h> | 42 #include <google/protobuf/compiler/java/java_file.h> |
| 43 #include <google/protobuf/compiler/java/java_generator_factory.h> | 43 #include <google/protobuf/compiler/java/java_generator_factory.h> |
| 44 #include <google/protobuf/compiler/java/java_helpers.h> | 44 #include <google/protobuf/compiler/java/java_helpers.h> |
| 45 #include <google/protobuf/compiler/java/java_options.h> | |
| 46 #include <google/protobuf/compiler/java/java_shared_code_generator.h> | 45 #include <google/protobuf/compiler/java/java_shared_code_generator.h> |
| 47 #include <google/protobuf/io/printer.h> | 46 #include <google/protobuf/io/printer.h> |
| 48 #include <google/protobuf/io/zero_copy_stream.h> | 47 #include <google/protobuf/io/zero_copy_stream.h> |
| 49 #include <google/protobuf/descriptor.pb.h> | 48 #include <google/protobuf/descriptor.pb.h> |
| 50 #include <google/protobuf/stubs/strutil.h> | 49 #include <google/protobuf/stubs/strutil.h> |
| 51 | 50 |
| 52 namespace google { | 51 namespace google { |
| 53 namespace protobuf { | 52 namespace protobuf { |
| 54 namespace compiler { | 53 namespace compiler { |
| 55 namespace java { | 54 namespace java { |
| 56 | 55 |
| 57 | 56 |
| 58 JavaGenerator::JavaGenerator() {} | 57 JavaGenerator::JavaGenerator() {} |
| 59 JavaGenerator::~JavaGenerator() {} | 58 JavaGenerator::~JavaGenerator() {} |
| 60 | 59 |
| 61 bool JavaGenerator::Generate(const FileDescriptor* file, | 60 bool JavaGenerator::Generate(const FileDescriptor* file, |
| 62 const string& parameter, | 61 const string& parameter, |
| 63 GeneratorContext* context, | 62 GeneratorContext* context, |
| 64 string* error) const { | 63 string* error) const { |
| 65 // ----------------------------------------------------------------- | 64 // ----------------------------------------------------------------- |
| 66 // parse generator options | 65 // parse generator options |
| 67 | 66 |
| 67 // Name a file where we will write a list of generated file names, one |
| 68 // per line. |
| 69 string output_list_file; |
| 68 | 70 |
| 69 std::vector<std::pair<string, string> > options; | 71 |
| 72 vector<pair<string, string> > options; |
| 70 ParseGeneratorParameter(parameter, &options); | 73 ParseGeneratorParameter(parameter, &options); |
| 71 Options file_options; | |
| 72 | 74 |
| 75 bool generate_immutable_code = false; |
| 76 bool generate_mutable_code = false; |
| 77 bool generate_shared_code = false; |
| 78 bool enforce_lite = false; |
| 73 for (int i = 0; i < options.size(); i++) { | 79 for (int i = 0; i < options.size(); i++) { |
| 74 if (options[i].first == "output_list_file") { | 80 if (options[i].first == "output_list_file") { |
| 75 file_options.output_list_file = options[i].second; | 81 output_list_file = options[i].second; |
| 76 } else if (options[i].first == "immutable") { | 82 } else if (options[i].first == "immutable") { |
| 77 file_options.generate_immutable_code = true; | 83 generate_immutable_code = true; |
| 78 } else if (options[i].first == "mutable") { | 84 } else if (options[i].first == "mutable") { |
| 79 file_options.generate_mutable_code = true; | 85 generate_mutable_code = true; |
| 80 } else if (options[i].first == "shared") { | 86 } else if (options[i].first == "shared") { |
| 81 file_options.generate_shared_code = true; | 87 generate_shared_code = true; |
| 82 } else if (options[i].first == "annotate_code") { | 88 } else if (options[i].first == "lite") { |
| 83 file_options.annotate_code = true; | 89 // When set, the protoc will generate the current files and all the |
| 84 } else if (options[i].first == "annotation_list_file") { | 90 // transitive dependencies as lite runtime. |
| 85 file_options.annotation_list_file = options[i].second; | 91 enforce_lite = true; |
| 86 } else { | 92 } else { |
| 87 *error = "Unknown generator option: " + options[i].first; | 93 *error = "Unknown generator option: " + options[i].first; |
| 88 return false; | 94 return false; |
| 89 } | 95 } |
| 90 } | 96 } |
| 91 | 97 |
| 92 if (file_options.enforce_lite && file_options.generate_mutable_code) { | 98 if (enforce_lite && generate_mutable_code) { |
| 93 *error = "lite runtime generator option cannot be used with mutable API."; | 99 *error = "lite runtime generator option cannot be used with mutable API."; |
| 94 return false; | 100 return false; |
| 95 } | 101 } |
| 96 | 102 |
| 97 // By default we generate immutable code and shared code for immutable API. | 103 // By default we generate immutable code and shared code for immutable API. |
| 98 if (!file_options.generate_immutable_code && | 104 if (!generate_immutable_code && !generate_mutable_code && |
| 99 !file_options.generate_mutable_code && | 105 !generate_shared_code) { |
| 100 !file_options.generate_shared_code) { | 106 generate_immutable_code = true; |
| 101 file_options.generate_immutable_code = true; | 107 generate_shared_code = true; |
| 102 file_options.generate_shared_code = true; | |
| 103 } | 108 } |
| 104 | 109 |
| 105 // ----------------------------------------------------------------- | 110 // ----------------------------------------------------------------- |
| 106 | 111 |
| 107 | 112 |
| 108 std::vector<string> all_files; | 113 vector<string> all_files; |
| 109 std::vector<string> all_annotations; | |
| 110 | 114 |
| 111 | 115 |
| 112 std::vector<FileGenerator*> file_generators; | 116 vector<FileGenerator*> file_generators; |
| 113 if (file_options.generate_immutable_code) { | 117 if (generate_immutable_code) { |
| 114 file_generators.push_back(new FileGenerator(file, file_options, | 118 file_generators.push_back( |
| 115 /* immutable = */ true)); | 119 new FileGenerator(file, /* immutable = */ true, enforce_lite)); |
| 116 } | 120 } |
| 117 if (file_options.generate_mutable_code) { | 121 if (generate_mutable_code) { |
| 118 file_generators.push_back(new FileGenerator(file, file_options, | 122 file_generators.push_back( |
| 119 /* mutable = */ false)); | 123 new FileGenerator(file, /* mutable = */ false, enforce_lite)); |
| 120 } | 124 } |
| 121 for (int i = 0; i < file_generators.size(); ++i) { | 125 for (int i = 0; i < file_generators.size(); ++i) { |
| 122 if (!file_generators[i]->Validate(error)) { | 126 if (!file_generators[i]->Validate(error)) { |
| 123 for (int j = 0; j < file_generators.size(); ++j) { | 127 for (int j = 0; j < file_generators.size(); ++j) { |
| 124 delete file_generators[j]; | 128 delete file_generators[j]; |
| 125 } | 129 } |
| 126 return false; | 130 return false; |
| 127 } | 131 } |
| 128 } | 132 } |
| 129 | 133 |
| 130 for (int i = 0; i < file_generators.size(); ++i) { | 134 for (int i = 0; i < file_generators.size(); ++i) { |
| 131 FileGenerator* file_generator = file_generators[i]; | 135 FileGenerator* file_generator = file_generators[i]; |
| 132 | 136 |
| 133 string package_dir = JavaPackageToDir(file_generator->java_package()); | 137 string package_dir = JavaPackageToDir(file_generator->java_package()); |
| 134 | 138 |
| 135 string java_filename = package_dir; | 139 string java_filename = package_dir; |
| 136 java_filename += file_generator->classname(); | 140 java_filename += file_generator->classname(); |
| 137 java_filename += ".java"; | 141 java_filename += ".java"; |
| 138 all_files.push_back(java_filename); | 142 all_files.push_back(java_filename); |
| 139 string info_full_path = java_filename + ".pb.meta"; | |
| 140 if (file_options.annotate_code) { | |
| 141 all_annotations.push_back(info_full_path); | |
| 142 } | |
| 143 | 143 |
| 144 // Generate main java file. | 144 // Generate main java file. |
| 145 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output( | 145 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output( |
| 146 context->Open(java_filename)); | 146 context->Open(java_filename)); |
| 147 GeneratedCodeInfo annotations; | 147 io::Printer printer(output.get(), '$'); |
| 148 io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector( | |
| 149 &annotations); | |
| 150 io::Printer printer(output.get(), '$', file_options.annotate_code | |
| 151 ? &annotation_collector | |
| 152 : NULL); | |
| 153 | |
| 154 file_generator->Generate(&printer); | 148 file_generator->Generate(&printer); |
| 155 | 149 |
| 156 // Generate sibling files. | 150 // Generate sibling files. |
| 157 file_generator->GenerateSiblings(package_dir, context, &all_files, | 151 file_generator->GenerateSiblings(package_dir, context, &all_files); |
| 158 &all_annotations); | |
| 159 | |
| 160 if (file_options.annotate_code) { | |
| 161 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> info_output( | |
| 162 context->Open(info_full_path)); | |
| 163 annotations.SerializeToZeroCopyStream(info_output.get()); | |
| 164 } | |
| 165 } | 152 } |
| 166 | 153 |
| 167 for (int i = 0; i < file_generators.size(); ++i) { | 154 for (int i = 0; i < file_generators.size(); ++i) { |
| 168 delete file_generators[i]; | 155 delete file_generators[i]; |
| 169 } | 156 } |
| 170 file_generators.clear(); | 157 file_generators.clear(); |
| 171 | 158 |
| 172 // Generate output list if requested. | 159 // Generate output list if requested. |
| 173 if (!file_options.output_list_file.empty()) { | 160 if (!output_list_file.empty()) { |
| 174 // Generate output list. This is just a simple text file placed in a | 161 // Generate output list. This is just a simple text file placed in a |
| 175 // deterministic location which lists the .java files being generated. | 162 // deterministic location which lists the .java files being generated. |
| 176 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output( | 163 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output( |
| 177 context->Open(file_options.output_list_file)); | 164 context->Open(output_list_file)); |
| 178 io::Printer srclist_printer(srclist_raw_output.get(), '$'); | 165 io::Printer srclist_printer(srclist_raw_output.get(), '$'); |
| 179 for (int i = 0; i < all_files.size(); i++) { | 166 for (int i = 0; i < all_files.size(); i++) { |
| 180 srclist_printer.Print("$filename$\n", "filename", all_files[i]); | 167 srclist_printer.Print("$filename$\n", "filename", all_files[i]); |
| 181 } | 168 } |
| 182 } | 169 } |
| 183 | 170 |
| 184 if (!file_options.annotation_list_file.empty()) { | |
| 185 // Generate output list. This is just a simple text file placed in a | |
| 186 // deterministic location which lists the .java files being generated. | |
| 187 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> annotation_list_raw_o
utput( | |
| 188 context->Open(file_options.annotation_list_file)); | |
| 189 io::Printer annotation_list_printer(annotation_list_raw_output.get(), '$'); | |
| 190 for (int i = 0; i < all_annotations.size(); i++) { | |
| 191 annotation_list_printer.Print("$filename$\n", "filename", | |
| 192 all_annotations[i]); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 return true; | 171 return true; |
| 197 } | 172 } |
| 198 | 173 |
| 199 } // namespace java | 174 } // namespace java |
| 200 } // namespace compiler | 175 } // namespace compiler |
| 201 } // namespace protobuf | 176 } // namespace protobuf |
| 202 } // namespace google | 177 } // namespace google |
| OLD | NEW |