Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-02 09:18:38

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_NAME_RESOLVER_H__
0009 #define GOOGLE_PROTOBUF_COMPILER_JAVA_NAME_RESOLVER_H__
0010 
0011 #include <string>
0012 
0013 #include "absl/container/flat_hash_map.h"
0014 #include "google/protobuf/compiler/java/options.h"
0015 #include "google/protobuf/port.h"
0016 
0017 // Must be last.
0018 #include "google/protobuf/port_def.inc"
0019 
0020 
0021 namespace google {
0022 namespace protobuf {
0023 class Descriptor;
0024 class EnumDescriptor;
0025 class FieldDescriptor;
0026 class FileDescriptor;
0027 class ServiceDescriptor;
0028 
0029 namespace compiler {
0030 namespace java {
0031 
0032 // Indicates how closely the two class names match.
0033 enum NameEquality { NO_MATCH, EXACT_EQUAL, EQUAL_IGNORE_CASE };
0034 
0035 // Used to get the Java class related names for a given descriptor. It caches
0036 // the results to avoid redundant calculation across multiple name queries.
0037 // Thread-safety note: This class is *not* thread-safe.
0038 class PROTOC_EXPORT ClassNameResolver {
0039  public:
0040   explicit ClassNameResolver(const Options& options = {}) : options_(options) {}
0041   ~ClassNameResolver() = default;
0042 
0043   ClassNameResolver(const ClassNameResolver&) = delete;
0044   ClassNameResolver& operator=(const ClassNameResolver&) = delete;
0045 
0046   // Gets the unqualified outer class name for the file.
0047   std::string GetFileClassName(const FileDescriptor* file, bool immutable);
0048   std::string GetFileClassName(const FileDescriptor* file, bool immutable,
0049                                bool kotlin);
0050   // Gets the unqualified immutable outer class name of a file.
0051   std::string GetFileImmutableClassName(const FileDescriptor* file);
0052   // Gets the unqualified default immutable outer class name of a file
0053   // (converted from the proto file's name).
0054   static std::string GetFileDefaultImmutableClassName(
0055       const FileDescriptor* file);
0056 
0057   // Check whether there is any type defined in the proto file that has
0058   // the given class name.
0059   static bool HasConflictingClassName(const FileDescriptor* file,
0060                                       absl::string_view classname,
0061                                       NameEquality equality_mode);
0062 
0063   // Gets the name of the outer class that holds descriptor information.
0064   // Descriptors are shared between immutable messages and mutable messages.
0065   // Since both of them are generated optionally, the descriptors need to be
0066   // put in another common place.
0067   std::string GetDescriptorClassName(const FileDescriptor* file);
0068 
0069   // Gets the fully-qualified class name corresponding to the given descriptor.
0070   std::string GetClassName(const Descriptor* descriptor, bool immutable);
0071   std::string GetClassName(const Descriptor* descriptor, bool immutable,
0072                            bool kotlin);
0073   std::string GetClassName(const EnumDescriptor* descriptor, bool immutable);
0074   std::string GetClassName(const EnumDescriptor* descriptor, bool immutable,
0075                            bool kotlin);
0076   std::string GetClassName(const ServiceDescriptor* descriptor, bool immutable);
0077   std::string GetClassName(const ServiceDescriptor* descriptor, bool immutable,
0078                            bool kotlin);
0079   std::string GetClassName(const FileDescriptor* descriptor, bool immutable);
0080   std::string GetClassName(const FileDescriptor* descriptor, bool immutable,
0081                            bool kotlin);
0082 
0083   template <class DescriptorType>
0084   std::string GetImmutableClassName(const DescriptorType* descriptor) {
0085     return GetClassName(descriptor, true);
0086   }
0087 
0088   // Gets the fully qualified name of an extension identifier.
0089   std::string GetExtensionIdentifierName(const FieldDescriptor* descriptor,
0090                                          bool immutable);
0091   std::string GetExtensionIdentifierName(const FieldDescriptor* descriptor,
0092                                          bool immutable, bool kotlin);
0093 
0094   // Gets the fully qualified name for generated classes in Java convention.
0095   // Nested classes will be separated using '$' instead of '.'
0096   // For example:
0097   //   com.package.OuterClass$OuterMessage$InnerMessage
0098   std::string GetJavaImmutableClassName(const Descriptor* descriptor);
0099   std::string GetJavaImmutableClassName(const EnumDescriptor* descriptor);
0100   std::string GetJavaImmutableClassName(const ServiceDescriptor* descriptor);
0101   std::string GetKotlinFactoryName(const Descriptor* descriptor);
0102   std::string GetFullyQualifiedKotlinFactoryName(const Descriptor* descriptor);
0103   std::string GetKotlinExtensionsClassName(const Descriptor* descriptor);
0104   std::string GetKotlinExtensionsClassNameEscaped(const Descriptor* descriptor);
0105   std::string GetFileJavaPackage(const FileDescriptor* file, bool immutable);
0106 
0107   // Get the full name of a Java class by prepending the Java package name
0108   // or outer class name.
0109   std::string GetClassFullName(absl::string_view name_without_package,
0110                                const FileDescriptor* file, bool immutable,
0111                                bool is_own_file);
0112   std::string GetClassFullName(absl::string_view name_without_package,
0113                                const FileDescriptor* file, bool immutable,
0114                                bool is_own_file, bool kotlin);
0115 
0116   Options options_;
0117 
0118  private:
0119   // Get the Java Class style full name of a message.
0120   template <typename Descriptor>
0121   std::string GetJavaClassFullName(absl::string_view name_without_package,
0122                                    const Descriptor& descriptor,
0123                                    bool immutable);
0124   template <typename Descriptor>
0125   std::string GetJavaClassFullName(absl::string_view name_without_package,
0126                                    const Descriptor& descriptor, bool immutable,
0127                                    bool kotlin);
0128 
0129   template <typename Descriptor>
0130   std::string GetJavaClassPackage(const Descriptor& descriptor, bool immutable);
0131 
0132 };
0133 
0134 }  // namespace java
0135 }  // namespace compiler
0136 }  // namespace protobuf
0137 }  // namespace google
0138 
0139 #include "google/protobuf/port_undef.inc"
0140 
0141 #endif  // GOOGLE_PROTOBUF_COMPILER_JAVA_NAME_RESOLVER_H__