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