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 #include <google/protobuf/compiler/java/java_name_resolver.h>
32
33 #include <map>
34 #include <string>
35
36 #include <google/protobuf/compiler/java/java_helpers.h>
37 #include <google/protobuf/stubs/substitute.h>
38
39 namespace google {
40 namespace protobuf {
41 namespace compiler {
42 namespace java {
43
44 namespace {
45 // A suffix that will be appended to the file's outer class name if the name
46 // conflicts with some other types defined in the file.
47 const char* kOuterClassNameSuffix = "OuterClass";
48
49 // Strip package name from a descriptor's full name.
50 // For example:
51 // Full name : foo.Bar.Baz
52 // Package name: foo
53 // After strip : Bar.Baz
StripPackageName(const std::string & full_name,const FileDescriptor * file)54 std::string StripPackageName(const std::string& full_name,
55 const FileDescriptor* file) {
56 if (file->package().empty()) {
57 return full_name;
58 } else {
59 // Strip package name
60 return full_name.substr(file->package().size() + 1);
61 }
62 }
63
64 // Get the name of a message's Java class without package name prefix.
ClassNameWithoutPackage(const Descriptor * descriptor,bool immutable)65 std::string ClassNameWithoutPackage(const Descriptor* descriptor,
66 bool immutable) {
67 return StripPackageName(descriptor->full_name(), descriptor->file());
68 }
69
70 // Get the name of an enum's Java class without package name prefix.
ClassNameWithoutPackage(const EnumDescriptor * descriptor,bool immutable)71 std::string ClassNameWithoutPackage(const EnumDescriptor* descriptor,
72 bool immutable) {
73 // Doesn't append "Mutable" for enum type's name.
74 const Descriptor* message_descriptor = descriptor->containing_type();
75 if (message_descriptor == NULL) {
76 return descriptor->name();
77 } else {
78 return ClassNameWithoutPackage(message_descriptor, immutable) + "." +
79 descriptor->name();
80 }
81 }
82
83 // Get the name of a service's Java class without package name prefix.
ClassNameWithoutPackage(const ServiceDescriptor * descriptor,bool immutable)84 std::string ClassNameWithoutPackage(const ServiceDescriptor* descriptor,
85 bool immutable) {
86 std::string full_name =
87 StripPackageName(descriptor->full_name(), descriptor->file());
88 // We don't allow nested service definitions.
89 GOOGLE_CHECK(full_name.find('.') == std::string::npos);
90 return full_name;
91 }
92
93 // Return true if a and b are equals (case insensitive).
CheckNameEquality(const std::string & a,const std::string & b)94 NameEquality CheckNameEquality(const std::string& a, const std::string& b) {
95 if (ToUpper(a) == ToUpper(b)) {
96 if (a == b) {
97 return NameEquality::EXACT_EQUAL;
98 }
99 return NameEquality::EQUAL_IGNORE_CASE;
100 }
101 return NameEquality::NO_MATCH;
102 }
103
104 // Check whether a given message or its nested types has the given class name.
MessageHasConflictingClassName(const Descriptor * message,const std::string & classname,NameEquality equality_mode)105 bool MessageHasConflictingClassName(const Descriptor* message,
106 const std::string& classname,
107 NameEquality equality_mode) {
108 if (CheckNameEquality(message->name(), classname) == equality_mode) {
109 return true;
110 }
111 for (int i = 0; i < message->nested_type_count(); ++i) {
112 if (MessageHasConflictingClassName(message->nested_type(i), classname,
113 equality_mode)) {
114 return true;
115 }
116 }
117 for (int i = 0; i < message->enum_type_count(); ++i) {
118 if (CheckNameEquality(message->enum_type(i)->name(), classname) ==
119 equality_mode) {
120 return true;
121 }
122 }
123 return false;
124 }
125
126 } // namespace
127
ClassNameResolver()128 ClassNameResolver::ClassNameResolver() {}
129
~ClassNameResolver()130 ClassNameResolver::~ClassNameResolver() {}
131
GetFileDefaultImmutableClassName(const FileDescriptor * file)132 std::string ClassNameResolver::GetFileDefaultImmutableClassName(
133 const FileDescriptor* file) {
134 std::string basename;
135 std::string::size_type last_slash = file->name().find_last_of('/');
136 if (last_slash == std::string::npos) {
137 basename = file->name();
138 } else {
139 basename = file->name().substr(last_slash + 1);
140 }
141 return UnderscoresToCamelCase(StripProto(basename), true);
142 }
143
GetFileImmutableClassName(const FileDescriptor * file)144 std::string ClassNameResolver::GetFileImmutableClassName(
145 const FileDescriptor* file) {
146 std::string& class_name = file_immutable_outer_class_names_[file];
147 if (class_name.empty()) {
148 if (file->options().has_java_outer_classname()) {
149 class_name = file->options().java_outer_classname();
150 } else {
151 class_name = GetFileDefaultImmutableClassName(file);
152 if (HasConflictingClassName(file, class_name,
153 NameEquality::EXACT_EQUAL)) {
154 class_name += kOuterClassNameSuffix;
155 }
156 }
157 }
158 return class_name;
159 }
160
GetFileClassName(const FileDescriptor * file,bool immutable)161 std::string ClassNameResolver::GetFileClassName(const FileDescriptor* file,
162 bool immutable) {
163 return GetFileClassName(file, immutable, false);
164 }
165
GetFileClassName(const FileDescriptor * file,bool immutable,bool kotlin)166 std::string ClassNameResolver::GetFileClassName(const FileDescriptor* file,
167 bool immutable, bool kotlin) {
168 if (kotlin) {
169 return GetFileImmutableClassName(file) + "Kt";
170 } else if (immutable) {
171 return GetFileImmutableClassName(file);
172 } else {
173 return "Mutable" + GetFileImmutableClassName(file);
174 }
175 }
176
177 // Check whether there is any type defined in the proto file that has
178 // the given class name.
HasConflictingClassName(const FileDescriptor * file,const std::string & classname,NameEquality equality_mode)179 bool ClassNameResolver::HasConflictingClassName(const FileDescriptor* file,
180 const std::string& classname,
181 NameEquality equality_mode) {
182 for (int i = 0; i < file->enum_type_count(); i++) {
183 if (CheckNameEquality(file->enum_type(i)->name(), classname) ==
184 equality_mode) {
185 return true;
186 }
187 }
188 for (int i = 0; i < file->service_count(); i++) {
189 if (CheckNameEquality(file->service(i)->name(), classname) ==
190 equality_mode) {
191 return true;
192 }
193 }
194 for (int i = 0; i < file->message_type_count(); i++) {
195 if (MessageHasConflictingClassName(file->message_type(i), classname,
196 equality_mode)) {
197 return true;
198 }
199 }
200 return false;
201 }
202
GetDescriptorClassName(const FileDescriptor * descriptor)203 std::string ClassNameResolver::GetDescriptorClassName(
204 const FileDescriptor* descriptor) {
205 return GetFileImmutableClassName(descriptor);
206 }
207
GetClassName(const FileDescriptor * descriptor,bool immutable)208 std::string ClassNameResolver::GetClassName(const FileDescriptor* descriptor,
209 bool immutable) {
210 return GetClassName(descriptor, immutable, false);
211 }
212
GetClassName(const FileDescriptor * descriptor,bool immutable,bool kotlin)213 std::string ClassNameResolver::GetClassName(const FileDescriptor* descriptor,
214 bool immutable, bool kotlin) {
215 std::string result = FileJavaPackage(descriptor, immutable);
216 if (!result.empty()) result += '.';
217 result += GetFileClassName(descriptor, immutable, kotlin);
218 return result;
219 }
220
221 // Get the full name of a Java class by prepending the Java package name
222 // or outer class name.
GetClassFullName(const std::string & name_without_package,const FileDescriptor * file,bool immutable,bool is_own_file)223 std::string ClassNameResolver::GetClassFullName(
224 const std::string& name_without_package, const FileDescriptor* file,
225 bool immutable, bool is_own_file) {
226 return GetClassFullName(name_without_package, file, immutable, is_own_file,
227 false);
228 }
229
GetClassFullName(const std::string & name_without_package,const FileDescriptor * file,bool immutable,bool is_own_file,bool kotlin)230 std::string ClassNameResolver::GetClassFullName(
231 const std::string& name_without_package, const FileDescriptor* file,
232 bool immutable, bool is_own_file, bool kotlin) {
233 std::string result;
234 if (is_own_file) {
235 result = FileJavaPackage(file, immutable);
236 } else {
237 result = GetClassName(file, immutable, kotlin);
238 }
239 if (!result.empty()) {
240 result += '.';
241 }
242 result += name_without_package;
243 if (kotlin) result += "Kt";
244 return result;
245 }
246
GetClassName(const Descriptor * descriptor,bool immutable)247 std::string ClassNameResolver::GetClassName(const Descriptor* descriptor,
248 bool immutable) {
249 return GetClassName(descriptor, immutable, false);
250 }
251
GetClassName(const Descriptor * descriptor,bool immutable,bool kotlin)252 std::string ClassNameResolver::GetClassName(const Descriptor* descriptor,
253 bool immutable, bool kotlin) {
254 return GetClassFullName(
255 ClassNameWithoutPackage(descriptor, immutable), descriptor->file(),
256 immutable, MultipleJavaFiles(descriptor->file(), immutable), kotlin);
257 }
258
GetClassName(const EnumDescriptor * descriptor,bool immutable)259 std::string ClassNameResolver::GetClassName(const EnumDescriptor* descriptor,
260 bool immutable) {
261 return GetClassName(descriptor, immutable, false);
262 }
263
GetClassName(const EnumDescriptor * descriptor,bool immutable,bool kotlin)264 std::string ClassNameResolver::GetClassName(const EnumDescriptor* descriptor,
265 bool immutable, bool kotlin) {
266 return GetClassFullName(
267 ClassNameWithoutPackage(descriptor, immutable), descriptor->file(),
268 immutable, MultipleJavaFiles(descriptor->file(), immutable), kotlin);
269 }
270
GetClassName(const ServiceDescriptor * descriptor,bool immutable)271 std::string ClassNameResolver::GetClassName(const ServiceDescriptor* descriptor,
272 bool immutable) {
273 return GetClassName(descriptor, immutable, false);
274 }
275
GetClassName(const ServiceDescriptor * descriptor,bool immutable,bool kotlin)276 std::string ClassNameResolver::GetClassName(const ServiceDescriptor* descriptor,
277 bool immutable, bool kotlin) {
278 return GetClassFullName(ClassNameWithoutPackage(descriptor, immutable),
279 descriptor->file(), immutable,
280 IsOwnFile(descriptor, immutable), kotlin);
281 }
282
283 // Get the Java Class style full name of a message.
GetJavaClassFullName(const std::string & name_without_package,const FileDescriptor * file,bool immutable)284 std::string ClassNameResolver::GetJavaClassFullName(
285 const std::string& name_without_package, const FileDescriptor* file,
286 bool immutable) {
287 return GetJavaClassFullName(name_without_package, file, immutable, false);
288 }
289
GetJavaClassFullName(const std::string & name_without_package,const FileDescriptor * file,bool immutable,bool kotlin)290 std::string ClassNameResolver::GetJavaClassFullName(
291 const std::string& name_without_package, const FileDescriptor* file,
292 bool immutable, bool kotlin) {
293 std::string result;
294 if (MultipleJavaFiles(file, immutable)) {
295 result = FileJavaPackage(file, immutable);
296 if (!result.empty()) result += '.';
297 } else {
298 result = GetClassName(file, immutable, kotlin);
299 if (!result.empty()) result += '$';
300 }
301 result += StringReplace(name_without_package, ".", "$", true);
302 return result;
303 }
304
GetExtensionIdentifierName(const FieldDescriptor * descriptor,bool immutable)305 std::string ClassNameResolver::GetExtensionIdentifierName(
306 const FieldDescriptor* descriptor, bool immutable) {
307 return GetExtensionIdentifierName(descriptor, immutable, false);
308 }
309
GetExtensionIdentifierName(const FieldDescriptor * descriptor,bool immutable,bool kotlin)310 std::string ClassNameResolver::GetExtensionIdentifierName(
311 const FieldDescriptor* descriptor, bool immutable, bool kotlin) {
312 return GetClassName(descriptor->containing_type(), immutable, kotlin) + "." +
313 descriptor->name();
314 }
315
GetJavaImmutableClassName(const Descriptor * descriptor)316 std::string ClassNameResolver::GetJavaImmutableClassName(
317 const Descriptor* descriptor) {
318 return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, true),
319 descriptor->file(), true);
320 }
321
GetJavaImmutableClassName(const EnumDescriptor * descriptor)322 std::string ClassNameResolver::GetJavaImmutableClassName(
323 const EnumDescriptor* descriptor) {
324 return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, true),
325 descriptor->file(), true);
326 }
327
328
329 } // namespace java
330 } // namespace compiler
331 } // namespace protobuf
332 } // namespace google
333