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: dweis@google.com (Daniel Weis)
32 : // Based on original Protocol Buffers design by
33 : // Sanjay Ghemawat, Jeff Dean, and others.
34 :
35 : #include <google/protobuf/compiler/java/java_message_builder_lite.h>
36 :
37 : #include <algorithm>
38 : #include <google/protobuf/stubs/hash.h>
39 : #include <map>
40 : #include <memory>
41 : #ifndef _SHARED_PTR_H
42 : #include <google/protobuf/stubs/shared_ptr.h>
43 : #endif
44 : #include <vector>
45 :
46 : #include <google/protobuf/compiler/java/java_context.h>
47 : #include <google/protobuf/compiler/java/java_doc_comment.h>
48 : #include <google/protobuf/compiler/java/java_enum.h>
49 : #include <google/protobuf/compiler/java/java_extension.h>
50 : #include <google/protobuf/compiler/java/java_generator_factory.h>
51 : #include <google/protobuf/compiler/java/java_helpers.h>
52 : #include <google/protobuf/compiler/java/java_name_resolver.h>
53 : #include <google/protobuf/io/coded_stream.h>
54 : #include <google/protobuf/io/printer.h>
55 : #include <google/protobuf/descriptor.pb.h>
56 : #include <google/protobuf/wire_format.h>
57 : #include <google/protobuf/stubs/strutil.h>
58 : #include <google/protobuf/stubs/substitute.h>
59 :
60 : namespace google {
61 : namespace protobuf {
62 : namespace compiler {
63 : namespace java {
64 :
65 : namespace {
66 0 : bool GenerateHasBits(const Descriptor* descriptor) {
67 0 : return SupportFieldPresence(descriptor->file()) ||
68 0 : HasRepeatedFields(descriptor);
69 : }
70 :
71 : string MapValueImmutableClassdName(const Descriptor* descriptor,
72 : ClassNameResolver* name_resolver) {
73 : const FieldDescriptor* value_field = descriptor->FindFieldByName("value");
74 : GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, value_field->type());
75 : return name_resolver->GetImmutableClassName(value_field->message_type());
76 : }
77 : } // namespace
78 :
79 0 : MessageBuilderLiteGenerator::MessageBuilderLiteGenerator(
80 0 : const Descriptor* descriptor, Context* context)
81 : : descriptor_(descriptor), context_(context),
82 0 : name_resolver_(context->GetNameResolver()),
83 0 : field_generators_(descriptor, context_) {
84 0 : GOOGLE_CHECK_EQ(
85 0 : FileOptions::LITE_RUNTIME, descriptor->file()->options().optimize_for());
86 0 : }
87 :
88 0 : MessageBuilderLiteGenerator::~MessageBuilderLiteGenerator() {}
89 :
90 0 : void MessageBuilderLiteGenerator::
91 : Generate(io::Printer* printer) {
92 0 : WriteMessageDocComment(printer, descriptor_);
93 : printer->Print(
94 : "public static final class Builder extends\n"
95 : " com.google.protobuf.GeneratedMessageLite.$extendible$Builder<\n"
96 : " $classname$, Builder> implements\n"
97 : " $extra_interfaces$\n"
98 : " $classname$OrBuilder {\n",
99 : "classname", name_resolver_->GetImmutableClassName(descriptor_),
100 : "extra_interfaces", ExtraBuilderInterfaces(descriptor_),
101 : "extendible",
102 0 : descriptor_->extension_range_count() > 0 ? "Extendable" : "");
103 0 : printer->Indent();
104 :
105 0 : GenerateCommonBuilderMethods(printer);
106 :
107 : // oneof
108 : map<string, string> vars;
109 0 : for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
110 0 : vars["oneof_name"] = context_->GetOneofGeneratorInfo(
111 0 : descriptor_->oneof_decl(i))->name;
112 0 : vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo(
113 0 : descriptor_->oneof_decl(i))->capitalized_name;
114 0 : vars["oneof_index"] = SimpleItoa(descriptor_->oneof_decl(i)->index());
115 :
116 : // oneofCase() and clearOneof()
117 : printer->Print(vars,
118 : "public $oneof_capitalized_name$Case\n"
119 : " get$oneof_capitalized_name$Case() {\n"
120 : " return instance.get$oneof_capitalized_name$Case();\n"
121 : "}\n"
122 : "\n"
123 : "public Builder clear$oneof_capitalized_name$() {\n"
124 : " copyOnWrite();\n"
125 : " instance.clear$oneof_capitalized_name$();\n"
126 : " return this;\n"
127 : "}\n"
128 0 : "\n");
129 : }
130 :
131 0 : if (GenerateHasBits(descriptor_)) {
132 : // Integers for bit fields.
133 : int totalBits = 0;
134 0 : for (int i = 0; i < descriptor_->field_count(); i++) {
135 0 : totalBits += field_generators_.get(descriptor_->field(i))
136 0 : .GetNumBitsForBuilder();
137 : }
138 0 : int totalInts = (totalBits + 31) / 32;
139 0 : for (int i = 0; i < totalInts; i++) {
140 : printer->Print("private int $bit_field_name$;\n",
141 0 : "bit_field_name", GetBitFieldName(i));
142 : }
143 : }
144 :
145 0 : for (int i = 0; i < descriptor_->field_count(); i++) {
146 0 : printer->Print("\n");
147 0 : field_generators_.get(descriptor_->field(i))
148 0 : .GenerateBuilderMembers(printer);
149 : }
150 :
151 0 : if (!PreserveUnknownFields(descriptor_)) {
152 : printer->Print(
153 : "public final Builder setUnknownFields(\n"
154 : " final com.google.protobuf.UnknownFieldSet unknownFields) {\n"
155 : " return this;\n"
156 : "}\n"
157 : "\n"
158 : "public final Builder mergeUnknownFields(\n"
159 : " final com.google.protobuf.UnknownFieldSet unknownFields) {\n"
160 : " return this;\n"
161 : "}\n"
162 0 : "\n");
163 : }
164 :
165 : printer->Print(
166 : "\n"
167 : "// @@protoc_insertion_point(builder_scope:$full_name$)\n",
168 0 : "full_name", descriptor_->full_name());
169 :
170 0 : printer->Outdent();
171 0 : printer->Print("}\n");
172 0 : }
173 :
174 : // ===================================================================
175 :
176 0 : void MessageBuilderLiteGenerator::
177 : GenerateCommonBuilderMethods(io::Printer* printer) {
178 : printer->Print(
179 : "// Construct using $classname$.newBuilder()\n"
180 : "private Builder() {\n"
181 : " super(DEFAULT_INSTANCE);\n"
182 : "}\n"
183 : "\n",
184 0 : "classname", name_resolver_->GetImmutableClassName(descriptor_));
185 0 : }
186 :
187 : // ===================================================================
188 :
189 : } // namespace java
190 : } // namespace compiler
191 : } // namespace protobuf
192 : } // namespace google
|