java_context.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 
32 
38 
40 
41 namespace google {
42 namespace protobuf {
43 namespace compiler {
44 namespace java {
45 
47  : name_resolver_(new ClassNameResolver), options_(options) {
49 }
50 
52 
54  return name_resolver_.get();
55 }
56 
57 namespace {
58 // Whether two fields have conflicting accessors (assuming name1 and name2
59 // are different). name1 and name2 are field1 and field2's camel-case name
60 // respectively.
61 bool IsConflicting(const FieldDescriptor* field1, const std::string& name1,
62  const FieldDescriptor* field2, const std::string& name2,
63  std::string* info) {
64  if (field1->is_repeated()) {
65  if (field2->is_repeated()) {
66  // Both fields are repeated.
67  return false;
68  } else {
69  // field1 is repeated, and field2 is not.
70  if (name1 + "Count" == name2) {
71  *info = "both repeated field \"" + field1->name() + "\" and singular " +
72  "field \"" + field2->name() + "\" generate the method \"" +
73  "get" + name1 + "Count()\"";
74  return true;
75  }
76  if (name1 + "List" == name2) {
77  *info = "both repeated field \"" + field1->name() + "\" and singular " +
78  "field \"" + field2->name() + "\" generate the method \"" +
79  "get" + name1 + "List()\"";
80  return true;
81  }
82  // Well, there are obviously many more conflicting cases, but it probably
83  // doesn't worth the effort to exhaust all of them because they rarely
84  // happen and as we are continuing adding new methods/changing existing
85  // methods the number of different conflicting cases will keep growing.
86  // We can just add more cases here when they are found in the real world.
87  return false;
88  }
89  } else {
90  if (field2->is_repeated()) {
91  return IsConflicting(field2, name2, field1, name1, info);
92  } else {
93  // None of the two fields are repeated.
94  return false;
95  }
96  }
97 }
98 } // namespace
99 
101  for (int i = 0; i < file->message_type_count(); ++i) {
103  }
104 }
105 
107  const Descriptor* message) {
108  for (int i = 0; i < message->nested_type_count(); ++i) {
110  }
111  std::vector<const FieldDescriptor*> fields;
112  for (int i = 0; i < message->field_count(); ++i) {
113  fields.push_back(message->field(i));
114  }
116 
117  for (int i = 0; i < message->oneof_decl_count(); ++i) {
118  const OneofDescriptor* oneof = message->oneof_decl(i);
119  OneofGeneratorInfo info;
120  info.name = UnderscoresToCamelCase(oneof->name(), false);
121  info.capitalized_name = UnderscoresToCamelCase(oneof->name(), true);
122  oneof_generator_info_map_[oneof] = info;
123  }
124 }
125 
127  const std::vector<const FieldDescriptor*>& fields) {
128  // Find out all fields that conflict with some other field in the same
129  // message.
130  std::vector<bool> is_conflict(fields.size());
131  std::vector<std::string> conflict_reason(fields.size());
132  for (int i = 0; i < fields.size(); ++i) {
133  const FieldDescriptor* field = fields[i];
135  for (int j = i + 1; j < fields.size(); ++j) {
136  const FieldDescriptor* other = fields[j];
137  const std::string& other_name = UnderscoresToCapitalizedCamelCase(other);
138  if (name == other_name) {
139  is_conflict[i] = is_conflict[j] = true;
140  conflict_reason[i] = conflict_reason[j] =
141  "capitalized name of field \"" + field->name() +
142  "\" conflicts with field \"" + other->name() + "\"";
143  } else if (IsConflicting(field, name, other, other_name,
144  &conflict_reason[j])) {
145  is_conflict[i] = is_conflict[j] = true;
146  conflict_reason[i] = conflict_reason[j];
147  }
148  }
149  if (is_conflict[i]) {
150  GOOGLE_LOG(WARNING) << "field \"" << field->full_name() << "\" is conflicting "
151  << "with another field: " << conflict_reason[i];
152  }
153  }
154  for (int i = 0; i < fields.size(); ++i) {
155  const FieldDescriptor* field = fields[i];
156  FieldGeneratorInfo info;
157  info.name = CamelCaseFieldName(field);
159  // For fields conflicting with some other fields, we append the field
160  // number to their field names in generated code to avoid conflicts.
161  if (is_conflict[i]) {
162  info.name += StrCat(field->number());
163  info.capitalized_name += StrCat(field->number());
164  info.disambiguated_reason = conflict_reason[i];
165  }
167  }
168 }
169 
171  const FieldDescriptor* field) const {
172  const FieldGeneratorInfo* result =
174  if (result == NULL) {
175  GOOGLE_LOG(FATAL) << "Can not find FieldGeneratorInfo for field: "
176  << field->full_name();
177  }
178  return result;
179 }
180 
182  const OneofDescriptor* oneof) const {
183  const OneofGeneratorInfo* result =
185  if (result == NULL) {
186  GOOGLE_LOG(FATAL) << "Can not find OneofGeneratorInfo for oneof: "
187  << oneof->name();
188  }
189  return result;
190 }
191 
192 // Does this message class have generated parsing, serialization, and other
193 // standard methods for which reflection-based fallback implementations exist?
195  return options_.enforce_lite ||
196  descriptor->file()->options().optimize_for() != FileOptions::CODE_SIZE;
197 }
198 
199 } // namespace java
200 } // namespace compiler
201 } // namespace protobuf
202 } // namespace google
google::protobuf::compiler::java::UnderscoresToCamelCase
std::string UnderscoresToCamelCase(const std::string &input, bool cap_next_letter)
Definition: java_helpers.cc:159
google::protobuf::compiler::java::Context::InitializeFieldGeneratorInfoForFields
void InitializeFieldGeneratorInfoForFields(const std::vector< const FieldDescriptor * > &fields)
Definition: java_context.cc:126
java_field.h
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
map_util.h
java_name_resolver.h
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
java_helpers.h
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
google::protobuf::compiler::java::Context::field_generator_info_map_
std::map< const FieldDescriptor *, FieldGeneratorInfo > field_generator_info_map_
Definition: java_context.h:101
FATAL
const int FATAL
Definition: log_severity.h:60
google::protobuf::compiler::java::Options
Definition: java_options.h:42
google::protobuf::compiler::java::Context::oneof_generator_info_map_
std::map< const OneofDescriptor *, OneofGeneratorInfo > oneof_generator_info_map_
Definition: java_context.h:103
google::protobuf::compiler::java::UnderscoresToCapitalizedCamelCase
std::string UnderscoresToCapitalizedCamelCase(const FieldDescriptor *field)
Definition: java_helpers.cc:200
google::protobuf::OneofDescriptor
Definition: src/google/protobuf/descriptor.h:843
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::compiler::java::FieldGeneratorInfo::name
std::string name
Definition: java_field.h:158
google::protobuf::compiler::java::FieldGeneratorInfo
Definition: java_field.h:157
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::OneofDescriptor::name
const std::string & name() const
google::protobuf::compiler::java::CamelCaseFieldName
std::string CamelCaseFieldName(const FieldDescriptor *field)
Definition: java_helpers.cc:224
google::protobuf::FindOrNull
const Collection::value_type::second_type * FindOrNull(const Collection &collection, const typename Collection::value_type::first_type &key)
Definition: map_util.h:137
strutil.h
google::protobuf::compiler::java::OneofGeneratorInfo
Definition: java_field.h:164
google::protobuf::compiler::java::Context::GetFieldGeneratorInfo
const FieldGeneratorInfo * GetFieldGeneratorInfo(const FieldDescriptor *field) const
Definition: java_context.cc:170
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: logging.h:146
google::protobuf::WARNING
static const LogLevel WARNING
Definition: protobuf/src/google/protobuf/testing/googletest.h:71
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
google::protobuf::compiler::java::Context::InitializeFieldGeneratorInfo
void InitializeFieldGeneratorInfo(const FileDescriptor *file)
Definition: java_context.cc:100
google::protobuf::compiler::java::OneofGeneratorInfo::name
std::string name
Definition: java_field.h:165
google::protobuf::compiler::java::Context::Context
Context(const FileDescriptor *file, const Options &options)
Definition: java_context.cc:46
google::protobuf::compiler::java::FieldGeneratorInfo::capitalized_name
std::string capitalized_name
Definition: java_field.h:159
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::compiler::java::Context::HasGeneratedMethods
bool HasGeneratedMethods(const Descriptor *descriptor) const
Definition: java_context.cc:194
java
fields
static const upb_fielddef fields[107]
Definition: ruby/ext/google/protobuf_c/upb.c:7671
google::protobuf::compiler::java::Context::~Context
~Context()
Definition: java_context.cc:51
google::protobuf::compiler::java::Context::InitializeFieldGeneratorInfoForMessage
void InitializeFieldGeneratorInfoForMessage(const Descriptor *message)
Definition: java_context.cc:106
google::protobuf::FieldDescriptor::name
const std::string & name() const
google::protobuf::FileDescriptor::message_type
const Descriptor * message_type(int index) const
google::protobuf::compiler::java::Context::options_
Options options_
Definition: java_context.h:104
google::protobuf::compiler::java::Context::name_resolver_
std::unique_ptr< ClassNameResolver > name_resolver_
Definition: java_context.h:99
google::protobuf::Descriptor
Definition: src/google/protobuf/descriptor.h:231
descriptor.h
google::protobuf::FileDescriptor
Definition: src/google/protobuf/descriptor.h:1320
google::protobuf::compiler::java::Options::enforce_lite
bool enforce_lite
Definition: java_options.h:56
java_context.h
google::protobuf::compiler::java::Context::GetNameResolver
ClassNameResolver * GetNameResolver() const
Definition: java_context.cc:53
google::protobuf::compiler::java::OneofGeneratorInfo::capitalized_name
std::string capitalized_name
Definition: java_field.h:166
google::protobuf::compiler::java::ClassNameResolver
Definition: java_name_resolver.h:56
FileOptions::CODE_SIZE
static constexpr OptimizeMode CODE_SIZE
Definition: descriptor.pb.h:3483
google::protobuf::compiler::java::Context::GetOneofGeneratorInfo
const OneofGeneratorInfo * GetOneofGeneratorInfo(const OneofDescriptor *oneof) const
Definition: java_context.cc:181
google::protobuf::FileDescriptor::message_type_count
int message_type_count() const
google::protobuf::FieldDescriptor::is_repeated
bool is_repeated() const
Definition: src/google/protobuf/descriptor.h:2067
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
options_
DebugStringOptions options_
Definition: src/google/protobuf/descriptor.cc:2410
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