temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 3 | // https://developers.google.com/protocol-buffers/ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 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/java_generator.h> |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 36 | |
| 37 | #include <memory> |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 38 | #ifndef _SHARED_PTR_H |
| 39 | #include <google/protobuf/stubs/shared_ptr.h> |
| 40 | #endif |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 41 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 42 | #include <google/protobuf/compiler/java/java_file.h> |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 43 | #include <google/protobuf/compiler/java/java_generator_factory.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 44 | #include <google/protobuf/compiler/java/java_helpers.h> |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 45 | #include <google/protobuf/compiler/java/java_shared_code_generator.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 46 | #include <google/protobuf/io/printer.h> |
| 47 | #include <google/protobuf/io/zero_copy_stream.h> |
| 48 | #include <google/protobuf/descriptor.pb.h> |
| 49 | #include <google/protobuf/stubs/strutil.h> |
| 50 | |
| 51 | namespace google { |
| 52 | namespace protobuf { |
| 53 | namespace compiler { |
| 54 | namespace java { |
| 55 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 56 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 57 | JavaGenerator::JavaGenerator() {} |
| 58 | JavaGenerator::~JavaGenerator() {} |
| 59 | |
| 60 | bool JavaGenerator::Generate(const FileDescriptor* file, |
| 61 | const string& parameter, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 62 | GeneratorContext* context, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 63 | string* error) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 64 | // ----------------------------------------------------------------- |
| 65 | // parse generator options |
| 66 | |
| 67 | // Name a file where we will write a list of generated file names, one |
| 68 | // per line. |
| 69 | string output_list_file; |
| 70 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 71 | |
| 72 | vector<pair<string, string> > options; |
| 73 | ParseGeneratorParameter(parameter, &options); |
| 74 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 75 | bool generate_immutable_code = false; |
| 76 | bool generate_mutable_code = false; |
| 77 | bool generate_shared_code = false; |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 78 | bool enforce_lite = false; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 79 | for (int i = 0; i < options.size(); i++) { |
| 80 | if (options[i].first == "output_list_file") { |
| 81 | output_list_file = options[i].second; |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 82 | } else if (options[i].first == "immutable") { |
| 83 | generate_immutable_code = true; |
| 84 | } else if (options[i].first == "mutable") { |
| 85 | generate_mutable_code = true; |
| 86 | } else if (options[i].first == "shared") { |
| 87 | generate_shared_code = true; |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 88 | } else if (options[i].first == "lite") { |
| 89 | // When set, the protoc will generate the current files and all the |
| 90 | // transitive dependencies as lite runtime. |
| 91 | enforce_lite = true; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 92 | } else { |
| 93 | *error = "Unknown generator option: " + options[i].first; |
| 94 | return false; |
| 95 | } |
| 96 | } |
| 97 | |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 98 | if (enforce_lite && generate_mutable_code) { |
| 99 | *error = "lite runtime generator option cannot be used with mutable API."; |
| 100 | return false; |
| 101 | } |
| 102 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 103 | // By default we generate immutable code and shared code for immutable API. |
| 104 | if (!generate_immutable_code && !generate_mutable_code && |
| 105 | !generate_shared_code) { |
| 106 | generate_immutable_code = true; |
| 107 | generate_shared_code = true; |
| 108 | } |
| 109 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 110 | // ----------------------------------------------------------------- |
| 111 | |
| 112 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 113 | vector<string> all_files; |
| 114 | |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 115 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 116 | vector<FileGenerator*> file_generators; |
| 117 | if (generate_immutable_code) { |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 118 | file_generators.push_back( |
| 119 | new FileGenerator(file, /* immutable = */ true, enforce_lite)); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 120 | } |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 121 | if (generate_mutable_code) { |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 122 | file_generators.push_back( |
| 123 | new FileGenerator(file, /* mutable = */ false, enforce_lite)); |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 124 | } |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 125 | for (int i = 0; i < file_generators.size(); ++i) { |
| 126 | if (!file_generators[i]->Validate(error)) { |
| 127 | for (int j = 0; j < file_generators.size(); ++j) { |
| 128 | delete file_generators[j]; |
| 129 | } |
| 130 | return false; |
| 131 | } |
| 132 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 133 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 134 | for (int i = 0; i < file_generators.size(); ++i) { |
| 135 | FileGenerator* file_generator = file_generators[i]; |
| 136 | |
| 137 | string package_dir = JavaPackageToDir(file_generator->java_package()); |
| 138 | |
| 139 | string java_filename = package_dir; |
| 140 | java_filename += file_generator->classname(); |
| 141 | java_filename += ".java"; |
| 142 | all_files.push_back(java_filename); |
| 143 | |
| 144 | // Generate main java file. |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 145 | google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output( |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 146 | context->Open(java_filename)); |
| 147 | io::Printer printer(output.get(), '$'); |
| 148 | file_generator->Generate(&printer); |
| 149 | |
| 150 | // Generate sibling files. |
| 151 | file_generator->GenerateSiblings(package_dir, context, &all_files); |
| 152 | } |
| 153 | |
| 154 | for (int i = 0; i < file_generators.size(); ++i) { |
| 155 | delete file_generators[i]; |
| 156 | } |
| 157 | file_generators.clear(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 158 | |
| 159 | // Generate output list if requested. |
| 160 | if (!output_list_file.empty()) { |
| 161 | // Generate output list. This is just a simple text file placed in a |
| 162 | // deterministic location which lists the .java files being generated. |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 163 | google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output( |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 164 | context->Open(output_list_file)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 165 | io::Printer srclist_printer(srclist_raw_output.get(), '$'); |
| 166 | for (int i = 0; i < all_files.size(); i++) { |
| 167 | srclist_printer.Print("$filename$\n", "filename", all_files[i]); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | } // namespace java |
| 175 | } // namespace compiler |
| 176 | } // namespace protobuf |
| 177 | } // namespace google |