java_name_resolver.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_NAME_RESOLVER_H__
  31. #define GOOGLE_PROTOBUF_COMPILER_JAVA_NAME_RESOLVER_H__
  32. #include <map>
  33. #include <string>
  34. #include <google/protobuf/stubs/common.h>
  35. namespace google {
  36. namespace protobuf {
  37. class Descriptor;
  38. class EnumDescriptor;
  39. class FieldDescriptor;
  40. class FileDescriptor;
  41. class ServiceDescriptor;
  42. namespace compiler {
  43. namespace java {
  44. // Used to get the Java class related names for a given descriptor. It caches
  45. // the results to avoid redundant calculation across multiple name queries.
  46. // Thread-safety note: This class is *not* thread-safe.
  47. class ClassNameResolver {
  48. public:
  49. ClassNameResolver();
  50. ~ClassNameResolver();
  51. // Gets the unqualified outer class name for the file.
  52. string GetFileClassName(const FileDescriptor* file, bool immutable);
  53. // Gets the unqualified immutable outer class name of a file.
  54. string GetFileImmutableClassName(const FileDescriptor* file);
  55. // Gets the unqualified default immutable outer class name of a file
  56. // (converted from the proto file's name).
  57. string GetFileDefaultImmutableClassName(const FileDescriptor* file);
  58. // Check whether there is any type defined in the proto file that has
  59. // the given class name.
  60. bool HasConflictingClassName(const FileDescriptor* file,
  61. const string& classname);
  62. // Gets the name of the outer class that holds descriptor information.
  63. // Descriptors are shared between immutable messages and mutable messages.
  64. // Since both of them are generated optionally, the descriptors need to be
  65. // put in another common place.
  66. string GetDescriptorClassName(const FileDescriptor* file);
  67. // Gets the fully-qualified class name corresponding to the given descriptor.
  68. string GetClassName(const Descriptor* descriptor, bool immutable);
  69. string GetClassName(const EnumDescriptor* descriptor, bool immutable);
  70. string GetClassName(const ServiceDescriptor* descriptor, bool immutable);
  71. string GetClassName(const FileDescriptor* descriptor, bool immutable);
  72. template<class DescriptorType>
  73. string GetImmutableClassName(const DescriptorType* descriptor) {
  74. return GetClassName(descriptor, true);
  75. }
  76. template<class DescriptorType>
  77. string GetMutableClassName(const DescriptorType* descriptor) {
  78. return GetClassName(descriptor, false);
  79. }
  80. // Gets the fully qualified name of an extension identifier.
  81. string GetExtensionIdentifierName(const FieldDescriptor* descriptor,
  82. bool immutable);
  83. // Gets the fully qualified name for generated classes in Java convention.
  84. // Nested classes will be separated using '$' instead of '.'
  85. // For example:
  86. // com.package.OuterClass$OuterMessage$InnerMessage
  87. string GetJavaImmutableClassName(const Descriptor* descriptor);
  88. string GetJavaImmutableClassName(const EnumDescriptor* descriptor);
  89. private:
  90. // Get the full name of a Java class by prepending the Java package name
  91. // or outer class name.
  92. string GetClassFullName(const string& name_without_package,
  93. const FileDescriptor* file,
  94. bool immutable,
  95. bool multiple_files);
  96. // Get the Java Class style full name of a message.
  97. string GetJavaClassFullName(
  98. const string& name_without_package,
  99. const FileDescriptor* file,
  100. bool immutable);
  101. // Caches the result to provide better performance.
  102. std::map<const FileDescriptor*, string> file_immutable_outer_class_names_;
  103. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ClassNameResolver);
  104. };
  105. } // namespace java
  106. } // namespace compiler
  107. } // namespace protobuf
  108. } // namespace google
  109. #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_NAME_RESOLVER_H__