• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/java_generator.h>
36 
37 #include <memory>
38 #ifndef _SHARED_PTR_H
39 #include <google/protobuf/stubs/shared_ptr.h>
40 #endif
41 
42 #include <google/protobuf/compiler/java/java_file.h>
43 #include <google/protobuf/compiler/java/java_generator_factory.h>
44 #include <google/protobuf/compiler/java/java_helpers.h>
45 #include <google/protobuf/compiler/java/java_shared_code_generator.h>
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 
56 
JavaGenerator()57 JavaGenerator::JavaGenerator() {}
~JavaGenerator()58 JavaGenerator::~JavaGenerator() {}
59 
Generate(const FileDescriptor * file,const string & parameter,GeneratorContext * context,string * error) const60 bool JavaGenerator::Generate(const FileDescriptor* file,
61                              const string& parameter,
62                              GeneratorContext* context,
63                              string* error) const {
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 
71 
72   vector<pair<string, string> > options;
73   ParseGeneratorParameter(parameter, &options);
74 
75   bool generate_immutable_code = false;
76   bool generate_mutable_code = false;
77   bool generate_shared_code = false;
78   bool enforce_lite = false;
79   for (int i = 0; i < options.size(); i++) {
80     if (options[i].first == "output_list_file") {
81       output_list_file = options[i].second;
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;
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;
92     } else {
93       *error = "Unknown generator option: " + options[i].first;
94       return false;
95     }
96   }
97 
98   if (enforce_lite && generate_mutable_code) {
99     *error = "lite runtime generator option cannot be used with mutable API.";
100     return false;
101   }
102 
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 
110   // -----------------------------------------------------------------
111 
112 
113   vector<string> all_files;
114 
115 
116   vector<FileGenerator*> file_generators;
117   if (generate_immutable_code) {
118     file_generators.push_back(
119         new FileGenerator(file, /* immutable = */ true, enforce_lite));
120   }
121   if (generate_mutable_code) {
122     file_generators.push_back(
123         new FileGenerator(file, /* mutable = */ false, enforce_lite));
124   }
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   }
133 
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.
145     google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
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();
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.
163     google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output(
164         context->Open(output_list_file));
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
178