Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:11:59

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 namespace google {
0021 namespace protobuf {
0022 class Descriptor;
0023 class EnumDescriptor;
0024 class FieldDescriptor;
0025 class FileDescriptor;
0026 class ServiceDescriptor;
0027 
0028 namespace compiler {
0029 namespace java {
0030 
0031 // Indicates how closely the two class names match.
0032 enum NameEquality { NO_MATCH, EXACT_EQUAL, EQUAL_IGNORE_CASE };
0033 
0034 // Used to get the Java class related names for a given descriptor. It caches
0035 // the results to avoid redundant calculation across multiple name queries.
0036 // Thread-safety note: This class is *not* thread-safe.
0037 class ClassNameResolver {
0038  public:
0039   explicit ClassNameResolver(const Options& options = {}) : options_(options) {}
0040   ~ClassNameResolver() = default;
0041 
0042   ClassNameResolver(const ClassNameResolver&) = delete;
0043   ClassNameResolver& operator=(const ClassNameResolver&) = delete;
0044 
0045   // Gets the unqualified outer class name for the file.
0046   std::string GetFileClassName(const FileDescriptor* file, bool immutable);
0047   std::string GetFileClassName(const FileDescriptor* file, bool immutable,
0048                                bool kotlin);
0049   // Gets the unqualified immutable outer class name of a file.
0050   std::string GetFileImmutableClassName(const FileDescriptor* file);
0051   // Gets the unqualified default immutable outer class name of a file
0052   // (converted from the proto file's name).
0053   std::string GetFileDefaultImmutableClassName(const FileDescriptor* file);
0054 
0055   // Check whether there is any type defined in the proto file that has
0056   // the given class name.
0057   bool HasConflictingClassName(const FileDescriptor* file,
0058                                absl::string_view classname,
0059                                NameEquality equality_mode);
0060 
0061   // Gets the name of the outer class that holds descriptor information.
0062   // Descriptors are shared between immutable messages and mutable messages.
0063   // Since both of them are generated optionally, the descriptors need to be
0064   // put in another common place.
0065   std::string GetDescriptorClassName(const FileDescriptor* file);
0066 
0067   // Gets the fully-qualified class name corresponding to the given descriptor.
0068   std::string GetClassName(const Descriptor* descriptor, bool immutable);
0069   std::string GetClassName(const Descriptor* descriptor, bool immutable,
0070                            bool kotlin);
0071   std::string GetClassName(const EnumDescriptor* descriptor, bool immutable);
0072   std::string GetClassName(const EnumDescriptor* descriptor, bool immutable,
0073                            bool kotlin);
0074   std::string GetClassName(const ServiceDescriptor* descriptor, bool immutable);
0075   std::string GetClassName(const ServiceDescriptor* descriptor, bool immutable,
0076                            bool kotlin);
0077   std::string GetClassName(const FileDescriptor* descriptor, bool immutable);
0078   std::string GetClassName(const FileDescriptor* descriptor, bool immutable,
0079                            bool kotlin);
0080 
0081   template <class DescriptorType>
0082   std::string GetImmutableClassName(const DescriptorType* descriptor) {
0083     return GetClassName(descriptor, true);
0084   }
0085   template <class DescriptorType>
0086   std::string GetMutableClassName(const DescriptorType* descriptor) {
0087     return GetClassName(descriptor, false);
0088   }
0089 
0090   // Gets the fully qualified name of an extension identifier.
0091   std::string GetExtensionIdentifierName(const FieldDescriptor* descriptor,
0092                                          bool immutable);
0093   std::string GetExtensionIdentifierName(const FieldDescriptor* descriptor,
0094                                          bool immutable, bool kotlin);
0095 
0096   // Gets the fully qualified name for generated classes in Java convention.
0097   // Nested classes will be separated using '$' instead of '.'
0098   // For example:
0099   //   com.package.OuterClass$OuterMessage$InnerMessage
0100   std::string GetJavaImmutableClassName(const Descriptor* descriptor);
0101   std::string GetJavaImmutableClassName(const EnumDescriptor* descriptor);
0102   std::string GetJavaImmutableClassName(const ServiceDescriptor* descriptor);
0103   std::string GetKotlinFactoryName(const Descriptor* descriptor);
0104   std::string GetKotlinExtensionsClassName(const Descriptor* descriptor);
0105   std::string GetKotlinExtensionsClassNameEscaped(const Descriptor* descriptor);
0106   std::string GetJavaMutableClassName(const Descriptor* descriptor);
0107   std::string GetJavaMutableClassName(const EnumDescriptor* descriptor);
0108   std::string GetJavaMutableClassName(const ServiceDescriptor* descriptor);
0109   // Gets the outer class and the actual class for downgraded mutable messages.
0110   std::string GetDowngradedFileClassName(const FileDescriptor* file);
0111   std::string GetDowngradedClassName(const Descriptor* descriptor);
0112 
0113   // Get the full name of a Java class by prepending the Java package name
0114   // or outer class name.
0115   std::string GetClassFullName(absl::string_view name_without_package,
0116                                const FileDescriptor* file, bool immutable,
0117                                bool is_own_file);
0118   std::string GetClassFullName(absl::string_view name_without_package,
0119                                const FileDescriptor* file, bool immutable,
0120                                bool is_own_file, bool kotlin);
0121 
0122   Options options_;
0123 
0124  private:
0125   // Get the Java Class style full name of a message.
0126   std::string GetJavaClassFullName(absl::string_view name_without_package,
0127                                    const FileDescriptor* file, bool immutable);
0128   std::string GetJavaClassFullName(absl::string_view name_without_package,
0129                                    const FileDescriptor* file, bool immutable,
0130                                    bool kotlin);
0131   // Caches the result to provide better performance.
0132   absl::flat_hash_map<const FileDescriptor*, std::string>
0133       file_immutable_outer_class_names_;
0134 };
0135 
0136 }  // namespace java
0137 }  // namespace compiler
0138 }  // namespace protobuf
0139 }  // namespace google
0140 
0141 #include "google/protobuf/port_undef.inc"
0142 
0143 #endif  // GOOGLE_PROTOBUF_COMPILER_JAVA_NAME_RESOLVER_H__