PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/google/protobuf/compiler/java/java_helpers.h

http://protobuf.googlecode.com/
C Header | 220 lines | 89 code | 39 blank | 92 comment | 1 complexity | af116643ccae9b13dc0eadb770a23cae MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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_HELPERS_H__
  34. #define GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
  35. #include <string>
  36. #include <google/protobuf/descriptor.pb.h>
  37. #include <google/protobuf/descriptor.h>
  38. namespace google {
  39. namespace protobuf {
  40. namespace compiler {
  41. namespace java {
  42. // Commonly-used separator comments. Thick is a line of '=', thin is a line
  43. // of '-'.
  44. extern const char kThickSeparator[];
  45. extern const char kThinSeparator[];
  46. // Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes
  47. // "fooBarBaz" or "FooBarBaz", respectively.
  48. string UnderscoresToCamelCase(const FieldDescriptor* field);
  49. string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field);
  50. // Similar, but for method names. (Typically, this merely has the effect
  51. // of lower-casing the first letter of the name.)
  52. string UnderscoresToCamelCase(const MethodDescriptor* method);
  53. // Strips ".proto" or ".protodevel" from the end of a filename.
  54. string StripProto(const string& filename);
  55. // Gets the unqualified class name for the file. Each .proto file becomes a
  56. // single Java class, with all its contents nested in that class.
  57. string FileClassName(const FileDescriptor* file);
  58. // Returns the file's Java package name.
  59. string FileJavaPackage(const FileDescriptor* file);
  60. // Returns output directory for the given package name.
  61. string JavaPackageToDir(string package_name);
  62. // Converts the given fully-qualified name in the proto namespace to its
  63. // fully-qualified name in the Java namespace, given that it is in the given
  64. // file.
  65. string ToJavaName(const string& full_name, const FileDescriptor* file);
  66. // These return the fully-qualified class name corresponding to the given
  67. // descriptor.
  68. string ClassName(const Descriptor* descriptor);
  69. string ClassName(const EnumDescriptor* descriptor);
  70. string ClassName(const ServiceDescriptor* descriptor);
  71. string ClassName(const FileDescriptor* descriptor);
  72. inline string ExtensionIdentifierName(const FieldDescriptor* descriptor) {
  73. return ToJavaName(descriptor->full_name(), descriptor->file());
  74. }
  75. // Get the unqualified name that should be used for a field's field
  76. // number constant.
  77. string FieldConstantName(const FieldDescriptor *field);
  78. // Returns the type of the FieldDescriptor.
  79. // This does nothing interesting for the open source release, but is used for
  80. // hacks that improve compatability with version 1 protocol buffers at Google.
  81. FieldDescriptor::Type GetType(const FieldDescriptor* field);
  82. enum JavaType {
  83. JAVATYPE_INT,
  84. JAVATYPE_LONG,
  85. JAVATYPE_FLOAT,
  86. JAVATYPE_DOUBLE,
  87. JAVATYPE_BOOLEAN,
  88. JAVATYPE_STRING,
  89. JAVATYPE_BYTES,
  90. JAVATYPE_ENUM,
  91. JAVATYPE_MESSAGE
  92. };
  93. JavaType GetJavaType(const FieldDescriptor* field);
  94. // Get the fully-qualified class name for a boxed primitive type, e.g.
  95. // "java.lang.Integer" for JAVATYPE_INT. Returns NULL for enum and message
  96. // types.
  97. const char* BoxedPrimitiveTypeName(JavaType type);
  98. string DefaultValue(const FieldDescriptor* field);
  99. bool IsDefaultValueJavaDefault(const FieldDescriptor* field);
  100. // Does this message class keep track of unknown fields?
  101. inline bool HasUnknownFields(const Descriptor* descriptor) {
  102. return descriptor->file()->options().optimize_for() !=
  103. FileOptions::LITE_RUNTIME;
  104. }
  105. // Does this message class have generated parsing, serialization, and other
  106. // standard methods for which reflection-based fallback implementations exist?
  107. inline bool HasGeneratedMethods(const Descriptor* descriptor) {
  108. return descriptor->file()->options().optimize_for() !=
  109. FileOptions::CODE_SIZE;
  110. }
  111. // Does this message have specialized equals() and hashCode() methods?
  112. inline bool HasEqualsAndHashCode(const Descriptor* descriptor) {
  113. return descriptor->file()->options().java_generate_equals_and_hash();
  114. }
  115. // Does this message class have descriptor and reflection methods?
  116. inline bool HasDescriptorMethods(const Descriptor* descriptor) {
  117. return descriptor->file()->options().optimize_for() !=
  118. FileOptions::LITE_RUNTIME;
  119. }
  120. inline bool HasDescriptorMethods(const EnumDescriptor* descriptor) {
  121. return descriptor->file()->options().optimize_for() !=
  122. FileOptions::LITE_RUNTIME;
  123. }
  124. inline bool HasDescriptorMethods(const FileDescriptor* descriptor) {
  125. return descriptor->options().optimize_for() !=
  126. FileOptions::LITE_RUNTIME;
  127. }
  128. inline bool HasNestedBuilders(const Descriptor* descriptor) {
  129. // The proto-lite version doesn't support nested builders.
  130. return descriptor->file()->options().optimize_for() !=
  131. FileOptions::LITE_RUNTIME;
  132. }
  133. // Should we generate generic services for this file?
  134. inline bool HasGenericServices(const FileDescriptor *file) {
  135. return file->service_count() > 0 &&
  136. file->options().optimize_for() != FileOptions::LITE_RUNTIME &&
  137. file->options().java_generic_services();
  138. }
  139. // Methods for shared bitfields.
  140. // Gets the name of the shared bitfield for the given index.
  141. string GetBitFieldName(int index);
  142. // Gets the name of the shared bitfield for the given bit index.
  143. // Effectively, GetBitFieldName(bitIndex / 32)
  144. string GetBitFieldNameForBit(int bitIndex);
  145. // Generates the java code for the expression that returns the boolean value
  146. // of the bit of the shared bitfields for the given bit index.
  147. // Example: "((bitField1_ & 0x04) == 0x04)"
  148. string GenerateGetBit(int bitIndex);
  149. // Generates the java code for the expression that sets the bit of the shared
  150. // bitfields for the given bit index.
  151. // Example: "bitField1_ = (bitField1_ | 0x04)"
  152. string GenerateSetBit(int bitIndex);
  153. // Generates the java code for the expression that clears the bit of the shared
  154. // bitfields for the given bit index.
  155. // Example: "bitField1_ = (bitField1_ & ~0x04)"
  156. string GenerateClearBit(int bitIndex);
  157. // Does the same as GenerateGetBit but operates on the bit field on a local
  158. // variable. This is used by the builder to copy the value in the builder to
  159. // the message.
  160. // Example: "((from_bitField1_ & 0x04) == 0x04)"
  161. string GenerateGetBitFromLocal(int bitIndex);
  162. // Does the same as GenerateSetBit but operates on the bit field on a local
  163. // variable. This is used by the builder to copy the value in the builder to
  164. // the message.
  165. // Example: "to_bitField1_ = (to_bitField1_ | 0x04)"
  166. string GenerateSetBitToLocal(int bitIndex);
  167. // Does the same as GenerateGetBit but operates on the bit field on a local
  168. // variable. This is used by the parsing constructor to record if a repeated
  169. // field is mutable.
  170. // Example: "((mutable_bitField1_ & 0x04) == 0x04)"
  171. string GenerateGetBitMutableLocal(int bitIndex);
  172. // Does the same as GenerateSetBit but operates on the bit field on a local
  173. // variable. This is used by the parsing constructor to record if a repeated
  174. // field is mutable.
  175. // Example: "mutable_bitField1_ = (mutable_bitField1_ | 0x04)"
  176. string GenerateSetBitMutableLocal(int bitIndex);
  177. } // namespace java
  178. } // namespace compiler
  179. } // namespace protobuf
  180. } // namespace google
  181. #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__