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