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/message_builder_lite.h>
36
37 #include <algorithm>
38 #include <map>
39 #include <memory>
40 #include <vector>
41
42 #include <google/protobuf/io/coded_stream.h>
43 #include <google/protobuf/io/printer.h>
44 #include <google/protobuf/wire_format.h>
45 #include <google/protobuf/stubs/strutil.h>
46 #include <google/protobuf/stubs/substitute.h>
47 #include <google/protobuf/compiler/java/context.h>
48 #include <google/protobuf/compiler/java/doc_comment.h>
49 #include <google/protobuf/compiler/java/enum.h>
50 #include <google/protobuf/compiler/java/extension.h>
51 #include <google/protobuf/compiler/java/generator_factory.h>
52 #include <google/protobuf/compiler/java/helpers.h>
53 #include <google/protobuf/compiler/java/name_resolver.h>
54 #include <google/protobuf/descriptor.pb.h>
55
56 // Must be last.
57 #include <google/protobuf/port_def.inc>
58
59 namespace google {
60 namespace protobuf {
61 namespace compiler {
62 namespace java {
63
MessageBuilderLiteGenerator(const Descriptor * descriptor,Context * context)64 MessageBuilderLiteGenerator::MessageBuilderLiteGenerator(
65 const Descriptor* descriptor, Context* context)
66 : descriptor_(descriptor),
67 context_(context),
68 name_resolver_(context->GetNameResolver()),
69 field_generators_(descriptor, context_) {
70 GOOGLE_CHECK(!HasDescriptorMethods(descriptor->file(), context->EnforceLite()))
71 << "Generator factory error: A lite message generator is used to "
72 "generate non-lite messages.";
73 for (int i = 0; i < descriptor_->field_count(); i++) {
74 if (IsRealOneof(descriptor_->field(i))) {
75 oneofs_.insert(descriptor_->field(i)->containing_oneof());
76 }
77 }
78 }
79
~MessageBuilderLiteGenerator()80 MessageBuilderLiteGenerator::~MessageBuilderLiteGenerator() {}
81
Generate(io::Printer * printer)82 void MessageBuilderLiteGenerator::Generate(io::Printer* printer) {
83 WriteMessageDocComment(printer, descriptor_);
84 printer->Print(
85 "public static final class Builder extends\n"
86 " com.google.protobuf.GeneratedMessageLite.$extendible$Builder<\n"
87 " $classname$, Builder> implements\n"
88 " $extra_interfaces$\n"
89 " $classname$OrBuilder {\n",
90 "classname", name_resolver_->GetImmutableClassName(descriptor_),
91 "extra_interfaces", ExtraBuilderInterfaces(descriptor_), "extendible",
92 descriptor_->extension_range_count() > 0 ? "Extendable" : "");
93 printer->Indent();
94
95 GenerateCommonBuilderMethods(printer);
96
97 // oneof
98 std::map<std::string, std::string> vars;
99 for (auto oneof : oneofs_) {
100 vars["oneof_name"] = context_->GetOneofGeneratorInfo(oneof)->name;
101 vars["oneof_capitalized_name"] =
102 context_->GetOneofGeneratorInfo(oneof)->capitalized_name;
103 vars["oneof_index"] = StrCat(oneof->index());
104
105 // oneofCase() and clearOneof()
106 printer->Print(vars,
107 "@java.lang.Override\n"
108 "public $oneof_capitalized_name$Case\n"
109 " get$oneof_capitalized_name$Case() {\n"
110 " return instance.get$oneof_capitalized_name$Case();\n"
111 "}\n"
112 "\n"
113 "public Builder clear$oneof_capitalized_name$() {\n"
114 " copyOnWrite();\n"
115 " instance.clear$oneof_capitalized_name$();\n"
116 " return this;\n"
117 "}\n"
118 "\n");
119 }
120
121 for (int i = 0; i < descriptor_->field_count(); i++) {
122 printer->Print("\n");
123 field_generators_.get(descriptor_->field(i))
124 .GenerateBuilderMembers(printer);
125 }
126
127 printer->Print(
128 "\n"
129 "// @@protoc_insertion_point(builder_scope:$full_name$)\n",
130 "full_name", descriptor_->full_name());
131
132 printer->Outdent();
133 printer->Print("}\n");
134 }
135
136 // ===================================================================
137
GenerateCommonBuilderMethods(io::Printer * printer)138 void MessageBuilderLiteGenerator::GenerateCommonBuilderMethods(
139 io::Printer* printer) {
140 printer->Print(
141 "// Construct using $classname$.newBuilder()\n"
142 "private Builder() {\n"
143 " super(DEFAULT_INSTANCE);\n"
144 "}\n"
145 "\n",
146 "classname", name_resolver_->GetImmutableClassName(descriptor_));
147 }
148
149 // ===================================================================
150
151 } // namespace java
152 } // namespace compiler
153 } // namespace protobuf
154 } // namespace google
155
156 #include <google/protobuf/port_undef.inc>
157