java_context.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include <google/protobuf/compiler/java/java_context.h>
  31. #include <google/protobuf/compiler/java/java_field.h>
  32. #include <google/protobuf/compiler/java/java_helpers.h>
  33. #include <google/protobuf/compiler/java/java_name_resolver.h>
  34. #include <google/protobuf/descriptor.h>
  35. #include <google/protobuf/stubs/strutil.h>
  36. #include <google/protobuf/stubs/map_util.h>
  37. namespace google {
  38. namespace protobuf {
  39. namespace compiler {
  40. namespace java {
  41. Context::Context(const FileDescriptor* file, const Options& options)
  42. : name_resolver_(new ClassNameResolver), options_(options) {
  43. InitializeFieldGeneratorInfo(file);
  44. }
  45. Context::~Context() {
  46. }
  47. ClassNameResolver* Context::GetNameResolver() const {
  48. return name_resolver_.get();
  49. }
  50. namespace {
  51. // Whether two fields have conflicting accessors (assuming name1 and name2
  52. // are different). name1 and name2 are field1 and field2's camel-case name
  53. // respectively.
  54. bool IsConflicting(const FieldDescriptor* field1, const string& name1,
  55. const FieldDescriptor* field2, const string& name2,
  56. string* info) {
  57. if (field1->is_repeated()) {
  58. if (field2->is_repeated()) {
  59. // Both fields are repeated.
  60. return false;
  61. } else {
  62. // field1 is repeated, and field2 is not.
  63. if (name1 + "Count" == name2) {
  64. *info = "both repeated field \"" + field1->name() + "\" and singular " +
  65. "field \"" + field2->name() + "\" generate the method \"" +
  66. "get" + name1 + "Count()\"";
  67. return true;
  68. }
  69. if (name1 + "List" == name2) {
  70. *info = "both repeated field \"" + field1->name() + "\" and singular " +
  71. "field \"" + field2->name() + "\" generate the method \"" +
  72. "get" + name1 + "List()\"";
  73. return true;
  74. }
  75. // Well, there are obviously many more conflicting cases, but it probably
  76. // doesn't worth the effort to exhaust all of them because they rarely
  77. // happen and as we are continuing adding new methods/changing existing
  78. // methods the number of different conflicting cases will keep growing.
  79. // We can just add more cases here when they are found in the real world.
  80. return false;
  81. }
  82. } else {
  83. if (field2->is_repeated()) {
  84. return IsConflicting(field2, name2, field1, name1, info);
  85. } else {
  86. // None of the two fields are repeated.
  87. return false;
  88. }
  89. }
  90. }
  91. } // namespace
  92. void Context::InitializeFieldGeneratorInfo(const FileDescriptor* file) {
  93. for (int i = 0; i < file->message_type_count(); ++i) {
  94. InitializeFieldGeneratorInfoForMessage(file->message_type(i));
  95. }
  96. }
  97. void Context::InitializeFieldGeneratorInfoForMessage(
  98. const Descriptor* message) {
  99. for (int i = 0; i < message->nested_type_count(); ++i) {
  100. InitializeFieldGeneratorInfoForMessage(message->nested_type(i));
  101. }
  102. std::vector<const FieldDescriptor*> fields;
  103. for (int i = 0; i < message->field_count(); ++i) {
  104. fields.push_back(message->field(i));
  105. }
  106. InitializeFieldGeneratorInfoForFields(fields);
  107. for (int i = 0; i < message->oneof_decl_count(); ++i) {
  108. const OneofDescriptor* oneof = message->oneof_decl(i);
  109. OneofGeneratorInfo info;
  110. info.name = UnderscoresToCamelCase(oneof->name(), false);
  111. info.capitalized_name = UnderscoresToCamelCase(oneof->name(), true);
  112. oneof_generator_info_map_[oneof] = info;
  113. }
  114. }
  115. void Context::InitializeFieldGeneratorInfoForFields(
  116. const std::vector<const FieldDescriptor*>& fields) {
  117. // Find out all fields that conflict with some other field in the same
  118. // message.
  119. std::vector<bool> is_conflict(fields.size());
  120. std::vector<string> conflict_reason(fields.size());
  121. for (int i = 0; i < fields.size(); ++i) {
  122. const FieldDescriptor* field = fields[i];
  123. const string& name = UnderscoresToCapitalizedCamelCase(field);
  124. for (int j = i + 1; j < fields.size(); ++j) {
  125. const FieldDescriptor* other = fields[j];
  126. const string& other_name = UnderscoresToCapitalizedCamelCase(other);
  127. if (name == other_name) {
  128. is_conflict[i] = is_conflict[j] = true;
  129. conflict_reason[i] = conflict_reason[j] =
  130. "capitalized name of field \"" + field->name() +
  131. "\" conflicts with field \"" + other->name() + "\"";
  132. } else if (IsConflicting(field, name, other, other_name,
  133. &conflict_reason[j])) {
  134. is_conflict[i] = is_conflict[j] = true;
  135. conflict_reason[i] = conflict_reason[j];
  136. }
  137. }
  138. if (is_conflict[i]) {
  139. GOOGLE_LOG(WARNING) << "field \"" << field->full_name() << "\" is conflicting "
  140. << "with another field: " << conflict_reason[i];
  141. }
  142. }
  143. for (int i = 0; i < fields.size(); ++i) {
  144. const FieldDescriptor* field = fields[i];
  145. FieldGeneratorInfo info;
  146. info.name = CamelCaseFieldName(field);
  147. info.capitalized_name = UnderscoresToCapitalizedCamelCase(field);
  148. // For fields conflicting with some other fields, we append the field
  149. // number to their field names in generated code to avoid conflicts.
  150. if (is_conflict[i]) {
  151. info.name += SimpleItoa(field->number());
  152. info.capitalized_name += SimpleItoa(field->number());
  153. info.disambiguated_reason = conflict_reason[i];
  154. }
  155. field_generator_info_map_[field] = info;
  156. }
  157. }
  158. const FieldGeneratorInfo* Context::GetFieldGeneratorInfo(
  159. const FieldDescriptor* field) const {
  160. const FieldGeneratorInfo* result =
  161. FindOrNull(field_generator_info_map_, field);
  162. if (result == NULL) {
  163. GOOGLE_LOG(FATAL) << "Can not find FieldGeneratorInfo for field: "
  164. << field->full_name();
  165. }
  166. return result;
  167. }
  168. const OneofGeneratorInfo* Context::GetOneofGeneratorInfo(
  169. const OneofDescriptor* oneof) const {
  170. const OneofGeneratorInfo* result =
  171. FindOrNull(oneof_generator_info_map_, oneof);
  172. if (result == NULL) {
  173. GOOGLE_LOG(FATAL) << "Can not find OneofGeneratorInfo for oneof: "
  174. << oneof->name();
  175. }
  176. return result;
  177. }
  178. // Does this message class have generated parsing, serialization, and other
  179. // standard methods for which reflection-based fallback implementations exist?
  180. bool Context::HasGeneratedMethods(const Descriptor* descriptor) const {
  181. return options_.enforce_lite ||
  182. descriptor->file()->options().optimize_for() != FileOptions::CODE_SIZE;
  183. }
  184. } // namespace java
  185. } // namespace compiler
  186. } // namespace protobuf
  187. } // namespace google