java_message_builder_lite.cc
Go to the documentation of this file.
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: dweis@google.com (Daniel Weis)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 
36 
37 #include <algorithm>
38 #include <map>
39 #include <memory>
40 #include <vector>
41 
55 
56 
57 namespace google {
58 namespace protobuf {
59 namespace compiler {
60 namespace java {
61 
63  const Descriptor* descriptor, Context* context)
65  context_(context),
66  name_resolver_(context->GetNameResolver()),
67  field_generators_(descriptor, context_) {
69  << "Generator factory error: A lite message generator is used to "
70  "generate non-lite messages.";
71 }
72 
74 
77  printer->Print(
78  "public static final class Builder extends\n"
79  " com.google.protobuf.GeneratedMessageLite.$extendible$Builder<\n"
80  " $classname$, Builder> implements\n"
81  " $extra_interfaces$\n"
82  " $classname$OrBuilder {\n",
84  "extra_interfaces", ExtraBuilderInterfaces(descriptor_), "extendible",
85  descriptor_->extension_range_count() > 0 ? "Extendable" : "");
86  printer->Indent();
87 
89 
90  // oneof
91  std::map<std::string, std::string> vars;
92  for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
93  vars["oneof_name"] =
95  vars["oneof_capitalized_name"] =
98  vars["oneof_index"] = StrCat(descriptor_->oneof_decl(i)->index());
99 
100  // oneofCase() and clearOneof()
101  printer->Print(vars,
102  "@java.lang.Override\n"
103  "public $oneof_capitalized_name$Case\n"
104  " get$oneof_capitalized_name$Case() {\n"
105  " return instance.get$oneof_capitalized_name$Case();\n"
106  "}\n"
107  "\n"
108  "public Builder clear$oneof_capitalized_name$() {\n"
109  " copyOnWrite();\n"
110  " instance.clear$oneof_capitalized_name$();\n"
111  " return this;\n"
112  "}\n"
113  "\n");
114  }
115 
116  for (int i = 0; i < descriptor_->field_count(); i++) {
117  printer->Print("\n");
119  .GenerateBuilderMembers(printer);
120  }
121 
122  printer->Print(
123  "\n"
124  "// @@protoc_insertion_point(builder_scope:$full_name$)\n",
125  "full_name", descriptor_->full_name());
126 
127  printer->Outdent();
128  printer->Print("}\n");
129 }
130 
131 // ===================================================================
132 
134  io::Printer* printer) {
135  printer->Print(
136  "// Construct using $classname$.newBuilder()\n"
137  "private Builder() {\n"
138  " super(DEFAULT_INSTANCE);\n"
139  "}\n"
140  "\n",
142 }
143 
144 // ===================================================================
145 
146 } // namespace java
147 } // namespace compiler
148 } // namespace protobuf
149 } // namespace google
google::protobuf::Descriptor::full_name
const std::string & full_name() const
google::protobuf::io::Printer::Print
void Print(const std::map< std::string, std::string > &variables, const char *text)
Definition: printer.cc:112
context_
MockGeneratorContext context_
Definition: csharp_bootstrap_unittest.cc:125
java_extension.h
java_name_resolver.h
java_doc_comment.h
google::protobuf::compiler::java::HasDescriptorMethods
bool HasDescriptorMethods(const Descriptor *descriptor, bool enforce_lite)
Definition: java_helpers.h:242
google::protobuf::compiler::java::Context::EnforceLite
bool EnforceLite() const
Definition: java_context.h:87
java_helpers.h
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
google::protobuf::compiler::java::Context
Definition: java_context.h:65
google::protobuf::compiler::java::ClassNameResolver::GetImmutableClassName
std::string GetImmutableClassName(const DescriptorType *descriptor)
Definition: java_name_resolver.h:88
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
java_message_builder_lite.h
google::protobuf::Descriptor::field
const FieldDescriptor * field(int index) const
google::protobuf::io::Printer::Indent
void Indent()
Definition: printer.cc:185
google::protobuf::compiler::java::WriteMessageDocComment
void WriteMessageDocComment(io::Printer *printer, const Descriptor *message)
Definition: java_doc_comment.cc:166
strutil.h
java_generator_factory.h
coded_stream.h
google::protobuf::Descriptor::oneof_decl
const OneofDescriptor * oneof_decl(int index) const
printer.h
google::protobuf::Descriptor::oneof_decl_count
int oneof_decl_count() const
google::protobuf::Descriptor::extension_range_count
int extension_range_count() const
google::protobuf::Descriptor::field_count
int field_count() const
google::protobuf::compiler::java::OneofGeneratorInfo::name
std::string name
Definition: java_field.h:165
GOOGLE_CHECK
#define GOOGLE_CHECK(EXPRESSION)
Definition: logging.h:153
google::protobuf::io::Printer
Definition: printer.h:181
google::protobuf::compiler::java::MessageBuilderLiteGenerator::field_generators_
FieldGeneratorMap< ImmutableFieldLiteGenerator > field_generators_
Definition: java_message_builder_lite.h:75
i
int i
Definition: gmock-matchers_test.cc:764
java
google::protobuf::compiler::java::MessageBuilderLiteGenerator::name_resolver_
ClassNameResolver * name_resolver_
Definition: java_message_builder_lite.h:74
google::protobuf::compiler::java::MessageBuilderLiteGenerator::~MessageBuilderLiteGenerator
virtual ~MessageBuilderLiteGenerator()
Definition: java_message_builder_lite.cc:73
google::protobuf::compiler::java::MessageBuilderLiteGenerator::MessageBuilderLiteGenerator
MessageBuilderLiteGenerator(const Descriptor *descriptor, Context *context)
Definition: java_message_builder_lite.cc:62
google::protobuf::OneofDescriptor::index
int index() const
Definition: src/google/protobuf/descriptor.h:2103
wire_format.h
google::protobuf::Descriptor
Definition: src/google/protobuf/descriptor.h:231
java_enum.h
google::protobuf::compiler::java::ExtraBuilderInterfaces
std::string ExtraBuilderInterfaces(const Descriptor *descriptor)
Definition: java_helpers.cc:299
substitute.h
java_context.h
google::protobuf::compiler::java::MessageBuilderLiteGenerator::Generate
virtual void Generate(io::Printer *printer)
Definition: java_message_builder_lite.cc:75
google::protobuf::compiler::java::OneofGeneratorInfo::capitalized_name
std::string capitalized_name
Definition: java_field.h:166
descriptor_
const Descriptor * descriptor_
Definition: field_comparator_test.cc:56
descriptor.pb.h
google::protobuf::compiler::java::MessageBuilderLiteGenerator::context_
Context * context_
Definition: java_message_builder_lite.h:73
google::protobuf::compiler::java::Context::GetOneofGeneratorInfo
const OneofGeneratorInfo * GetOneofGeneratorInfo(const OneofDescriptor *oneof) const
Definition: java_context.cc:181
google::protobuf::io::Printer::Outdent
void Outdent()
Definition: printer.cc:187
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::compiler::java::MessageBuilderLiteGenerator::GenerateCommonBuilderMethods
void GenerateCommonBuilderMethods(io::Printer *printer)
Definition: java_message_builder_lite.cc:133
google::protobuf::compiler::java::MessageBuilderLiteGenerator::descriptor_
const Descriptor * descriptor_
Definition: java_message_builder_lite.h:72


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:55