• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <google/protobuf/compiler/java/java_extension_lite.h>
32 
33 #include <google/protobuf/compiler/java/java_context.h>
34 #include <google/protobuf/compiler/java/java_doc_comment.h>
35 #include <google/protobuf/compiler/java/java_helpers.h>
36 #include <google/protobuf/compiler/java/java_name_resolver.h>
37 #include <google/protobuf/io/printer.h>
38 #include <google/protobuf/stubs/strutil.h>
39 
40 namespace google {
41 namespace protobuf {
42 namespace compiler {
43 namespace java {
44 
ImmutableExtensionLiteGenerator(const FieldDescriptor * descriptor,Context * context)45 ImmutableExtensionLiteGenerator::ImmutableExtensionLiteGenerator(
46     const FieldDescriptor* descriptor, Context* context)
47   : descriptor_(descriptor), context_(context),
48     name_resolver_(context->GetNameResolver()) {
49   if (descriptor_->extension_scope() != NULL) {
50     scope_ = name_resolver_->GetImmutableClassName(
51         descriptor_->extension_scope());
52   } else {
53     scope_ = name_resolver_->GetImmutableClassName(descriptor_->file());
54   }
55 }
56 
~ImmutableExtensionLiteGenerator()57 ImmutableExtensionLiteGenerator::~ImmutableExtensionLiteGenerator() {}
58 
Generate(io::Printer * printer)59 void ImmutableExtensionLiteGenerator::Generate(io::Printer* printer) {
60   map<string, string> vars;
61   const bool kUseImmutableNames = true;
62   InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_,
63                    &vars);
64   printer->Print(vars,
65       "public static final int $constant_name$ = $number$;\n");
66 
67   WriteFieldDocComment(printer, descriptor_);
68   if (descriptor_->is_repeated()) {
69     printer->Print(
70         vars,
71         "public static final\n"
72         "  com.google.protobuf.GeneratedMessageLite.GeneratedExtension<\n"
73         "    $containing_type$,\n"
74         "    $type$> $name$ = com.google.protobuf.GeneratedMessageLite\n"
75         "        .newRepeatedGeneratedExtension(\n"
76         "      $containing_type$.getDefaultInstance(),\n"
77         "      $prototype$,\n"
78         "      $enum_map$,\n"
79         "      $number$,\n"
80         "      com.google.protobuf.WireFormat.FieldType.$type_constant$,\n"
81         "      $packed$,\n"
82         "      $singular_type$.class);\n");
83   } else {
84     printer->Print(
85         vars,
86         "public static final\n"
87         "  com.google.protobuf.GeneratedMessageLite.GeneratedExtension<\n"
88         "    $containing_type$,\n"
89         "    $type$> $name$ = com.google.protobuf.GeneratedMessageLite\n"
90         "        .newSingularGeneratedExtension(\n"
91         "      $containing_type$.getDefaultInstance(),\n"
92         "      $default$,\n"
93         "      $prototype$,\n"
94         "      $enum_map$,\n"
95         "      $number$,\n"
96         "      com.google.protobuf.WireFormat.FieldType.$type_constant$,\n"
97         "      $singular_type$.class);\n");
98   }
99 }
100 
GenerateNonNestedInitializationCode(io::Printer * printer)101 int ImmutableExtensionLiteGenerator::GenerateNonNestedInitializationCode(
102     io::Printer* printer) {
103   return 0;
104 }
105 
GenerateRegistrationCode(io::Printer * printer)106 int ImmutableExtensionLiteGenerator::GenerateRegistrationCode(
107     io::Printer* printer) {
108   printer->Print(
109     "registry.add($scope$.$name$);\n",
110     "scope", scope_,
111     "name", UnderscoresToCamelCase(descriptor_));
112   return 7;
113 }
114 
115 }  // namespace java
116 }  // namespace compiler
117 }  // namespace protobuf
118 }  // namespace google
119