• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/compiler/java/java_context.h>
43 #include <google/protobuf/compiler/java/java_doc_comment.h>
44 #include <google/protobuf/compiler/java/java_enum.h>
45 #include <google/protobuf/compiler/java/java_extension.h>
46 #include <google/protobuf/compiler/java/java_generator_factory.h>
47 #include <google/protobuf/compiler/java/java_helpers.h>
48 #include <google/protobuf/compiler/java/java_name_resolver.h>
49 #include <google/protobuf/descriptor.pb.h>
50 #include <google/protobuf/io/coded_stream.h>
51 #include <google/protobuf/io/printer.h>
52 #include <google/protobuf/wire_format.h>
53 #include <google/protobuf/stubs/strutil.h>
54 #include <google/protobuf/stubs/substitute.h>
55 
56 
57 namespace google {
58 namespace protobuf {
59 namespace compiler {
60 namespace java {
61 
MessageBuilderLiteGenerator(const Descriptor * descriptor,Context * context)62 MessageBuilderLiteGenerator::MessageBuilderLiteGenerator(
63     const Descriptor* descriptor, Context* context)
64     : descriptor_(descriptor),
65       context_(context),
66       name_resolver_(context->GetNameResolver()),
67       field_generators_(descriptor, context_) {
68   GOOGLE_CHECK(!HasDescriptorMethods(descriptor->file(), context->EnforceLite()))
69       << "Generator factory error: A lite message generator is used to "
70          "generate non-lite messages.";
71 }
72 
~MessageBuilderLiteGenerator()73 MessageBuilderLiteGenerator::~MessageBuilderLiteGenerator() {}
74 
Generate(io::Printer * printer)75 void MessageBuilderLiteGenerator::Generate(io::Printer* printer) {
76   WriteMessageDocComment(printer, descriptor_);
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",
83       "classname", name_resolver_->GetImmutableClassName(descriptor_),
84       "extra_interfaces", ExtraBuilderInterfaces(descriptor_), "extendible",
85       descriptor_->extension_range_count() > 0 ? "Extendable" : "");
86   printer->Indent();
87 
88   GenerateCommonBuilderMethods(printer);
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"] =
94         context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name;
95     vars["oneof_capitalized_name"] =
96         context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))
97             ->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");
118     field_generators_.get(descriptor_->field(i))
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 
GenerateCommonBuilderMethods(io::Printer * printer)133 void MessageBuilderLiteGenerator::GenerateCommonBuilderMethods(
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",
141       "classname", name_resolver_->GetImmutableClassName(descriptor_));
142 }
143 
144 // ===================================================================
145 
146 }  // namespace java
147 }  // namespace compiler
148 }  // namespace protobuf
149 }  // namespace google
150