java_name_resolver.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 
33 #include <map>
34 #include <string>
35 
36 
39 
40 
41 namespace google {
42 namespace protobuf {
43 namespace compiler {
44 namespace java {
45 
46 namespace {
47 // A suffix that will be appended to the file's outer class name if the name
48 // conflicts with some other types defined in the file.
49 const char* kOuterClassNameSuffix = "OuterClass";
50 
51 // Strip package name from a descriptor's full name.
52 // For example:
53 // Full name : foo.Bar.Baz
54 // Package name: foo
55 // After strip : Bar.Baz
56 std::string StripPackageName(const std::string& full_name,
57  const FileDescriptor* file) {
58  if (file->package().empty()) {
59  return full_name;
60  } else {
61  // Strip package name
62  return full_name.substr(file->package().size() + 1);
63  }
64 }
65 
66 // Get the name of a message's Java class without package name prefix.
67 std::string ClassNameWithoutPackage(const Descriptor* descriptor,
68  bool immutable) {
69  return StripPackageName(descriptor->full_name(), descriptor->file());
70 }
71 
72 // Get the name of an enum's Java class without package name prefix.
73 std::string ClassNameWithoutPackage(const EnumDescriptor* descriptor,
74  bool immutable) {
75  // Doesn't append "Mutable" for enum type's name.
76  const Descriptor* message_descriptor = descriptor->containing_type();
77  if (message_descriptor == NULL) {
78  return descriptor->name();
79  } else {
80  return ClassNameWithoutPackage(message_descriptor, immutable) + "." +
81  descriptor->name();
82  }
83 }
84 
85 // Get the name of a service's Java class without package name prefix.
86 std::string ClassNameWithoutPackage(const ServiceDescriptor* descriptor,
87  bool immutable) {
88  std::string full_name =
89  StripPackageName(descriptor->full_name(), descriptor->file());
90  // We don't allow nested service definitions.
91  GOOGLE_CHECK(full_name.find('.') == std::string::npos);
92  return full_name;
93 }
94 
95 // Return true if a and b are equals (case insensitive).
96 NameEquality CheckNameEquality(const string& a, const string& b) {
97  if (ToUpper(a) == ToUpper(b)) {
98  if (a == b) {
100  }
102  }
103  return NameEquality::NO_MATCH;
104 }
105 
106 // Check whether a given message or its nested types has the given class name.
107 bool MessageHasConflictingClassName(const Descriptor* message,
108  const std::string& classname,
109  NameEquality equality_mode) {
110  if (CheckNameEquality(message->name(), classname) == equality_mode) {
111  return true;
112  }
113  for (int i = 0; i < message->nested_type_count(); ++i) {
114  if (MessageHasConflictingClassName(message->nested_type(i), classname,
115  equality_mode)) {
116  return true;
117  }
118  }
119  for (int i = 0; i < message->enum_type_count(); ++i) {
120  if (CheckNameEquality(message->enum_type(i)->name(), classname) ==
121  equality_mode) {
122  return true;
123  }
124  }
125  return false;
126 }
127 
128 } // namespace
129 
131 
133 
135  const FileDescriptor* file) {
136  std::string basename;
137  std::string::size_type last_slash = file->name().find_last_of('/');
138  if (last_slash == std::string::npos) {
139  basename = file->name();
140  } else {
141  basename = file->name().substr(last_slash + 1);
142  }
143  return UnderscoresToCamelCase(StripProto(basename), true);
144 }
145 
147  const FileDescriptor* file) {
149  if (class_name.empty()) {
150  if (file->options().has_java_outer_classname()) {
151  class_name = file->options().java_outer_classname();
152  } else {
153  class_name = GetFileDefaultImmutableClassName(file);
154  if (HasConflictingClassName(file, class_name,
156  class_name += kOuterClassNameSuffix;
157  }
158  }
159  }
160  return class_name;
161 }
162 
164  bool immutable) {
165  if (immutable) {
166  return GetFileImmutableClassName(file);
167  } else {
168  return "Mutable" + GetFileImmutableClassName(file);
169  }
170 }
171 
172 // Check whether there is any type defined in the proto file that has
173 // the given class name.
175  const std::string& classname,
176  NameEquality equality_mode) {
177  for (int i = 0; i < file->enum_type_count(); i++) {
178  if (CheckNameEquality(file->enum_type(i)->name(), classname) ==
179  equality_mode) {
180  return true;
181  }
182  }
183  for (int i = 0; i < file->service_count(); i++) {
184  if (CheckNameEquality(file->service(i)->name(), classname) ==
185  equality_mode) {
186  return true;
187  }
188  }
189  for (int i = 0; i < file->message_type_count(); i++) {
190  if (MessageHasConflictingClassName(file->message_type(i), classname,
191  equality_mode)) {
192  return true;
193  }
194  }
195  return false;
196 }
197 
199  const FileDescriptor* descriptor) {
201 }
202 
204  bool immutable) {
205  std::string result = FileJavaPackage(descriptor, immutable);
206  if (!result.empty()) result += '.';
207  result += GetFileClassName(descriptor, immutable);
208  return result;
209 }
210 
211 // Get the full name of a Java class by prepending the Java package name
212 // or outer class name.
214  const std::string& name_without_package, const FileDescriptor* file,
215  bool immutable, bool multiple_files) {
216  std::string result;
217  if (multiple_files) {
218  result = FileJavaPackage(file, immutable);
219  } else {
220  result = GetClassName(file, immutable);
221  }
222  if (!result.empty()) {
223  result += '.';
224  }
225  result += name_without_package;
226  return result;
227 }
228 
230  bool immutable) {
231  return GetClassFullName(ClassNameWithoutPackage(descriptor, immutable),
232  descriptor->file(), immutable,
233  MultipleJavaFiles(descriptor->file(), immutable));
234 }
235 
237  bool immutable) {
238  return GetClassFullName(ClassNameWithoutPackage(descriptor, immutable),
239  descriptor->file(), immutable,
240  MultipleJavaFiles(descriptor->file(), immutable));
241 }
242 
244  bool immutable) {
245  return GetClassFullName(ClassNameWithoutPackage(descriptor, immutable),
246  descriptor->file(), immutable,
247  MultipleJavaFiles(descriptor->file(), immutable));
248 }
249 
250 // Get the Java Class style full name of a message.
252  const std::string& name_without_package, const FileDescriptor* file,
253  bool immutable) {
254  std::string result;
255  if (MultipleJavaFiles(file, immutable)) {
256  result = FileJavaPackage(file, immutable);
257  if (!result.empty()) result += '.';
258  } else {
259  result = GetClassName(file, immutable);
260  if (!result.empty()) result += '$';
261  }
262  result += StringReplace(name_without_package, ".", "$", true);
263  return result;
264 }
265 
267  const FieldDescriptor* descriptor, bool immutable) {
268  return GetClassName(descriptor->containing_type(), immutable) + "." +
269  descriptor->name();
270 }
271 
273  const Descriptor* descriptor) {
274  return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, true),
275  descriptor->file(), true);
276 }
277 
279  const EnumDescriptor* descriptor) {
280  return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, true),
281  descriptor->file(), true);
282 }
283 
284 
285 } // namespace java
286 } // namespace compiler
287 } // namespace protobuf
288 } // 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::EXACT_EQUAL
@ EXACT_EQUAL
Definition: java_name_resolver.h:51
java_name_resolver.h
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
google::protobuf::FileDescriptor::enum_type
const EnumDescriptor * enum_type(int index) const
java_helpers.h
NULL
NULL
Definition: test_security_zap.cpp:405
FileDescriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:125
google::protobuf::compiler::java::ClassNameResolver::file_immutable_outer_class_names_
std::map< const FileDescriptor *, std::string > file_immutable_outer_class_names_
Definition: java_name_resolver.h:117
google::protobuf::compiler::java::MultipleJavaFiles
bool MultipleJavaFiles(const FileDescriptor *descriptor, bool immutable)
Definition: java_helpers.h:158
google::protobuf::compiler::java::ClassNameResolver::GetExtensionIdentifierName
std::string GetExtensionIdentifierName(const FieldDescriptor *descriptor, bool immutable)
Definition: java_name_resolver.cc:266
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::ServiceDescriptor::name
const std::string & name() const
google::protobuf::compiler::java::ClassNameResolver::GetClassFullName
std::string GetClassFullName(const std::string &name_without_package, const FileDescriptor *file, bool immutable, bool multiple_files)
Definition: java_name_resolver.cc:213
Descriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:113
google::protobuf::FileDescriptor::enum_type_count
int enum_type_count() const
b
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:3228
google::protobuf::compiler::java::NameEquality
NameEquality
Definition: java_name_resolver.h:51
google::protobuf::ServiceDescriptor
Definition: src/google/protobuf/descriptor.h:1152
google::protobuf::compiler::java::ClassNameResolver::ClassNameResolver
ClassNameResolver()
Definition: java_name_resolver.cc:130
google::protobuf::EnumDescriptor::name
const std::string & name() const
google::protobuf::compiler::java::ClassNameResolver::GetFileImmutableClassName
std::string GetFileImmutableClassName(const FileDescriptor *file)
Definition: java_name_resolver.cc:146
google::protobuf::FileDescriptor::service
const ServiceDescriptor * service(int index) const
google::protobuf::compiler::java::ClassNameResolver::~ClassNameResolver
~ClassNameResolver()
Definition: java_name_resolver.cc:132
google::protobuf::compiler::java::ClassNameResolver::GetJavaClassFullName
std::string GetJavaClassFullName(const std::string &name_without_package, const FileDescriptor *file, bool immutable)
Definition: java_name_resolver.cc:251
google::protobuf::compiler::java::ClassNameResolver::GetFileClassName
std::string GetFileClassName(const FileDescriptor *file, bool immutable)
Definition: java_name_resolver.cc:163
google::protobuf::FileDescriptor::options
const FileOptions & options() const
EnumDescriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:137
google::protobuf::compiler::java::FileJavaPackage
std::string FileJavaPackage(const FileDescriptor *file, bool immutable)
Definition: java_helpers.cc:245
GOOGLE_CHECK
#define GOOGLE_CHECK(EXPRESSION)
Definition: logging.h:153
google::protobuf::compiler::java::ClassNameResolver::GetJavaImmutableClassName
std::string GetJavaImmutableClassName(const Descriptor *descriptor)
Definition: java_name_resolver.cc:272
i
int i
Definition: gmock-matchers_test.cc:764
FileOptions::java_outer_classname
const std::string & java_outer_classname() const
Definition: descriptor.pb.h:9525
java
google::protobuf::FileDescriptor::name
const std::string & name() const
google::protobuf::StringReplace
void StringReplace(const string &s, const string &oldsub, const string &newsub, bool replace_all, string *res)
Definition: strutil.cc:148
google::protobuf::FileDescriptor::message_type
const Descriptor * message_type(int index) const
google::protobuf::compiler::java::NO_MATCH
@ NO_MATCH
Definition: java_name_resolver.h:51
google::protobuf::FileDescriptor::service_count
int service_count() const
google::protobuf::Descriptor
Definition: src/google/protobuf/descriptor.h:231
google::protobuf::ToUpper
string ToUpper(const string &s)
Definition: strutil.h:193
substitute.h
google::protobuf::FileDescriptor
Definition: src/google/protobuf/descriptor.h:1320
google::protobuf::compiler::java::EQUAL_IGNORE_CASE
@ EQUAL_IGNORE_CASE
Definition: java_name_resolver.h:51
google::protobuf::compiler::java::StripProto
std::string StripProto(const std::string &filename)
Definition: java_helpers.cc:232
google::protobuf::compiler::java::ClassNameResolver::GetClassName
std::string GetClassName(const Descriptor *descriptor, bool immutable)
Definition: java_name_resolver.cc:229
google::protobuf::compiler::java::ClassNameResolver::GetDescriptorClassName
std::string GetDescriptorClassName(const FileDescriptor *file)
Definition: java_name_resolver.cc:198
google::protobuf::compiler::java::ClassNameResolver::HasConflictingClassName
bool HasConflictingClassName(const FileDescriptor *file, const std::string &classname, NameEquality equality_mode)
Definition: java_name_resolver.cc:174
google::protobuf::compiler::java::ClassNameResolver::GetFileDefaultImmutableClassName
std::string GetFileDefaultImmutableClassName(const FileDescriptor *file)
Definition: java_name_resolver.cc:134
google::protobuf::EnumDescriptor
Definition: src/google/protobuf/descriptor.h:918
a
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:3228
google::protobuf::FileDescriptor::message_type_count
int message_type_count() const
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


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