java_message.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__
  34. #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__
  35. #include <string>
  36. #include <map>
  37. #include <google/protobuf/compiler/java/java_field.h>
  38. namespace google {
  39. namespace protobuf {
  40. namespace compiler {
  41. namespace java {
  42. class Context; // context.h
  43. class ClassNameResolver; // name_resolver.h
  44. }
  45. }
  46. namespace io {
  47. class Printer; // printer.h
  48. }
  49. }
  50. namespace protobuf {
  51. namespace compiler {
  52. namespace java {
  53. static const int kMaxStaticSize = 1 << 15; // aka 32k
  54. class MessageGenerator {
  55. public:
  56. explicit MessageGenerator(const Descriptor* descriptor);
  57. virtual ~MessageGenerator();
  58. // All static variables have to be declared at the top-level of the file
  59. // so that we can control initialization order, which is important for
  60. // DescriptorProto bootstrapping to work.
  61. virtual void GenerateStaticVariables(
  62. io::Printer* printer, int* bytecode_estimate) = 0;
  63. // Output code which initializes the static variables generated by
  64. // GenerateStaticVariables(). Returns an estimate of bytecode size.
  65. virtual int GenerateStaticVariableInitializers(io::Printer* printer) = 0;
  66. // Generate the class itself.
  67. virtual void Generate(io::Printer* printer) = 0;
  68. // Generates the base interface that both the class and its builder implement
  69. virtual void GenerateInterface(io::Printer* printer) = 0;
  70. // Generate code to register all contained extensions with an
  71. // ExtensionRegistry.
  72. virtual void GenerateExtensionRegistrationCode(io::Printer* printer) = 0;
  73. protected:
  74. const Descriptor* descriptor_;
  75. private:
  76. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator);
  77. };
  78. class ImmutableMessageGenerator : public MessageGenerator {
  79. public:
  80. ImmutableMessageGenerator(const Descriptor* descriptor, Context* context);
  81. virtual ~ImmutableMessageGenerator();
  82. virtual void Generate(io::Printer* printer);
  83. virtual void GenerateInterface(io::Printer* printer);
  84. virtual void GenerateExtensionRegistrationCode(io::Printer* printer);
  85. virtual void GenerateStaticVariables(
  86. io::Printer* printer, int* bytecode_estimate);
  87. // Returns an estimate of the number of bytes the printed code will compile to
  88. virtual int GenerateStaticVariableInitializers(io::Printer* printer);
  89. private:
  90. void GenerateFieldAccessorTable(io::Printer* printer, int* bytecode_estimate);
  91. // Returns an estimate of the number of bytes the printed code will compile to
  92. int GenerateFieldAccessorTableInitializer(io::Printer* printer);
  93. void GenerateMessageSerializationMethods(io::Printer* printer);
  94. void GenerateParseFromMethods(io::Printer* printer);
  95. void GenerateSerializeOneField(io::Printer* printer,
  96. const FieldDescriptor* field);
  97. void GenerateSerializeOneExtensionRange(
  98. io::Printer* printer, const Descriptor::ExtensionRange* range);
  99. void GenerateBuilder(io::Printer* printer);
  100. void GenerateIsInitialized(io::Printer* printer);
  101. void GenerateDescriptorMethods(io::Printer* printer);
  102. void GenerateInitializers(io::Printer* printer);
  103. void GenerateEqualsAndHashCode(io::Printer* printer);
  104. void GenerateParser(io::Printer* printer);
  105. void GenerateParsingConstructor(io::Printer* printer);
  106. void GenerateAnyMethods(io::Printer* printer);
  107. Context* context_;
  108. ClassNameResolver* name_resolver_;
  109. FieldGeneratorMap<ImmutableFieldGenerator> field_generators_;
  110. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableMessageGenerator);
  111. };
  112. } // namespace java
  113. } // namespace compiler
  114. } // namespace protobuf
  115. } // namespace google
  116. #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__