blob: a46c7fc41a4a5c73d7822a40c8675d10434a11e7 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
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.com4de8f552014-07-18 00:47:59 +000036
37#include <memory>
Feng Xiao6ef984a2014-11-10 17:34:54 -080038#ifndef _SHARED_PTR_H
39#include <google/protobuf/stubs/shared_ptr.h>
40#endif
jieluo@google.com4de8f552014-07-18 00:47:59 +000041
temporal40ee5512008-07-10 02:12:20 +000042#include <google/protobuf/compiler/java/java_file.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000043#include <google/protobuf/compiler/java/java_generator_factory.h>
temporal40ee5512008-07-10 02:12:20 +000044#include <google/protobuf/compiler/java/java_helpers.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000045#include <google/protobuf/compiler/java/java_shared_code_generator.h>
temporal40ee5512008-07-10 02:12:20 +000046#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
51namespace google {
52namespace protobuf {
53namespace compiler {
54namespace java {
55
kenton@google.comfccb1462009-12-18 02:11:36 +000056
temporal40ee5512008-07-10 02:12:20 +000057JavaGenerator::JavaGenerator() {}
58JavaGenerator::~JavaGenerator() {}
59
60bool JavaGenerator::Generate(const FileDescriptor* file,
61 const string& parameter,
liujisi@google.com33165fe2010-11-02 13:14:58 +000062 GeneratorContext* context,
temporal40ee5512008-07-10 02:12:20 +000063 string* error) const {
temporal40ee5512008-07-10 02:12:20 +000064 // -----------------------------------------------------------------
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.com33165fe2010-11-02 13:14:58 +000071
72 vector<pair<string, string> > options;
73 ParseGeneratorParameter(parameter, &options);
74
jieluo@google.com4de8f552014-07-18 00:47:59 +000075 bool generate_immutable_code = false;
76 bool generate_mutable_code = false;
77 bool generate_shared_code = false;
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070078 bool enforce_lite = false;
temporal40ee5512008-07-10 02:12:20 +000079 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.com4de8f552014-07-18 00:47:59 +000082 } 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 Liu3b3c8ab2016-03-30 11:39:59 -070088 } 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;
temporal40ee5512008-07-10 02:12:20 +000092 } else {
93 *error = "Unknown generator option: " + options[i].first;
94 return false;
95 }
96 }
97
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070098 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.com4de8f552014-07-18 00:47:59 +0000103 // 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
temporal40ee5512008-07-10 02:12:20 +0000110 // -----------------------------------------------------------------
111
112
temporal40ee5512008-07-10 02:12:20 +0000113 vector<string> all_files;
114
Feng Xiao6ef984a2014-11-10 17:34:54 -0800115
jieluo@google.com4de8f552014-07-18 00:47:59 +0000116 vector<FileGenerator*> file_generators;
117 if (generate_immutable_code) {
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700118 file_generators.push_back(
119 new FileGenerator(file, /* immutable = */ true, enforce_lite));
jieluo@google.com4de8f552014-07-18 00:47:59 +0000120 }
Feng Xiao6ef984a2014-11-10 17:34:54 -0800121 if (generate_mutable_code) {
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700122 file_generators.push_back(
123 new FileGenerator(file, /* mutable = */ false, enforce_lite));
Feng Xiao6ef984a2014-11-10 17:34:54 -0800124 }
jieluo@google.com4de8f552014-07-18 00:47:59 +0000125 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 }
temporal40ee5512008-07-10 02:12:20 +0000133
jieluo@google.com4de8f552014-07-18 00:47:59 +0000134 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 Xiaof157a562014-11-14 11:50:31 -0800145 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
jieluo@google.com4de8f552014-07-18 00:47:59 +0000146 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();
temporal40ee5512008-07-10 02:12:20 +0000158
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 Xiaof157a562014-11-14 11:50:31 -0800163 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output(
jieluo@google.com4de8f552014-07-18 00:47:59 +0000164 context->Open(output_list_file));
temporal40ee5512008-07-10 02:12:20 +0000165 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