blob: 8719d00d1970a96163d137d30e79669365e85105 [file] [log] [blame]
Bo Yang5db21732015-05-21 14:28:59 -07001// 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
60namespace google {
61namespace protobuf {
62namespace compiler {
63namespace java {
64
65namespace {
66bool GenerateHasBits(const Descriptor* descriptor) {
67 return SupportFieldPresence(descriptor->file()) ||
68 HasRepeatedFields(descriptor);
69}
70
71string 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
79MessageBuilderLiteGenerator::MessageBuilderLiteGenerator(
80 const Descriptor* descriptor, Context* context)
81 : descriptor_(descriptor), context_(context),
82 name_resolver_(context->GetNameResolver()),
83 field_generators_(descriptor, context_) {
84 GOOGLE_CHECK_EQ(
85 FileOptions::LITE_RUNTIME, descriptor->file()->options().optimize_for());
86}
87
88MessageBuilderLiteGenerator::~MessageBuilderLiteGenerator() {}
89
90void MessageBuilderLiteGenerator::
91Generate(io::Printer* printer) {
92 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 descriptor_->extension_range_count() > 0 ? "Extendable" : "");
103 printer->Indent();
104
105 GenerateCommonBuilderMethods(printer);
106
107 // oneof
108 map<string, string> vars;
109 for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
110 vars["oneof_name"] = context_->GetOneofGeneratorInfo(
111 descriptor_->oneof_decl(i))->name;
112 vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo(
113 descriptor_->oneof_decl(i))->capitalized_name;
114 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 "\n");
129 }
130
131 if (GenerateHasBits(descriptor_)) {
132 // Integers for bit fields.
133 int totalBits = 0;
134 for (int i = 0; i < descriptor_->field_count(); i++) {
135 totalBits += field_generators_.get(descriptor_->field(i))
136 .GetNumBitsForBuilder();
137 }
138 int totalInts = (totalBits + 31) / 32;
139 for (int i = 0; i < totalInts; i++) {
140 printer->Print("private int $bit_field_name$;\n",
141 "bit_field_name", GetBitFieldName(i));
142 }
143 }
144
145 for (int i = 0; i < descriptor_->field_count(); i++) {
146 printer->Print("\n");
147 field_generators_.get(descriptor_->field(i))
148 .GenerateBuilderMembers(printer);
149 }
150
151 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 "\n");
163 }
164
165 printer->Print(
166 "\n"
167 "// @@protoc_insertion_point(builder_scope:$full_name$)\n",
168 "full_name", descriptor_->full_name());
169
170 printer->Outdent();
171 printer->Print("}\n");
172}
173
174// ===================================================================
175
176void MessageBuilderLiteGenerator::
177GenerateCommonBuilderMethods(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 "classname", name_resolver_->GetImmutableClassName(descriptor_));
185}
186
187// ===================================================================
188
189} // namespace java
190} // namespace compiler
191} // namespace protobuf
192} // namespace google