java_field.cc
Go to the documentation of this file.
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: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 
36 
37 #include <memory>
38 
56 
57 
58 
59 namespace google {
60 namespace protobuf {
61 namespace compiler {
62 namespace java {
63 
64 namespace {
65 
66 ImmutableFieldGenerator* MakeImmutableGenerator(const FieldDescriptor* field,
67  int messageBitIndex,
68  int builderBitIndex,
69  Context* context) {
70  if (field->is_repeated()) {
71  switch (GetJavaType(field)) {
72  case JAVATYPE_MESSAGE:
73  if (IsMapEntry(field->message_type())) {
74  return new ImmutableMapFieldGenerator(field, messageBitIndex,
75  builderBitIndex, context);
76  } else {
77  return new RepeatedImmutableMessageFieldGenerator(
78  field, messageBitIndex, builderBitIndex, context);
79  }
80  case JAVATYPE_ENUM:
81  return new RepeatedImmutableEnumFieldGenerator(
82  field, messageBitIndex, builderBitIndex, context);
83  case JAVATYPE_STRING:
84  return new RepeatedImmutableStringFieldGenerator(
85  field, messageBitIndex, builderBitIndex, context);
86  default:
87  return new RepeatedImmutablePrimitiveFieldGenerator(
88  field, messageBitIndex, builderBitIndex, context);
89  }
90  } else {
91  if (field->containing_oneof()) {
92  switch (GetJavaType(field)) {
93  case JAVATYPE_MESSAGE:
94  return new ImmutableMessageOneofFieldGenerator(
95  field, messageBitIndex, builderBitIndex, context);
96  case JAVATYPE_ENUM:
97  return new ImmutableEnumOneofFieldGenerator(field, messageBitIndex,
98  builderBitIndex, context);
99  case JAVATYPE_STRING:
100  return new ImmutableStringOneofFieldGenerator(
101  field, messageBitIndex, builderBitIndex, context);
102  default:
103  return new ImmutablePrimitiveOneofFieldGenerator(
104  field, messageBitIndex, builderBitIndex, context);
105  }
106  } else {
107  switch (GetJavaType(field)) {
108  case JAVATYPE_MESSAGE:
109  return new ImmutableMessageFieldGenerator(field, messageBitIndex,
110  builderBitIndex, context);
111  case JAVATYPE_ENUM:
112  return new ImmutableEnumFieldGenerator(field, messageBitIndex,
113  builderBitIndex, context);
114  case JAVATYPE_STRING:
115  return new ImmutableStringFieldGenerator(field, messageBitIndex,
116  builderBitIndex, context);
117  default:
118  return new ImmutablePrimitiveFieldGenerator(field, messageBitIndex,
119  builderBitIndex, context);
120  }
121  }
122  }
123 }
124 
125 ImmutableFieldLiteGenerator* MakeImmutableLiteGenerator(
126  const FieldDescriptor* field, int messageBitIndex, Context* context) {
127  if (field->is_repeated()) {
128  switch (GetJavaType(field)) {
129  case JAVATYPE_MESSAGE:
130  if (IsMapEntry(field->message_type())) {
131  return new ImmutableMapFieldLiteGenerator(field, messageBitIndex,
132  context);
133  } else {
134  return new RepeatedImmutableMessageFieldLiteGenerator(
135  field, messageBitIndex, context);
136  }
137  case JAVATYPE_ENUM:
138  return new RepeatedImmutableEnumFieldLiteGenerator(
139  field, messageBitIndex, context);
140  case JAVATYPE_STRING:
141  return new RepeatedImmutableStringFieldLiteGenerator(
142  field, messageBitIndex, context);
143  default:
144  return new RepeatedImmutablePrimitiveFieldLiteGenerator(
145  field, messageBitIndex, context);
146  }
147  } else {
148  if (field->containing_oneof()) {
149  switch (GetJavaType(field)) {
150  case JAVATYPE_MESSAGE:
151  return new ImmutableMessageOneofFieldLiteGenerator(
152  field, messageBitIndex, context);
153  case JAVATYPE_ENUM:
154  return new ImmutableEnumOneofFieldLiteGenerator(
155  field, messageBitIndex, context);
156  case JAVATYPE_STRING:
157  return new ImmutableStringOneofFieldLiteGenerator(
158  field, messageBitIndex, context);
159  default:
160  return new ImmutablePrimitiveOneofFieldLiteGenerator(
161  field, messageBitIndex, context);
162  }
163  } else {
164  switch (GetJavaType(field)) {
165  case JAVATYPE_MESSAGE:
166  return new ImmutableMessageFieldLiteGenerator(field, messageBitIndex,
167  context);
168  case JAVATYPE_ENUM:
169  return new ImmutableEnumFieldLiteGenerator(field, messageBitIndex,
170  context);
171  case JAVATYPE_STRING:
172  return new ImmutableStringFieldLiteGenerator(field, messageBitIndex,
173  context);
174  default:
175  return new ImmutablePrimitiveFieldLiteGenerator(
176  field, messageBitIndex, context);
177  }
178  }
179  }
180 }
181 
182 
183 static inline void ReportUnexpectedPackedFieldsCall(io::Printer* printer) {
184  // Reaching here indicates a bug. Cases are:
185  // - This FieldGenerator should support packing,
186  // but this method should be overridden.
187  // - This FieldGenerator doesn't support packing, and this method
188  // should never have been called.
189  GOOGLE_LOG(FATAL) << "GenerateParsingCodeFromPacked() "
190  << "called on field generator that does not support packing.";
191 }
192 
193 } // namespace
194 
196 
198  io::Printer* printer) const {
199  ReportUnexpectedPackedFieldsCall(printer);
200 }
201 
203 
204 // ===================================================================
205 
206 template <>
208  const Descriptor* descriptor, Context* context)
209  : descriptor_(descriptor), field_generators_(descriptor->field_count()) {
210  // Construct all the FieldGenerators and assign them bit indices for their
211  // bit fields.
212  int messageBitIndex = 0;
213  int builderBitIndex = 0;
214  for (int i = 0; i < descriptor->field_count(); i++) {
215  ImmutableFieldGenerator* generator = MakeImmutableGenerator(
216  descriptor->field(i), messageBitIndex, builderBitIndex, context);
217  field_generators_[i].reset(generator);
218  messageBitIndex += generator->GetNumBitsForMessage();
219  builderBitIndex += generator->GetNumBitsForBuilder();
220  }
221 }
222 
223 template <>
225 
226 template <>
228  const Descriptor* descriptor, Context* context)
229  : descriptor_(descriptor), field_generators_(descriptor->field_count()) {
230  // Construct all the FieldGenerators and assign them bit indices for their
231  // bit fields.
232  int messageBitIndex = 0;
233  for (int i = 0; i < descriptor->field_count(); i++) {
234  ImmutableFieldLiteGenerator* generator = MakeImmutableLiteGenerator(
235  descriptor->field(i), messageBitIndex, context);
236  field_generators_[i].reset(generator);
237  messageBitIndex += generator->GetNumBitsForMessage();
238  }
239 }
240 
241 template <>
243 
244 
246  const FieldGeneratorInfo* info,
247  std::map<std::string, std::string>* variables) {
248  (*variables)["field_name"] = descriptor->name();
249  (*variables)["name"] = info->name;
250  (*variables)["classname"] = descriptor->containing_type()->name();
251  (*variables)["capitalized_name"] = info->capitalized_name;
252  (*variables)["disambiguated_reason"] = info->disambiguated_reason;
253  (*variables)["constant_name"] = FieldConstantName(descriptor);
254  (*variables)["number"] = StrCat(descriptor->number());
255  // These variables are placeholders to pick out the beginning and ends of
256  // identifiers for annotations (when doing so with existing variables would
257  // be ambiguous or impossible). They should never be set to anything but the
258  // empty string.
259  (*variables)["{"] = "";
260  (*variables)["}"] = "";
261 }
262 
264  const OneofGeneratorInfo* info,
265  std::map<std::string, std::string>* variables) {
266  (*variables)["oneof_name"] = info->name;
267  (*variables)["oneof_capitalized_name"] = info->capitalized_name;
268  (*variables)["oneof_index"] =
269  StrCat(descriptor->containing_oneof()->index());
270  (*variables)["oneof_stored_type"] = GetOneofStoredType(descriptor);
271  (*variables)["set_oneof_case_message"] =
272  info->name + "Case_ = " + StrCat(descriptor->number());
273  (*variables)["clear_oneof_case_message"] = info->name + "Case_ = 0";
274  (*variables)["has_oneof_case_message"] =
275  info->name + "Case_ == " + StrCat(descriptor->number());
276 }
277 
278 void PrintExtraFieldInfo(const std::map<std::string, std::string>& variables,
279  io::Printer* printer) {
280  const std::map<std::string, std::string>::const_iterator it =
281  variables.find("disambiguated_reason");
282  if (it != variables.end() && !it->second.empty()) {
283  printer->Print(
284  variables,
285  "// An alternative name is used for field \"$field_name$\" because:\n"
286  "// $disambiguated_reason$\n");
287  }
288 }
289 
290 } // namespace java
291 } // namespace compiler
292 } // namespace protobuf
293 } // namespace google
google::protobuf::io::Printer::Print
void Print(const std::map< std::string, std::string > &variables, const char *text)
Definition: printer.cc:112
java_field.h
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
java_map_field.h
google::protobuf::compiler::java::IsMapEntry
bool IsMapEntry(const Descriptor *descriptor)
Definition: java_helpers.h:375
java_helpers.h
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
google::protobuf::compiler::java::FieldGeneratorMap
Definition: java_field.h:118
FATAL
const int FATAL
Definition: log_severity.h:60
google::protobuf::compiler::java::JAVATYPE_ENUM
@ JAVATYPE_ENUM
Definition: java_helpers.h:213
google::protobuf::compiler::java::FieldGeneratorMap::FieldGeneratorMap
FieldGeneratorMap(const Descriptor *descriptor, Context *context)
google::protobuf::compiler::cpp::FieldConstantName
std::string FieldConstantName(const FieldDescriptor *field)
Definition: cpp_helpers.cc:451
google::protobuf::compiler::java::Context
Definition: java_context.h:65
google::protobuf::compiler::java::ImmutableFieldGenerator::GetNumBitsForBuilder
virtual int GetNumBitsForBuilder() const =0
java_string_field.h
java_string_field_lite.h
google::protobuf::compiler::java::FieldGeneratorInfo::name
std::string name
Definition: java_field.h:158
google::protobuf::compiler::java::SetCommonOneofVariables
void SetCommonOneofVariables(const FieldDescriptor *descriptor, const OneofGeneratorInfo *info, std::map< std::string, std::string > *variables)
Definition: java_field.cc:263
google::protobuf::compiler::java::FieldGeneratorInfo
Definition: java_field.h:157
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::compiler::java::ImmutableFieldGenerator
Definition: java_field.h:65
java_map_field_lite.h
google::protobuf::compiler::java::ImmutableFieldLiteGenerator::~ImmutableFieldLiteGenerator
virtual ~ImmutableFieldLiteGenerator()
Definition: java_field.cc:202
FieldDescriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:129
google::protobuf::compiler::java::JAVATYPE_MESSAGE
@ JAVATYPE_MESSAGE
Definition: java_helpers.h:214
strutil.h
google::protobuf::compiler::java::OneofGeneratorInfo
Definition: java_field.h:164
google::protobuf::compiler::java::ImmutableFieldLiteGenerator::GetNumBitsForMessage
virtual int GetNumBitsForMessage() const =0
google::protobuf::compiler::java::GetJavaType
JavaType GetJavaType(const FieldDescriptor *field)
Definition: java_helpers.cc:321
printer.h
google::protobuf::compiler::java::PrintExtraFieldInfo
void PrintExtraFieldInfo(const std::map< std::string, std::string > &variables, io::Printer *printer)
Definition: java_field.cc:278
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: logging.h:146
java_enum_field.h
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
google::protobuf::compiler::java::OneofGeneratorInfo::name
std::string name
Definition: java_field.h:165
google::protobuf::io::Printer
Definition: printer.h:181
google::protobuf::compiler::java::FieldGeneratorInfo::capitalized_name
std::string capitalized_name
Definition: java_field.h:159
java_message_field.h
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::compiler::java::ImmutableFieldGenerator::~ImmutableFieldGenerator
virtual ~ImmutableFieldGenerator()
Definition: java_field.cc:195
java
java_enum_field_lite.h
java_primitive_field.h
common.h
google::protobuf::compiler::java::ImmutableFieldLiteGenerator
Definition: java_field.h:96
logging.h
google::protobuf::compiler::java::ImmutableFieldGenerator::GetNumBitsForMessage
virtual int GetNumBitsForMessage() const =0
google::protobuf::Descriptor
Definition: src/google/protobuf/descriptor.h:231
google::protobuf::compiler::java::GetOneofStoredType
std::string GetOneofStoredType(const FieldDescriptor *field)
Definition: java_helpers.cc:433
google::protobuf::compiler::java::ImmutableFieldGenerator::GenerateParsingCodeFromPacked
virtual void GenerateParsingCodeFromPacked(io::Printer *printer) const
Definition: java_field.cc:197
substitute.h
google::protobuf::compiler::java::SetCommonFieldVariables
void SetCommonFieldVariables(const FieldDescriptor *descriptor, const FieldGeneratorInfo *info, std::map< std::string, std::string > *variables)
Definition: java_field.cc:245
java_context.h
java_message_field_lite.h
google::protobuf::compiler::java::OneofGeneratorInfo::capitalized_name
std::string capitalized_name
Definition: java_field.h:166
descriptor_
const Descriptor * descriptor_
Definition: field_comparator_test.cc:56
java_primitive_field_lite.h
it
MapIter it
Definition: php/ext/google/protobuf/map.c:205
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::compiler::java::JAVATYPE_STRING
@ JAVATYPE_STRING
Definition: java_helpers.h:211
google::protobuf::compiler::java::FieldGeneratorInfo::disambiguated_reason
std::string disambiguated_reason
Definition: java_field.h:160


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:54