• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include "google/protobuf/compiler/java/name_resolver.h"
9 
10 #include <string>
11 
12 #include "absl/log/absl_check.h"
13 #include "absl/strings/ascii.h"
14 #include "absl/strings/str_replace.h"
15 #include "absl/strings/substitute.h"
16 #include "google/protobuf/compiler/code_generator.h"
17 #include "google/protobuf/compiler/java/helpers.h"
18 #include "google/protobuf/compiler/java/names.h"
19 #include "google/protobuf/descriptor.h"
20 
21 
22 // Must be last.
23 #include "google/protobuf/port_def.inc"
24 
25 namespace google {
26 namespace protobuf {
27 namespace compiler {
28 namespace java {
29 
30 namespace {
31 // A suffix that will be appended to the file's outer class name if the name
32 // conflicts with some other types defined in the file.
33 const char* kOuterClassNameSuffix = "OuterClass";
34 
35 // Strip package name from a descriptor's full name.
36 // For example:
37 //   Full name   : foo.Bar.Baz
38 //   Package name: foo
39 //   After strip : Bar.Baz
StripPackageName(absl::string_view full_name,const FileDescriptor * file)40 absl::string_view StripPackageName(absl::string_view full_name,
41                                    const FileDescriptor* file) {
42   if (file->package().empty()) {
43     return full_name;
44   } else {
45     // Strip package name
46     return full_name.substr(file->package().size() + 1);
47   }
48 }
49 
50 // Get the name of a message's Java class without package name prefix.
ClassNameWithoutPackage(const Descriptor * descriptor,bool immutable)51 std::string ClassNameWithoutPackage(const Descriptor* descriptor,
52                                     bool immutable) {
53   return std::string(
54       StripPackageName(descriptor->full_name(), descriptor->file()));
55 }
56 
ClassNameWithoutPackageKotlin(const Descriptor * descriptor)57 std::string ClassNameWithoutPackageKotlin(const Descriptor* descriptor) {
58   std::string result = std::string(descriptor->name());
59   const Descriptor* temp = descriptor->containing_type();
60 
61   while (temp) {
62     result = absl::StrCat(temp->name(), "Kt.", result);
63     temp = temp->containing_type();
64   }
65   return result;
66 }
67 
68 // Get the name of an enum's Java class without package name prefix.
ClassNameWithoutPackage(const EnumDescriptor * descriptor,bool immutable)69 std::string ClassNameWithoutPackage(const EnumDescriptor* descriptor,
70                                     bool immutable) {
71   // Doesn't append "Mutable" for enum type's name.
72   const Descriptor* message_descriptor = descriptor->containing_type();
73   if (message_descriptor == nullptr) {
74     return std::string(descriptor->name());
75   } else {
76     return absl::StrCat(ClassNameWithoutPackage(message_descriptor, immutable),
77                         ".", descriptor->name());
78   }
79 }
80 
81 // Get the name of a service's Java class without package name prefix.
ClassNameWithoutPackage(const ServiceDescriptor * descriptor,bool immutable)82 std::string ClassNameWithoutPackage(const ServiceDescriptor* descriptor,
83                                     bool immutable) {
84   absl::string_view full_name =
85       StripPackageName(descriptor->full_name(), descriptor->file());
86   // We don't allow nested service definitions.
87   ABSL_CHECK(!absl::StrContains(full_name, '.'));
88   return std::string(full_name);
89 }
90 
91 // Return true if a and b are equals (case insensitive).
CheckNameEquality(absl::string_view a,absl::string_view b)92 NameEquality CheckNameEquality(absl::string_view a, absl::string_view b) {
93   if (absl::AsciiStrToUpper(a) == absl::AsciiStrToUpper(b)) {
94     if (a == b) {
95       return NameEquality::EXACT_EQUAL;
96     }
97     return NameEquality::EQUAL_IGNORE_CASE;
98   }
99   return NameEquality::NO_MATCH;
100 }
101 
102 // Check whether a given message or its nested types has the given class name.
MessageHasConflictingClassName(const Descriptor * message,absl::string_view classname,NameEquality equality_mode)103 bool MessageHasConflictingClassName(const Descriptor* message,
104                                     absl::string_view classname,
105                                     NameEquality equality_mode) {
106   if (CheckNameEquality(message->name(), classname) == equality_mode) {
107     return true;
108   }
109   for (int i = 0; i < message->nested_type_count(); ++i) {
110     if (MessageHasConflictingClassName(message->nested_type(i), classname,
111                                        equality_mode)) {
112       return true;
113     }
114   }
115   for (int i = 0; i < message->enum_type_count(); ++i) {
116     if (CheckNameEquality(message->enum_type(i)->name(), classname) ==
117         equality_mode) {
118       return true;
119     }
120   }
121   return false;
122 }
123 
124 }  // namespace
125 
GetFileDefaultImmutableClassName(const FileDescriptor * file)126 std::string ClassNameResolver::GetFileDefaultImmutableClassName(
127     const FileDescriptor* file) {
128   std::string basename;
129   std::string::size_type last_slash = file->name().find_last_of('/');
130   if (last_slash == std::string::npos) {
131     basename = file->name();
132   } else {
133     basename = file->name().substr(last_slash + 1);
134   }
135   return UnderscoresToCamelCase(StripProto(basename), true);
136 }
137 
GetFileImmutableClassName(const FileDescriptor * file)138 std::string ClassNameResolver::GetFileImmutableClassName(
139     const FileDescriptor* file) {
140   std::string& class_name = file_immutable_outer_class_names_[file];
141   if (class_name.empty()) {
142     if (file->options().has_java_outer_classname()) {
143       class_name = file->options().java_outer_classname();
144     } else {
145       class_name = GetFileDefaultImmutableClassName(file);
146       if (HasConflictingClassName(file, class_name,
147                                   NameEquality::EXACT_EQUAL)) {
148         class_name += kOuterClassNameSuffix;
149       }
150     }
151   }
152   return class_name;
153 }
154 
GetFileClassName(const FileDescriptor * file,bool immutable)155 std::string ClassNameResolver::GetFileClassName(const FileDescriptor* file,
156                                                 bool immutable) {
157   return GetFileClassName(file, immutable, false);
158 }
159 
GetFileClassName(const FileDescriptor * file,bool immutable,bool kotlin)160 std::string ClassNameResolver::GetFileClassName(const FileDescriptor* file,
161                                                 bool immutable, bool kotlin) {
162   if (kotlin) {
163     return absl::StrCat(GetFileImmutableClassName(file), "Kt");
164   } else if (immutable) {
165     return GetFileImmutableClassName(file);
166   } else {
167     return absl::StrCat("Mutable", GetFileImmutableClassName(file));
168   }
169 }
170 
171 // Check whether there is any type defined in the proto file that has
172 // the given class name.
HasConflictingClassName(const FileDescriptor * file,absl::string_view classname,NameEquality equality_mode)173 bool ClassNameResolver::HasConflictingClassName(const FileDescriptor* file,
174                                                 absl::string_view classname,
175                                                 NameEquality equality_mode) {
176   for (int i = 0; i < file->enum_type_count(); i++) {
177     if (CheckNameEquality(file->enum_type(i)->name(), classname) ==
178         equality_mode) {
179       return true;
180     }
181   }
182   for (int i = 0; i < file->service_count(); i++) {
183     if (CheckNameEquality(file->service(i)->name(), classname) ==
184         equality_mode) {
185       return true;
186     }
187   }
188   for (int i = 0; i < file->message_type_count(); i++) {
189     if (MessageHasConflictingClassName(file->message_type(i), classname,
190                                        equality_mode)) {
191       return true;
192     }
193   }
194   return false;
195 }
196 
GetDescriptorClassName(const FileDescriptor * file)197 std::string ClassNameResolver::GetDescriptorClassName(
198     const FileDescriptor* file) {
199   if (options_.opensource_runtime) {
200     return GetFileImmutableClassName(file);
201   } else {
202     return absl::StrCat(GetFileImmutableClassName(file), "InternalDescriptors");
203   }
204 }
205 
GetClassName(const FileDescriptor * descriptor,bool immutable)206 std::string ClassNameResolver::GetClassName(const FileDescriptor* descriptor,
207                                             bool immutable) {
208   return GetClassName(descriptor, immutable, false);
209 }
210 
GetClassName(const FileDescriptor * descriptor,bool immutable,bool kotlin)211 std::string ClassNameResolver::GetClassName(const FileDescriptor* descriptor,
212                                             bool immutable, bool kotlin) {
213   std::string result = FileJavaPackage(descriptor, immutable, options_);
214   if (!result.empty()) result += '.';
215   result += GetFileClassName(descriptor, immutable, kotlin);
216   return result;
217 }
218 
219 // Get the full name of a Java class by prepending the Java package name
220 // or outer class name.
GetClassFullName(absl::string_view name_without_package,const FileDescriptor * file,bool immutable,bool is_own_file)221 std::string ClassNameResolver::GetClassFullName(
222     absl::string_view name_without_package, const FileDescriptor* file,
223     bool immutable, bool is_own_file) {
224   return GetClassFullName(name_without_package, file, immutable, is_own_file,
225                           false);
226 }
227 
GetClassFullName(absl::string_view name_without_package,const FileDescriptor * file,bool immutable,bool is_own_file,bool kotlin)228 std::string ClassNameResolver::GetClassFullName(
229     absl::string_view name_without_package, const FileDescriptor* file,
230     bool immutable, bool is_own_file, bool kotlin) {
231   std::string result;
232   if (is_own_file) {
233     result = FileJavaPackage(file, immutable, options_);
234   } else {
235     result = GetClassName(file, immutable, kotlin);
236   }
237   if (!result.empty()) {
238     absl::StrAppend(&result, ".");
239   }
240   absl::StrAppend(&result, name_without_package);
241   if (kotlin) absl::StrAppend(&result, "Kt");
242   return result;
243 }
244 
GetClassName(const Descriptor * descriptor,bool immutable)245 std::string ClassNameResolver::GetClassName(const Descriptor* descriptor,
246                                             bool immutable) {
247   return GetClassName(descriptor, immutable, false);
248 }
249 
GetClassName(const Descriptor * descriptor,bool immutable,bool kotlin)250 std::string ClassNameResolver::GetClassName(const Descriptor* descriptor,
251                                             bool immutable, bool kotlin) {
252   return GetClassFullName(
253       ClassNameWithoutPackage(descriptor, immutable), descriptor->file(),
254       immutable, MultipleJavaFiles(descriptor->file(), immutable), kotlin);
255 }
256 
GetClassName(const EnumDescriptor * descriptor,bool immutable)257 std::string ClassNameResolver::GetClassName(const EnumDescriptor* descriptor,
258                                             bool immutable) {
259   return GetClassName(descriptor, immutable, false);
260 }
261 
GetClassName(const EnumDescriptor * descriptor,bool immutable,bool kotlin)262 std::string ClassNameResolver::GetClassName(const EnumDescriptor* descriptor,
263                                             bool immutable, bool kotlin) {
264   return GetClassFullName(
265       ClassNameWithoutPackage(descriptor, immutable), descriptor->file(),
266       immutable, MultipleJavaFiles(descriptor->file(), immutable), kotlin);
267 }
268 
GetClassName(const ServiceDescriptor * descriptor,bool immutable)269 std::string ClassNameResolver::GetClassName(const ServiceDescriptor* descriptor,
270                                             bool immutable) {
271   return GetClassName(descriptor, immutable, false);
272 }
273 
GetClassName(const ServiceDescriptor * descriptor,bool immutable,bool kotlin)274 std::string ClassNameResolver::GetClassName(const ServiceDescriptor* descriptor,
275                                             bool immutable, bool kotlin) {
276   return GetClassFullName(ClassNameWithoutPackage(descriptor, immutable),
277                           descriptor->file(), immutable,
278                           IsOwnFile(descriptor, immutable), kotlin);
279 }
280 
281 // Get the Java Class style full name of a message.
GetJavaClassFullName(absl::string_view name_without_package,const FileDescriptor * file,bool immutable)282 std::string ClassNameResolver::GetJavaClassFullName(
283     absl::string_view name_without_package, const FileDescriptor* file,
284     bool immutable) {
285   return GetJavaClassFullName(name_without_package, file, immutable, false);
286 }
287 
GetJavaClassFullName(absl::string_view name_without_package,const FileDescriptor * file,bool immutable,bool kotlin)288 std::string ClassNameResolver::GetJavaClassFullName(
289     absl::string_view name_without_package, const FileDescriptor* file,
290     bool immutable, bool kotlin) {
291   std::string result;
292   if (MultipleJavaFiles(file, immutable)) {
293     result = FileJavaPackage(file, immutable, options_);
294     if (!result.empty()) result += '.';
295   } else {
296     result = GetClassName(file, immutable, kotlin);
297     if (!result.empty()) result += '$';
298   }
299   result += absl::StrReplaceAll(name_without_package, {{".", "$"}});
300   return result;
301 }
302 
GetExtensionIdentifierName(const FieldDescriptor * descriptor,bool immutable)303 std::string ClassNameResolver::GetExtensionIdentifierName(
304     const FieldDescriptor* descriptor, bool immutable) {
305   return GetExtensionIdentifierName(descriptor, immutable, false);
306 }
307 
GetExtensionIdentifierName(const FieldDescriptor * descriptor,bool immutable,bool kotlin)308 std::string ClassNameResolver::GetExtensionIdentifierName(
309     const FieldDescriptor* descriptor, bool immutable, bool kotlin) {
310   return absl::StrCat(
311       GetClassName(descriptor->containing_type(), immutable, kotlin), ".",
312       descriptor->name());
313 }
314 
GetKotlinFactoryName(const Descriptor * descriptor)315 std::string ClassNameResolver::GetKotlinFactoryName(
316     const Descriptor* descriptor) {
317   std::string name = ToCamelCase(descriptor->name(), /* lower_first = */ true);
318   return IsForbiddenKotlin(name) ? absl::StrCat(name, "_") : name;
319 }
320 
GetJavaImmutableClassName(const Descriptor * descriptor)321 std::string ClassNameResolver::GetJavaImmutableClassName(
322     const Descriptor* descriptor) {
323   return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, true),
324                               descriptor->file(), true);
325 }
326 
GetJavaImmutableClassName(const EnumDescriptor * descriptor)327 std::string ClassNameResolver::GetJavaImmutableClassName(
328     const EnumDescriptor* descriptor) {
329   return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, true),
330                               descriptor->file(), true);
331 }
332 
GetJavaImmutableClassName(const ServiceDescriptor * descriptor)333 std::string ClassNameResolver::GetJavaImmutableClassName(
334     const ServiceDescriptor* descriptor) {
335   return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, true),
336                               descriptor->file(), true);
337 }
338 
GetKotlinExtensionsClassName(const Descriptor * descriptor)339 std::string ClassNameResolver::GetKotlinExtensionsClassName(
340     const Descriptor* descriptor) {
341   return GetClassFullName(ClassNameWithoutPackageKotlin(descriptor),
342                           descriptor->file(), true, true, true);
343 }
344 
GetKotlinExtensionsClassNameEscaped(const Descriptor * descriptor)345 std::string ClassNameResolver::GetKotlinExtensionsClassNameEscaped(
346     const Descriptor* descriptor) {
347   std::string name_without_package = ClassNameWithoutPackageKotlin(descriptor);
348   std::string full_name = GetClassFullName(
349       name_without_package, descriptor->file(), true, true, true);
350   std::string name_without_package_suffix =
351       absl::StrCat(".", name_without_package, "Kt");
352   size_t package_end = full_name.rfind(name_without_package_suffix);
353   if (package_end != std::string::npos) {
354     return absl::StrCat("`", full_name.substr(0, package_end), "`",
355                         name_without_package_suffix);
356   }
357   return full_name;
358 }
359 
GetJavaMutableClassName(const Descriptor * descriptor)360 std::string ClassNameResolver::GetJavaMutableClassName(
361     const Descriptor* descriptor) {
362   return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, false),
363                               descriptor->file(), false);
364 }
365 
GetJavaMutableClassName(const EnumDescriptor * descriptor)366 std::string ClassNameResolver::GetJavaMutableClassName(
367     const EnumDescriptor* descriptor) {
368   return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, false),
369                               descriptor->file(), false);
370 }
371 
GetJavaMutableClassName(const ServiceDescriptor * descriptor)372 std::string ClassNameResolver::GetJavaMutableClassName(
373     const ServiceDescriptor* descriptor) {
374   return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, false),
375                               descriptor->file(), false);
376 }
377 
GetDowngradedFileClassName(const FileDescriptor * file)378 std::string ClassNameResolver::GetDowngradedFileClassName(
379     const FileDescriptor* file) {
380   return absl::StrCat("Downgraded", GetFileClassName(file, false));
381 }
382 
GetDowngradedClassName(const Descriptor * descriptor)383 std::string ClassNameResolver::GetDowngradedClassName(
384     const Descriptor* descriptor) {
385   return absl::StrCat(FileJavaPackage(descriptor->file(), true, options_), ".",
386                       GetDowngradedFileClassName(descriptor->file()), ".",
387                       ClassNameWithoutPackage(descriptor, false));
388 }
389 
390 }  // namespace java
391 }  // namespace compiler
392 }  // namespace protobuf
393 }  // namespace google
394 
395 #include "google/protobuf/port_undef.inc"
396