LCOV - code coverage report
Current view: top level - third_party/protobuf/src/google/protobuf/compiler/java - java_enum_lite.cc (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 0 63 0.0 %
Date: 2015-10-10 Functions: 0 4 0.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 <map>
      36             : #include <string>
      37             : 
      38             : #include <google/protobuf/compiler/java/java_context.h>
      39             : #include <google/protobuf/compiler/java/java_enum_lite.h>
      40             : #include <google/protobuf/compiler/java/java_doc_comment.h>
      41             : #include <google/protobuf/compiler/java/java_helpers.h>
      42             : #include <google/protobuf/compiler/java/java_name_resolver.h>
      43             : #include <google/protobuf/io/printer.h>
      44             : #include <google/protobuf/descriptor.pb.h>
      45             : #include <google/protobuf/stubs/strutil.h>
      46             : 
      47             : namespace google {
      48             : namespace protobuf {
      49             : namespace compiler {
      50             : namespace java {
      51             : 
      52             : namespace {
      53             : bool EnumHasCustomOptions(const EnumDescriptor* descriptor) {
      54             :   if (descriptor->options().unknown_fields().field_count() > 0) return true;
      55             :   for (int i = 0; i < descriptor->value_count(); ++i) {
      56             :     const EnumValueDescriptor* value = descriptor->value(i);
      57             :     if (value->options().unknown_fields().field_count() > 0) return true;
      58             :   }
      59             :   return false;
      60             : }
      61             : }  // namespace
      62             : 
      63           0 : EnumLiteGenerator::EnumLiteGenerator(const EnumDescriptor* descriptor,
      64             :                              bool immutable_api,
      65             :                              Context* context)
      66             :   : descriptor_(descriptor), immutable_api_(immutable_api),
      67           0 :     name_resolver_(context->GetNameResolver())  {
      68           0 :   for (int i = 0; i < descriptor_->value_count(); i++) {
      69           0 :     const EnumValueDescriptor* value = descriptor_->value(i);
      70             :     const EnumValueDescriptor* canonical_value =
      71           0 :       descriptor_->FindValueByNumber(value->number());
      72             : 
      73           0 :     if (value == canonical_value) {
      74           0 :       canonical_values_.push_back(value);
      75             :     } else {
      76             :       Alias alias;
      77           0 :       alias.value = value;
      78           0 :       alias.canonical_value = canonical_value;
      79           0 :       aliases_.push_back(alias);
      80             :     }
      81             :   }
      82           0 : }
      83             : 
      84           0 : EnumLiteGenerator::~EnumLiteGenerator() {}
      85             : 
      86           0 : void EnumLiteGenerator::Generate(io::Printer* printer) {
      87           0 :   WriteEnumDocComment(printer, descriptor_);
      88           0 :   if (HasDescriptorMethods(descriptor_)) {
      89             :     printer->Print(
      90             :       "public enum $classname$\n"
      91             :       "    implements com.google.protobuf.ProtocolMessageEnum {\n",
      92           0 :       "classname", descriptor_->name());
      93             :   } else {
      94             :     printer->Print(
      95             :       "public enum $classname$\n"
      96             :       "    implements com.google.protobuf.Internal.EnumLite {\n",
      97           0 :       "classname", descriptor_->name());
      98             :   }
      99           0 :   printer->Indent();
     100             : 
     101           0 :   for (int i = 0; i < canonical_values_.size(); i++) {
     102             :     map<string, string> vars;
     103           0 :     vars["name"] = canonical_values_[i]->name();
     104           0 :     vars["index"] = SimpleItoa(canonical_values_[i]->index());
     105           0 :     vars["number"] = SimpleItoa(canonical_values_[i]->number());
     106           0 :     WriteEnumValueDocComment(printer, canonical_values_[i]);
     107           0 :     if (canonical_values_[i]->options().deprecated()) {
     108           0 :       printer->Print("@java.lang.Deprecated\n");
     109             :     }
     110             :     printer->Print(vars,
     111           0 :       "$name$($index$, $number$),\n");
     112             :   }
     113             : 
     114           0 :   if (SupportUnknownEnumValue(descriptor_->file())) {
     115           0 :     printer->Print("UNRECOGNIZED(-1, -1),\n");
     116             :   }
     117             : 
     118             :   printer->Print(
     119             :     ";\n"
     120           0 :     "\n");
     121             : 
     122             :   // -----------------------------------------------------------------
     123             : 
     124           0 :   for (int i = 0; i < aliases_.size(); i++) {
     125             :     map<string, string> vars;
     126           0 :     vars["classname"] = descriptor_->name();
     127           0 :     vars["name"] = aliases_[i].value->name();
     128           0 :     vars["canonical_name"] = aliases_[i].canonical_value->name();
     129           0 :     WriteEnumValueDocComment(printer, aliases_[i].value);
     130             :     printer->Print(vars,
     131           0 :       "public static final $classname$ $name$ = $canonical_name$;\n");
     132             :   }
     133             : 
     134           0 :   for (int i = 0; i < descriptor_->value_count(); i++) {
     135             :     map<string, string> vars;
     136           0 :     vars["name"] = descriptor_->value(i)->name();
     137           0 :     vars["number"] = SimpleItoa(descriptor_->value(i)->number());
     138           0 :     WriteEnumValueDocComment(printer, descriptor_->value(i));
     139             :     printer->Print(vars,
     140           0 :       "public static final int $name$_VALUE = $number$;\n");
     141             :   }
     142           0 :   printer->Print("\n");
     143             : 
     144             :   // -----------------------------------------------------------------
     145             : 
     146             :   printer->Print(
     147             :     "\n"
     148           0 :     "public final int getNumber() {\n");
     149           0 :   if (SupportUnknownEnumValue(descriptor_->file())) {
     150             :     printer->Print(
     151             :       "  if (index == -1) {\n"
     152             :       "    throw new java.lang.IllegalArgumentException(\n"
     153             :       "        \"Can't get the number of an unknown enum value.\");\n"
     154           0 :       "  }\n");
     155             :   }
     156             :   printer->Print(
     157             :     "  return value;\n"
     158             :     "}\n"
     159             :     "\n"
     160             :     "public static $classname$ valueOf(int value) {\n"
     161             :     "  switch (value) {\n",
     162           0 :     "classname", descriptor_->name());
     163           0 :   printer->Indent();
     164           0 :   printer->Indent();
     165             : 
     166           0 :   for (int i = 0; i < canonical_values_.size(); i++) {
     167             :     printer->Print(
     168             :       "case $number$: return $name$;\n",
     169           0 :       "name", canonical_values_[i]->name(),
     170           0 :       "number", SimpleItoa(canonical_values_[i]->number()));
     171             :   }
     172             : 
     173           0 :   printer->Outdent();
     174           0 :   printer->Outdent();
     175             :   printer->Print(
     176             :     "    default: return null;\n"
     177             :     "  }\n"
     178             :     "}\n"
     179             :     "\n"
     180             :     "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
     181             :     "    internalGetValueMap() {\n"
     182             :     "  return internalValueMap;\n"
     183             :     "}\n"
     184             :     "private static final com.google.protobuf.Internal.EnumLiteMap<\n"
     185             :     "    $classname$> internalValueMap =\n"
     186             :     "      new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n"
     187             :     "        public $classname$ findValueByNumber(int number) {\n"
     188             :     "          return $classname$.valueOf(number);\n"
     189             :     "        }\n"
     190             :     "      };\n"
     191             :     "\n",
     192           0 :     "classname", descriptor_->name());
     193             : 
     194             :   printer->Print(
     195             :     "private final int value;\n\n"
     196             :     "private $classname$(int index, int value) {\n",
     197           0 :     "classname", descriptor_->name());
     198             :   printer->Print(
     199             :     "  this.value = value;\n"
     200           0 :     "}\n");
     201             : 
     202             :   printer->Print(
     203             :     "\n"
     204             :     "// @@protoc_insertion_point(enum_scope:$full_name$)\n",
     205           0 :     "full_name", descriptor_->full_name());
     206             : 
     207           0 :   printer->Outdent();
     208           0 :   printer->Print("}\n\n");
     209           0 : }
     210             : 
     211           0 : bool EnumLiteGenerator::CanUseEnumValues() {
     212           0 :   if (canonical_values_.size() != descriptor_->value_count()) {
     213             :     return false;
     214             :   }
     215           0 :   for (int i = 0; i < descriptor_->value_count(); i++) {
     216           0 :     if (descriptor_->value(i)->name() != canonical_values_[i]->name()) {
     217             :       return false;
     218             :     }
     219             :   }
     220             :   return true;
     221             : }
     222             : 
     223             : }  // namespace java
     224             : }  // namespace compiler
     225             : }  // namespace protobuf
     226             : }  // namespace google

Generated by: LCOV version 1.10