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