blob: 6c6f728619784b1f5608a8b80b5d7680a00a4422 [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;
temporal40ee5512008-07-10 02:12:20 +000078 for (int i = 0; i < options.size(); i++) {
79 if (options[i].first == "output_list_file") {
80 output_list_file = options[i].second;
jieluo@google.com4de8f552014-07-18 00:47:59 +000081 } else if (options[i].first == "immutable") {
82 generate_immutable_code = true;
83 } else if (options[i].first == "mutable") {
84 generate_mutable_code = true;
85 } else if (options[i].first == "shared") {
86 generate_shared_code = true;
temporal40ee5512008-07-10 02:12:20 +000087 } else {
88 *error = "Unknown generator option: " + options[i].first;
89 return false;
90 }
91 }
92
jieluo@google.com4de8f552014-07-18 00:47:59 +000093 // By default we generate immutable code and shared code for immutable API.
94 if (!generate_immutable_code && !generate_mutable_code &&
95 !generate_shared_code) {
96 generate_immutable_code = true;
97 generate_shared_code = true;
98 }
99
temporal40ee5512008-07-10 02:12:20 +0000100 // -----------------------------------------------------------------
101
102
temporal40ee5512008-07-10 02:12:20 +0000103 vector<string> all_files;
104
Feng Xiao6ef984a2014-11-10 17:34:54 -0800105
jieluo@google.com4de8f552014-07-18 00:47:59 +0000106 vector<FileGenerator*> file_generators;
107 if (generate_immutable_code) {
108 file_generators.push_back(new FileGenerator(file, /* immutable = */ true));
109 }
Feng Xiao6ef984a2014-11-10 17:34:54 -0800110 if (generate_mutable_code) {
111 file_generators.push_back(new FileGenerator(file, /* mutable = */ false));
112 }
jieluo@google.com4de8f552014-07-18 00:47:59 +0000113 for (int i = 0; i < file_generators.size(); ++i) {
114 if (!file_generators[i]->Validate(error)) {
115 for (int j = 0; j < file_generators.size(); ++j) {
116 delete file_generators[j];
117 }
118 return false;
119 }
120 }
temporal40ee5512008-07-10 02:12:20 +0000121
jieluo@google.com4de8f552014-07-18 00:47:59 +0000122 for (int i = 0; i < file_generators.size(); ++i) {
123 FileGenerator* file_generator = file_generators[i];
124
125 string package_dir = JavaPackageToDir(file_generator->java_package());
126
127 string java_filename = package_dir;
128 java_filename += file_generator->classname();
129 java_filename += ".java";
130 all_files.push_back(java_filename);
131
132 // Generate main java file.
Feng Xiaof157a562014-11-14 11:50:31 -0800133 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
jieluo@google.com4de8f552014-07-18 00:47:59 +0000134 context->Open(java_filename));
135 io::Printer printer(output.get(), '$');
136 file_generator->Generate(&printer);
137
138 // Generate sibling files.
139 file_generator->GenerateSiblings(package_dir, context, &all_files);
140 }
141
142 for (int i = 0; i < file_generators.size(); ++i) {
143 delete file_generators[i];
144 }
145 file_generators.clear();
temporal40ee5512008-07-10 02:12:20 +0000146
147 // Generate output list if requested.
148 if (!output_list_file.empty()) {
149 // Generate output list. This is just a simple text file placed in a
150 // deterministic location which lists the .java files being generated.
Feng Xiaof157a562014-11-14 11:50:31 -0800151 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output(
jieluo@google.com4de8f552014-07-18 00:47:59 +0000152 context->Open(output_list_file));
temporal40ee5512008-07-10 02:12:20 +0000153 io::Printer srclist_printer(srclist_raw_output.get(), '$');
154 for (int i = 0; i < all_files.size(); i++) {
155 srclist_printer.Print("$filename$\n", "filename", all_files[i]);
156 }
157 }
158
159 return true;
160}
161
162} // namespace java
163} // namespace compiler
164} // namespace protobuf
165} // namespace google