160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// Protocol Buffers - Google's data interchange format
260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// Copyright 2008 Google Inc.  All rights reserved.
360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// https://developers.google.com/protocol-buffers/
460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//
560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// Redistribution and use in source and binary forms, with or without
660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// modification, are permitted provided that the following conditions are
760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// met:
860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//
960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//     * Redistributions of source code must retain the above copyright
1060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// notice, this list of conditions and the following disclaimer.
1160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//     * Redistributions in binary form must reproduce the above
1260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// copyright notice, this list of conditions and the following disclaimer
1360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// in the documentation and/or other materials provided with the
1460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// distribution.
1560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//     * Neither the name of Google Inc. nor the names of its
1660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// contributors may be used to endorse or promote products derived from
1760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// this software without specific prior written permission.
1860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//
1960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
3160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair// Author: kenton@google.com (Kenton Varda)
3260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//  Based on original Protocol Buffers design by
3360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair//  Sanjay Ghemawat, Jeff Dean, and others.
3460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
3560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair#include <google/protobuf/compiler/java/java_generator.h>
3660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
3760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair#include <memory>
3860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair#ifndef _SHARED_PTR_H
3960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair#include <google/protobuf/stubs/shared_ptr.h>
4060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair#endif
4160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair#include <google/protobuf/compiler/java/java_file.h>
4360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair#include <google/protobuf/compiler/java/java_generator_factory.h>
4460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair#include <google/protobuf/compiler/java/java_helpers.h>
4560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair#include <google/protobuf/compiler/java/java_shared_code_generator.h>
4660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair#include <google/protobuf/io/printer.h>
4760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair#include <google/protobuf/io/zero_copy_stream.h>
4860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair#include <google/protobuf/descriptor.pb.h>
4960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair#include <google/protobuf/stubs/strutil.h>
5060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairnamespace google {
5260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairnamespace protobuf {
5360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairnamespace compiler {
5460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairnamespace java {
5560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairJavaGenerator::JavaGenerator() {}
5860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairJavaGenerator::~JavaGenerator() {}
5960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
6060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairbool JavaGenerator::Generate(const FileDescriptor* file,
6160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair                             const string& parameter,
6260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair                             GeneratorContext* context,
6360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair                             string* error) const {
6460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  // -----------------------------------------------------------------
6560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  // parse generator options
6660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
6760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  // Name a file where we will write a list of generated file names, one
6860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  // per line.
6960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  string output_list_file;
7060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
7160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
7260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  vector<pair<string, string> > options;
7360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  ParseGeneratorParameter(parameter, &options);
7460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
7560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  bool generate_immutable_code = false;
7660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  bool generate_mutable_code = false;
7760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  bool generate_shared_code = false;
7860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  bool enforce_lite = false;
7960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  for (int i = 0; i < options.size(); i++) {
8060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    if (options[i].first == "output_list_file") {
8160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair      output_list_file = options[i].second;
8260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    } else if (options[i].first == "immutable") {
8360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair      generate_immutable_code = true;
8460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    } else if (options[i].first == "mutable") {
8560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair      generate_mutable_code = true;
8660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    } else if (options[i].first == "shared") {
8760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair      generate_shared_code = true;
8860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    } else if (options[i].first == "lite") {
8960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair      // When set, the protoc will generate the current files and all the
9060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair      // transitive dependencies as lite runtime.
9160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair      enforce_lite = true;
9260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    } else {
9360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair      *error = "Unknown generator option: " + options[i].first;
9460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair      return false;
9560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
9660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  }
9760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
9860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  if (enforce_lite && generate_mutable_code) {
9960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    *error = "lite runtime generator option cannot be used with mutable API.";
10060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    return false;
10160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair  }
10260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
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