LCOV - code coverage report
Current view: top level - third_party/protobuf/src/google/protobuf/compiler/java - java_generator.cc (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 2 42 4.8 %
Date: 2015-10-10 Functions: 2 4 50.0 %

          Line data    Source code
       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             : 
      57          34 : JavaGenerator::JavaGenerator() {}
      58          17 : JavaGenerator::~JavaGenerator() {}
      59             : 
      60           0 : 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           0 :   vector<pair<string, string> > options;
      73           0 :   ParseGeneratorParameter(parameter, &options);
      74             : 
      75             :   bool generate_immutable_code = false;
      76             :   bool generate_mutable_code = false;
      77             :   bool generate_shared_code = false;
      78           0 :   for (int i = 0; i < options.size(); i++) {
      79           0 :     if (options[i].first == "output_list_file") {
      80           0 :       output_list_file = options[i].second;
      81           0 :     } else if (options[i].first == "immutable") {
      82             :       generate_immutable_code = true;
      83           0 :     } else if (options[i].first == "mutable") {
      84             :       generate_mutable_code = true;
      85           0 :     } else if (options[i].first == "shared") {
      86             :       generate_shared_code = true;
      87             :     } else {
      88           0 :       *error = "Unknown generator option: " + options[i].first;
      89           0 :       return false;
      90             :     }
      91             :   }
      92             : 
      93             :   // By default we generate immutable code and shared code for immutable API.
      94           0 :   if (!generate_immutable_code && !generate_mutable_code &&
      95             :       !generate_shared_code) {
      96           0 :     generate_immutable_code = true;
      97           0 :     generate_shared_code = true;
      98             :   }
      99             : 
     100             :   // -----------------------------------------------------------------
     101             : 
     102             : 
     103           0 :   vector<string> all_files;
     104             : 
     105             : 
     106             :   vector<FileGenerator*> file_generators;
     107           0 :   if (generate_immutable_code) {
     108           0 :     file_generators.push_back(new FileGenerator(file, /* immutable = */ true));
     109             :   }
     110           0 :   if (generate_mutable_code) {
     111           0 :     file_generators.push_back(new FileGenerator(file, /* mutable = */ false));
     112             :   }
     113           0 :   for (int i = 0; i < file_generators.size(); ++i) {
     114           0 :     if (!file_generators[i]->Validate(error)) {
     115           0 :       for (int j = 0; j < file_generators.size(); ++j) {
     116           0 :         delete file_generators[j];
     117             :       }
     118             :       return false;
     119             :     }
     120             :   }
     121             : 
     122           0 :   for (int i = 0; i < file_generators.size(); ++i) {
     123           0 :     FileGenerator* file_generator = file_generators[i];
     124             : 
     125           0 :     string package_dir = JavaPackageToDir(file_generator->java_package());
     126             : 
     127           0 :     string java_filename = package_dir;
     128           0 :     java_filename += file_generator->classname();
     129             :     java_filename += ".java";
     130           0 :     all_files.push_back(java_filename);
     131             : 
     132             :     // Generate main java file.
     133             :     google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
     134           0 :         context->Open(java_filename));
     135           0 :     io::Printer printer(output.get(), '$');
     136           0 :     file_generator->Generate(&printer);
     137             : 
     138             :     // Generate sibling files.
     139           0 :     file_generator->GenerateSiblings(package_dir, context, &all_files);
     140             :   }
     141             : 
     142           0 :   for (int i = 0; i < file_generators.size(); ++i) {
     143           0 :     delete file_generators[i];
     144             :   }
     145             :   file_generators.clear();
     146             : 
     147             :   // Generate output list if requested.
     148           0 :   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.
     151             :     google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output(
     152           0 :         context->Open(output_list_file));
     153           0 :     io::Printer srclist_printer(srclist_raw_output.get(), '$');
     154           0 :     for (int i = 0; i < all_files.size(); i++) {
     155           0 :       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

Generated by: LCOV version 1.10