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 (at) 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/java_generator.h> 36 37 #include <memory> 38 39 #include <google/protobuf/compiler/java/java_file.h> 40 #include <google/protobuf/compiler/java/java_generator_factory.h> 41 #include <google/protobuf/compiler/java/java_helpers.h> 42 #include <google/protobuf/compiler/java/java_shared_code_generator.h> 43 #include <google/protobuf/io/printer.h> 44 #include <google/protobuf/io/zero_copy_stream.h> 45 #include <google/protobuf/descriptor.pb.h> 46 #include <google/protobuf/stubs/strutil.h> 47 48 namespace google { 49 namespace protobuf { 50 namespace compiler { 51 namespace java { 52 53 54 JavaGenerator::JavaGenerator() {} 55 JavaGenerator::~JavaGenerator() {} 56 57 bool JavaGenerator::Generate(const FileDescriptor* file, 58 const string& parameter, 59 GeneratorContext* context, 60 string* error) const { 61 // ----------------------------------------------------------------- 62 // parse generator options 63 64 // Name a file where we will write a list of generated file names, one 65 // per line. 66 string output_list_file; 67 68 69 vector<pair<string, string> > options; 70 ParseGeneratorParameter(parameter, &options); 71 72 bool generate_immutable_code = false; 73 bool generate_mutable_code = false; 74 bool generate_shared_code = false; 75 for (int i = 0; i < options.size(); i++) { 76 if (options[i].first == "output_list_file") { 77 output_list_file = options[i].second; 78 } else if (options[i].first == "immutable") { 79 generate_immutable_code = true; 80 } else if (options[i].first == "mutable") { 81 generate_mutable_code = true; 82 } else if (options[i].first == "shared") { 83 generate_shared_code = true; 84 } else { 85 *error = "Unknown generator option: " + options[i].first; 86 return false; 87 } 88 } 89 90 // By default we generate immutable code and shared code for immutable API. 91 if (!generate_immutable_code && !generate_mutable_code && 92 !generate_shared_code) { 93 generate_immutable_code = true; 94 generate_shared_code = true; 95 } 96 97 // ----------------------------------------------------------------- 98 99 100 vector<string> all_files; 101 102 vector<FileGenerator*> file_generators; 103 if (generate_immutable_code) { 104 file_generators.push_back(new FileGenerator(file, /* immutable = */ true)); 105 } 106 for (int i = 0; i < file_generators.size(); ++i) { 107 if (!file_generators[i]->Validate(error)) { 108 for (int j = 0; j < file_generators.size(); ++j) { 109 delete file_generators[j]; 110 } 111 return false; 112 } 113 } 114 115 for (int i = 0; i < file_generators.size(); ++i) { 116 FileGenerator* file_generator = file_generators[i]; 117 118 string package_dir = JavaPackageToDir(file_generator->java_package()); 119 120 string java_filename = package_dir; 121 java_filename += file_generator->classname(); 122 java_filename += ".java"; 123 all_files.push_back(java_filename); 124 125 // Generate main java file. 126 scoped_ptr<io::ZeroCopyOutputStream> output( 127 context->Open(java_filename)); 128 io::Printer printer(output.get(), '$'); 129 file_generator->Generate(&printer); 130 131 // Generate sibling files. 132 file_generator->GenerateSiblings(package_dir, context, &all_files); 133 } 134 135 for (int i = 0; i < file_generators.size(); ++i) { 136 delete file_generators[i]; 137 } 138 file_generators.clear(); 139 140 // Generate output list if requested. 141 if (!output_list_file.empty()) { 142 // Generate output list. This is just a simple text file placed in a 143 // deterministic location which lists the .java files being generated. 144 scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output( 145 context->Open(output_list_file)); 146 io::Printer srclist_printer(srclist_raw_output.get(), '$'); 147 for (int i = 0; i < all_files.size(); i++) { 148 srclist_printer.Print("$filename$\n", "filename", all_files[i]); 149 } 150 } 151 152 return true; 153 } 154 155 } // namespace java 156 } // namespace compiler 157 } // namespace protobuf 158 } // namespace google 159