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