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 if (immutable) {
164 return GetFileImmutableClassName(file);
165 } else {
166 return "Mutable" + GetFileImmutableClassName(file);
167 }
168 }
169
170 // Check whether there is any type defined in the proto file that has
171 // the given class name.
HasConflictingClassName(const FileDescriptor * file,const std::string & classname,NameEquality equality_mode)172 bool ClassNameResolver::HasConflictingClassName(const FileDescriptor* file,
173 const std::string& classname,
174 NameEquality equality_mode) {
175 for (int i = 0; i < file->enum_type_count(); i++) {
176 if (CheckNameEquality(file->enum_type(i)->name(), classname) ==
177 equality_mode) {
178 return true;
179 }
180 }
181 for (int i = 0; i < file->service_count(); i++) {
182 if (CheckNameEquality(file->service(i)->name(), classname) ==
183 equality_mode) {
184 return true;
185 }
186 }
187 for (int i = 0; i < file->message_type_count(); i++) {
188 if (MessageHasConflictingClassName(file->message_type(i), classname,
189 equality_mode)) {
190 return true;
191 }
192 }
193 return false;
194 }
195
GetDescriptorClassName(const FileDescriptor * descriptor)196 std::string ClassNameResolver::GetDescriptorClassName(
197 const FileDescriptor* descriptor) {
198 return GetFileImmutableClassName(descriptor);
199 }
200
GetClassName(const FileDescriptor * descriptor,bool immutable)201 std::string ClassNameResolver::GetClassName(const FileDescriptor* descriptor,
202 bool immutable) {
203 std::string result = FileJavaPackage(descriptor, immutable);
204 if (!result.empty()) result += '.';
205 result += GetFileClassName(descriptor, immutable);
206 return result;
207 }
208
209 // Get the full name of a Java class by prepending the Java package name
210 // or outer class name.
GetClassFullName(const std::string & name_without_package,const FileDescriptor * file,bool immutable,bool is_own_file)211 std::string ClassNameResolver::GetClassFullName(
212 const std::string& name_without_package, const FileDescriptor* file,
213 bool immutable, bool is_own_file) {
214 std::string result;
215 if (is_own_file) {
216 result = FileJavaPackage(file, immutable);
217 } else {
218 result = GetClassName(file, immutable);
219 }
220 if (!result.empty()) {
221 result += '.';
222 }
223 result += name_without_package;
224 return result;
225 }
226
GetClassName(const Descriptor * descriptor,bool immutable)227 std::string ClassNameResolver::GetClassName(const Descriptor* descriptor,
228 bool immutable) {
229 return GetClassFullName(ClassNameWithoutPackage(descriptor, immutable),
230 descriptor->file(), immutable,
231 MultipleJavaFiles(descriptor->file(), immutable));
232 }
233
GetClassName(const EnumDescriptor * descriptor,bool immutable)234 std::string ClassNameResolver::GetClassName(const EnumDescriptor* descriptor,
235 bool immutable) {
236 return GetClassFullName(ClassNameWithoutPackage(descriptor, immutable),
237 descriptor->file(), immutable,
238 MultipleJavaFiles(descriptor->file(), immutable));
239 }
240
GetClassName(const ServiceDescriptor * descriptor,bool immutable)241 std::string ClassNameResolver::GetClassName(const ServiceDescriptor* descriptor,
242 bool immutable) {
243 return GetClassFullName(ClassNameWithoutPackage(descriptor, immutable),
244 descriptor->file(), immutable,
245 IsOwnFile(descriptor, immutable));
246 }
247
248 // Get the Java Class style full name of a message.
GetJavaClassFullName(const std::string & name_without_package,const FileDescriptor * file,bool immutable)249 std::string ClassNameResolver::GetJavaClassFullName(
250 const std::string& name_without_package, const FileDescriptor* file,
251 bool immutable) {
252 std::string result;
253 if (MultipleJavaFiles(file, immutable)) {
254 result = FileJavaPackage(file, immutable);
255 if (!result.empty()) result += '.';
256 } else {
257 result = GetClassName(file, immutable);
258 if (!result.empty()) result += '$';
259 }
260 result += StringReplace(name_without_package, ".", "$", true);
261 return result;
262 }
263
GetExtensionIdentifierName(const FieldDescriptor * descriptor,bool immutable)264 std::string ClassNameResolver::GetExtensionIdentifierName(
265 const FieldDescriptor* descriptor, bool immutable) {
266 return GetClassName(descriptor->containing_type(), immutable) + "." +
267 descriptor->name();
268 }
269
GetJavaImmutableClassName(const Descriptor * descriptor)270 std::string ClassNameResolver::GetJavaImmutableClassName(
271 const Descriptor* descriptor) {
272 return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, true),
273 descriptor->file(), true);
274 }
275
GetJavaImmutableClassName(const EnumDescriptor * descriptor)276 std::string ClassNameResolver::GetJavaImmutableClassName(
277 const EnumDescriptor* descriptor) {
278 return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, true),
279 descriptor->file(), true);
280 }
281
282
283 } // namespace java
284 } // namespace compiler
285 } // namespace protobuf
286 } // namespace google
287