| 1 | // Protocol Buffers - Google's data interchange format | 
| 2 | // Copyright 2008 Google Inc.  All rights reserved. | 
| 3 | // https://developers.google.com/protocol-buffers/ | 
| 4 | // | 
| 5 | // Redistribution and use in source and binary forms, with or without | 
| 6 | // modification, are permitted provided that the following conditions are | 
| 7 | // met: | 
| 8 | // | 
| 9 | //     * Redistributions of source code must retain the above copyright | 
| 10 | // notice, this list of conditions and the following disclaimer. | 
| 11 | //     * Redistributions in binary form must reproduce the above | 
| 12 | // copyright notice, this list of conditions and the following disclaimer | 
| 13 | // in the documentation and/or other materials provided with the | 
| 14 | // distribution. | 
| 15 | //     * Neither the name of Google Inc. nor the names of its | 
| 16 | // contributors may be used to endorse or promote products derived from | 
| 17 | // this software without specific prior written permission. | 
| 18 | // | 
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
| 30 |  | 
| 31 | // Author: kenton@google.com (Kenton Varda) | 
| 32 | //  Based on original Protocol Buffers design by | 
| 33 | //  Sanjay Ghemawat, Jeff Dean, and others. | 
| 34 |  | 
| 35 | #include <google/protobuf/compiler/java/generator.h> | 
| 36 |  | 
| 37 |  | 
| 38 | #include <memory> | 
| 39 |  | 
| 40 | #include <google/protobuf/io/printer.h> | 
| 41 | #include <google/protobuf/io/zero_copy_stream.h> | 
| 42 | #include <google/protobuf/stubs/stringprintf.h> | 
| 43 | #include <google/protobuf/compiler/java/file.h> | 
| 44 | #include <google/protobuf/compiler/java/generator_factory.h> | 
| 45 | #include <google/protobuf/compiler/java/helpers.h> | 
| 46 | #include <google/protobuf/compiler/java/name_resolver.h> | 
| 47 | #include <google/protobuf/compiler/java/options.h> | 
| 48 | #include <google/protobuf/compiler/java/shared_code_generator.h> | 
| 49 | #include <google/protobuf/descriptor.pb.h> | 
| 50 |  | 
| 51 | #include <google/protobuf/stubs/strutil.h> | 
| 52 |  | 
| 53 | namespace google { | 
| 54 | namespace protobuf { | 
| 55 | namespace compiler { | 
| 56 | namespace java { | 
| 57 |  | 
| 58 |  | 
| 59 | JavaGenerator::JavaGenerator() {} | 
| 60 | JavaGenerator::~JavaGenerator() {} | 
| 61 |  | 
| 62 | uint64_t JavaGenerator::GetSupportedFeatures() const { | 
| 63 |   return CodeGenerator::Feature::FEATURE_PROTO3_OPTIONAL; | 
| 64 | } | 
| 65 |  | 
| 66 | bool JavaGenerator::Generate(const FileDescriptor* file, | 
| 67 |                              const std::string& parameter, | 
| 68 |                              GeneratorContext* context, | 
| 69 |                              std::string* error) const { | 
| 70 |   // ----------------------------------------------------------------- | 
| 71 |   // parse generator options | 
| 72 |  | 
| 73 |   std::vector<std::pair<std::string, std::string> > options; | 
| 74 |   ParseGeneratorParameter(parameter, &options); | 
| 75 |   Options file_options; | 
| 76 |  | 
| 77 |   for (int i = 0; i < options.size(); i++) { | 
| 78 |     if (options[i].first == "output_list_file" ) { | 
| 79 |       file_options.output_list_file = options[i].second; | 
| 80 |     } else if (options[i].first == "immutable" ) { | 
| 81 |       file_options.generate_immutable_code = true; | 
| 82 |     } else if (options[i].first == "mutable" ) { | 
| 83 |       file_options.generate_mutable_code = true; | 
| 84 |     } else if (options[i].first == "shared" ) { | 
| 85 |       file_options.generate_shared_code = true; | 
| 86 |     } else if (options[i].first == "lite" ) { | 
| 87 |       // Note: Java Lite does not guarantee API/ABI stability. We may choose to | 
| 88 |       // break existing API in order to boost performance / reduce code size. | 
| 89 |       file_options.enforce_lite = true; | 
| 90 |     } else if (options[i].first == "annotate_code" ) { | 
| 91 |       file_options.annotate_code = true; | 
| 92 |     } else if (options[i].first == "annotation_list_file" ) { | 
| 93 |       file_options.annotation_list_file = options[i].second; | 
| 94 |     } else { | 
| 95 |       *error = "Unknown generator option: "  + options[i].first; | 
| 96 |       return false; | 
| 97 |     } | 
| 98 |   } | 
| 99 |  | 
| 100 |   if (file_options.enforce_lite && file_options.generate_mutable_code) { | 
| 101 |     *error = "lite runtime generator option cannot be used with mutable API." ; | 
| 102 |     return false; | 
| 103 |   } | 
| 104 |  | 
| 105 |   // By default we generate immutable code and shared code for immutable API. | 
| 106 |   if (!file_options.generate_immutable_code && | 
| 107 |       !file_options.generate_mutable_code && | 
| 108 |       !file_options.generate_shared_code) { | 
| 109 |     file_options.generate_immutable_code = true; | 
| 110 |     file_options.generate_shared_code = true; | 
| 111 |   } | 
| 112 |  | 
| 113 |   // ----------------------------------------------------------------- | 
| 114 |  | 
| 115 |  | 
| 116 |   std::vector<std::string> all_files; | 
| 117 |   std::vector<std::string> all_annotations; | 
| 118 |  | 
| 119 |  | 
| 120 |   std::vector<FileGenerator*> file_generators; | 
| 121 |   if (file_options.generate_immutable_code) { | 
| 122 |     file_generators.push_back(x: new FileGenerator(file, file_options, | 
| 123 |                                                 /* immutable = */ true)); | 
| 124 |   } | 
| 125 |   if (file_options.generate_mutable_code) { | 
| 126 |     file_generators.push_back(x: new FileGenerator(file, file_options, | 
| 127 |                                                 /* mutable = */ false)); | 
| 128 |   } | 
| 129 |  | 
| 130 |   for (int i = 0; i < file_generators.size(); ++i) { | 
| 131 |     if (!file_generators[i]->Validate(error)) { | 
| 132 |       for (int j = 0; j < file_generators.size(); ++j) { | 
| 133 |         delete file_generators[j]; | 
| 134 |       } | 
| 135 |       return false; | 
| 136 |     } | 
| 137 |   } | 
| 138 |  | 
| 139 |   for (int i = 0; i < file_generators.size(); ++i) { | 
| 140 |     FileGenerator* file_generator = file_generators[i]; | 
| 141 |  | 
| 142 |     std::string package_dir = JavaPackageToDir(package_name: file_generator->java_package()); | 
| 143 |  | 
| 144 |     std::string java_filename = package_dir; | 
| 145 |     java_filename += file_generator->classname(); | 
| 146 |     java_filename += ".java" ; | 
| 147 |     all_files.push_back(x: java_filename); | 
| 148 |     std::string info_full_path = java_filename + ".pb.meta" ; | 
| 149 |     if (file_options.annotate_code) { | 
| 150 |       all_annotations.push_back(x: info_full_path); | 
| 151 |     } | 
| 152 |  | 
| 153 |     // Generate main java file. | 
| 154 |     std::unique_ptr<io::ZeroCopyOutputStream> output( | 
| 155 |         context->Open(filename: java_filename)); | 
| 156 |     GeneratedCodeInfo annotations; | 
| 157 |     io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector( | 
| 158 |         &annotations); | 
| 159 |     io::Printer printer( | 
| 160 |         output.get(), '$', | 
| 161 |         file_options.annotate_code ? &annotation_collector : NULL); | 
| 162 |  | 
| 163 |     file_generator->Generate(printer: &printer); | 
| 164 |  | 
| 165 |     // Generate sibling files. | 
| 166 |     file_generator->GenerateSiblings(package_dir, generator_context: context, file_list: &all_files, | 
| 167 |                                      annotation_list: &all_annotations); | 
| 168 |  | 
| 169 |     if (file_options.annotate_code) { | 
| 170 |       std::unique_ptr<io::ZeroCopyOutputStream> info_output( | 
| 171 |           context->Open(filename: info_full_path)); | 
| 172 |       annotations.SerializeToZeroCopyStream(output: info_output.get()); | 
| 173 |     } | 
| 174 |   } | 
| 175 |  | 
| 176 |  | 
| 177 |   for (int i = 0; i < file_generators.size(); ++i) { | 
| 178 |     delete file_generators[i]; | 
| 179 |   } | 
| 180 |   file_generators.clear(); | 
| 181 |  | 
| 182 |   // Generate output list if requested. | 
| 183 |   if (!file_options.output_list_file.empty()) { | 
| 184 |     // Generate output list.  This is just a simple text file placed in a | 
| 185 |     // deterministic location which lists the .java files being generated. | 
| 186 |     std::unique_ptr<io::ZeroCopyOutputStream> srclist_raw_output( | 
| 187 |         context->Open(filename: file_options.output_list_file)); | 
| 188 |     io::Printer srclist_printer(srclist_raw_output.get(), '$'); | 
| 189 |     for (int i = 0; i < all_files.size(); i++) { | 
| 190 |       srclist_printer.Print(text: "$filename$\n" , args: "filename" , args: all_files[i]); | 
| 191 |     } | 
| 192 |   } | 
| 193 |  | 
| 194 |   if (!file_options.annotation_list_file.empty()) { | 
| 195 |     // Generate output list.  This is just a simple text file placed in a | 
| 196 |     // deterministic location which lists the .java files being generated. | 
| 197 |     std::unique_ptr<io::ZeroCopyOutputStream> annotation_list_raw_output( | 
| 198 |         context->Open(filename: file_options.annotation_list_file)); | 
| 199 |     io::Printer annotation_list_printer(annotation_list_raw_output.get(), '$'); | 
| 200 |     for (int i = 0; i < all_annotations.size(); i++) { | 
| 201 |       annotation_list_printer.Print(text: "$filename$\n" , args: "filename" , | 
| 202 |                                     args: all_annotations[i]); | 
| 203 |     } | 
| 204 |   } | 
| 205 |  | 
| 206 |   return true; | 
| 207 | } | 
| 208 |  | 
| 209 | }  // namespace java | 
| 210 | }  // namespace compiler | 
| 211 | }  // namespace protobuf | 
| 212 | }  // namespace google | 
| 213 |  |