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 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_NAME_RESOLVER_H__ 9 #define GOOGLE_PROTOBUF_COMPILER_JAVA_NAME_RESOLVER_H__ 10 11 #include <string> 12 13 #include "absl/container/flat_hash_map.h" 14 #include "google/protobuf/compiler/java/options.h" 15 #include "google/protobuf/port.h" 16 17 // Must be last. 18 #include "google/protobuf/port_def.inc" 19 20 namespace google { 21 namespace protobuf { 22 class Descriptor; 23 class EnumDescriptor; 24 class FieldDescriptor; 25 class FileDescriptor; 26 class ServiceDescriptor; 27 28 namespace compiler { 29 namespace java { 30 31 // Indicates how closely the two class names match. 32 enum NameEquality { NO_MATCH, EXACT_EQUAL, EQUAL_IGNORE_CASE }; 33 34 // Used to get the Java class related names for a given descriptor. It caches 35 // the results to avoid redundant calculation across multiple name queries. 36 // Thread-safety note: This class is *not* thread-safe. 37 class ClassNameResolver { 38 public: options_(options)39 explicit ClassNameResolver(const Options& options = {}) : options_(options) {} 40 ~ClassNameResolver() = default; 41 42 ClassNameResolver(const ClassNameResolver&) = delete; 43 ClassNameResolver& operator=(const ClassNameResolver&) = delete; 44 45 // Gets the unqualified outer class name for the file. 46 std::string GetFileClassName(const FileDescriptor* file, bool immutable); 47 std::string GetFileClassName(const FileDescriptor* file, bool immutable, 48 bool kotlin); 49 // Gets the unqualified immutable outer class name of a file. 50 std::string GetFileImmutableClassName(const FileDescriptor* file); 51 // Gets the unqualified default immutable outer class name of a file 52 // (converted from the proto file's name). 53 std::string GetFileDefaultImmutableClassName(const FileDescriptor* file); 54 55 // Check whether there is any type defined in the proto file that has 56 // the given class name. 57 bool HasConflictingClassName(const FileDescriptor* file, 58 absl::string_view classname, 59 NameEquality equality_mode); 60 61 // Gets the name of the outer class that holds descriptor information. 62 // Descriptors are shared between immutable messages and mutable messages. 63 // Since both of them are generated optionally, the descriptors need to be 64 // put in another common place. 65 std::string GetDescriptorClassName(const FileDescriptor* file); 66 67 // Gets the fully-qualified class name corresponding to the given descriptor. 68 std::string GetClassName(const Descriptor* descriptor, bool immutable); 69 std::string GetClassName(const Descriptor* descriptor, bool immutable, 70 bool kotlin); 71 std::string GetClassName(const EnumDescriptor* descriptor, bool immutable); 72 std::string GetClassName(const EnumDescriptor* descriptor, bool immutable, 73 bool kotlin); 74 std::string GetClassName(const ServiceDescriptor* descriptor, bool immutable); 75 std::string GetClassName(const ServiceDescriptor* descriptor, bool immutable, 76 bool kotlin); 77 std::string GetClassName(const FileDescriptor* descriptor, bool immutable); 78 std::string GetClassName(const FileDescriptor* descriptor, bool immutable, 79 bool kotlin); 80 81 template <class DescriptorType> GetImmutableClassName(const DescriptorType * descriptor)82 std::string GetImmutableClassName(const DescriptorType* descriptor) { 83 return GetClassName(descriptor, true); 84 } 85 template <class DescriptorType> GetMutableClassName(const DescriptorType * descriptor)86 std::string GetMutableClassName(const DescriptorType* descriptor) { 87 return GetClassName(descriptor, false); 88 } 89 90 // Gets the fully qualified name of an extension identifier. 91 std::string GetExtensionIdentifierName(const FieldDescriptor* descriptor, 92 bool immutable); 93 std::string GetExtensionIdentifierName(const FieldDescriptor* descriptor, 94 bool immutable, bool kotlin); 95 96 // Gets the fully qualified name for generated classes in Java convention. 97 // Nested classes will be separated using '$' instead of '.' 98 // For example: 99 // com.package.OuterClass$OuterMessage$InnerMessage 100 std::string GetJavaImmutableClassName(const Descriptor* descriptor); 101 std::string GetJavaImmutableClassName(const EnumDescriptor* descriptor); 102 std::string GetJavaImmutableClassName(const ServiceDescriptor* descriptor); 103 std::string GetKotlinFactoryName(const Descriptor* descriptor); 104 std::string GetKotlinExtensionsClassName(const Descriptor* descriptor); 105 std::string GetKotlinExtensionsClassNameEscaped(const Descriptor* descriptor); 106 std::string GetJavaMutableClassName(const Descriptor* descriptor); 107 std::string GetJavaMutableClassName(const EnumDescriptor* descriptor); 108 std::string GetJavaMutableClassName(const ServiceDescriptor* descriptor); 109 // Gets the outer class and the actual class for downgraded mutable messages. 110 std::string GetDowngradedFileClassName(const FileDescriptor* file); 111 std::string GetDowngradedClassName(const Descriptor* descriptor); 112 113 // Get the full name of a Java class by prepending the Java package name 114 // or outer class name. 115 std::string GetClassFullName(absl::string_view name_without_package, 116 const FileDescriptor* file, bool immutable, 117 bool is_own_file); 118 std::string GetClassFullName(absl::string_view name_without_package, 119 const FileDescriptor* file, bool immutable, 120 bool is_own_file, bool kotlin); 121 122 Options options_; 123 124 private: 125 // Get the Java Class style full name of a message. 126 std::string GetJavaClassFullName(absl::string_view name_without_package, 127 const FileDescriptor* file, bool immutable); 128 std::string GetJavaClassFullName(absl::string_view name_without_package, 129 const FileDescriptor* file, bool immutable, 130 bool kotlin); 131 // Caches the result to provide better performance. 132 absl::flat_hash_map<const FileDescriptor*, std::string> 133 file_immutable_outer_class_names_; 134 }; 135 136 } // namespace java 137 } // namespace compiler 138 } // namespace protobuf 139 } // namespace google 140 141 #include "google/protobuf/port_undef.inc" 142 143 #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_NAME_RESOLVER_H__ 144