temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3 | // http://code.google.com/p/protobuf/ |
| 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 30 | |
| 31 | // Author: kenton@google.com (Kenton Varda) |
| 32 | // Based on original Protocol Buffers design by |
| 33 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | |
| 35 | #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ |
| 36 | #define GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ |
| 37 | |
| 38 | #include <string> |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 39 | #include <google/protobuf/descriptor.pb.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 40 | #include <google/protobuf/descriptor.h> |
| 41 | |
| 42 | namespace google { |
| 43 | namespace protobuf { |
| 44 | namespace compiler { |
| 45 | namespace java { |
| 46 | |
| 47 | // Commonly-used separator comments. Thick is a line of '=', thin is a line |
| 48 | // of '-'. |
| 49 | extern const char kThickSeparator[]; |
| 50 | extern const char kThinSeparator[]; |
| 51 | |
| 52 | // Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes |
| 53 | // "fooBarBaz" or "FooBarBaz", respectively. |
| 54 | string UnderscoresToCamelCase(const FieldDescriptor* field); |
| 55 | string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field); |
| 56 | |
| 57 | // Similar, but for method names. (Typically, this merely has the effect |
| 58 | // of lower-casing the first letter of the name.) |
| 59 | string UnderscoresToCamelCase(const MethodDescriptor* method); |
| 60 | |
| 61 | // Strips ".proto" or ".protodevel" from the end of a filename. |
| 62 | string StripProto(const string& filename); |
| 63 | |
| 64 | // Gets the unqualified class name for the file. Each .proto file becomes a |
| 65 | // single Java class, with all its contents nested in that class. |
| 66 | string FileClassName(const FileDescriptor* file); |
| 67 | |
| 68 | // Returns the file's Java package name. |
| 69 | string FileJavaPackage(const FileDescriptor* file); |
| 70 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 71 | // Returns output directory for the given package name. |
| 72 | string JavaPackageToDir(string package_name); |
| 73 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 74 | // Converts the given fully-qualified name in the proto namespace to its |
| 75 | // fully-qualified name in the Java namespace, given that it is in the given |
| 76 | // file. |
| 77 | string ToJavaName(const string& full_name, const FileDescriptor* file); |
| 78 | |
| 79 | // These return the fully-qualified class name corresponding to the given |
| 80 | // descriptor. |
| 81 | inline string ClassName(const Descriptor* descriptor) { |
| 82 | return ToJavaName(descriptor->full_name(), descriptor->file()); |
| 83 | } |
| 84 | inline string ClassName(const EnumDescriptor* descriptor) { |
| 85 | return ToJavaName(descriptor->full_name(), descriptor->file()); |
| 86 | } |
| 87 | inline string ClassName(const ServiceDescriptor* descriptor) { |
| 88 | return ToJavaName(descriptor->full_name(), descriptor->file()); |
| 89 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 90 | inline string ExtensionIdentifierName(const FieldDescriptor* descriptor) { |
| 91 | return ToJavaName(descriptor->full_name(), descriptor->file()); |
| 92 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 93 | string ClassName(const FileDescriptor* descriptor); |
| 94 | |
kenton@google.com | cfa2d8a | 2009-04-18 00:02:12 +0000 | [diff] [blame] | 95 | // Get the unqualified name that should be used for a field's field |
| 96 | // number constant. |
| 97 | string FieldConstantName(const FieldDescriptor *field); |
| 98 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 99 | // Returns the type of the FieldDescriptor. |
| 100 | // This does nothing interesting for the open source release, but is used for |
| 101 | // hacks that improve compatability with version 1 protocol buffers at Google. |
| 102 | FieldDescriptor::Type GetType(const FieldDescriptor* field); |
| 103 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 104 | enum JavaType { |
| 105 | JAVATYPE_INT, |
| 106 | JAVATYPE_LONG, |
| 107 | JAVATYPE_FLOAT, |
| 108 | JAVATYPE_DOUBLE, |
| 109 | JAVATYPE_BOOLEAN, |
| 110 | JAVATYPE_STRING, |
| 111 | JAVATYPE_BYTES, |
| 112 | JAVATYPE_ENUM, |
| 113 | JAVATYPE_MESSAGE |
| 114 | }; |
| 115 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 116 | JavaType GetJavaType(const FieldDescriptor* field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 117 | |
| 118 | // Get the fully-qualified class name for a boxed primitive type, e.g. |
| 119 | // "java.lang.Integer" for JAVATYPE_INT. Returns NULL for enum and message |
| 120 | // types. |
| 121 | const char* BoxedPrimitiveTypeName(JavaType type); |
| 122 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 123 | string DefaultValue(const FieldDescriptor* field); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 124 | bool IsDefaultValueJavaDefault(const FieldDescriptor* field); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 125 | |
| 126 | // Does this message class keep track of unknown fields? |
| 127 | inline bool HasUnknownFields(const Descriptor* descriptor) { |
| 128 | return descriptor->file()->options().optimize_for() != |
| 129 | FileOptions::LITE_RUNTIME; |
| 130 | } |
| 131 | |
| 132 | // Does this message class have generated parsing, serialization, and other |
| 133 | // standard methods for which reflection-based fallback implementations exist? |
| 134 | inline bool HasGeneratedMethods(const Descriptor* descriptor) { |
| 135 | return descriptor->file()->options().optimize_for() != |
| 136 | FileOptions::CODE_SIZE; |
| 137 | } |
| 138 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 139 | // Does this message have specialized equals() and hashCode() methods? |
| 140 | inline bool HasEqualsAndHashCode(const Descriptor* descriptor) { |
| 141 | return descriptor->file()->options().java_generate_equals_and_hash(); |
| 142 | } |
| 143 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 144 | // Does this message class have descriptor and reflection methods? |
| 145 | inline bool HasDescriptorMethods(const Descriptor* descriptor) { |
| 146 | return descriptor->file()->options().optimize_for() != |
| 147 | FileOptions::LITE_RUNTIME; |
| 148 | } |
| 149 | inline bool HasDescriptorMethods(const EnumDescriptor* descriptor) { |
| 150 | return descriptor->file()->options().optimize_for() != |
| 151 | FileOptions::LITE_RUNTIME; |
| 152 | } |
| 153 | inline bool HasDescriptorMethods(const FileDescriptor* descriptor) { |
| 154 | return descriptor->options().optimize_for() != |
| 155 | FileOptions::LITE_RUNTIME; |
| 156 | } |
| 157 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 158 | inline bool HasNestedBuilders(const Descriptor* descriptor) { |
| 159 | // The proto-lite version doesn't support nested builders. |
| 160 | return descriptor->file()->options().optimize_for() != |
| 161 | FileOptions::LITE_RUNTIME; |
| 162 | } |
| 163 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 164 | // Should we generate generic services for this file? |
| 165 | inline bool HasGenericServices(const FileDescriptor *file) { |
| 166 | return file->service_count() > 0 && |
| 167 | file->options().optimize_for() != FileOptions::LITE_RUNTIME && |
| 168 | file->options().java_generic_services(); |
| 169 | } |
| 170 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 171 | |
| 172 | // Methods for shared bitfields. |
| 173 | |
| 174 | // Gets the name of the shared bitfield for the given index. |
| 175 | string GetBitFieldName(int index); |
| 176 | |
| 177 | // Gets the name of the shared bitfield for the given bit index. |
| 178 | // Effectively, GetBitFieldName(bitIndex / 32) |
| 179 | string GetBitFieldNameForBit(int bitIndex); |
| 180 | |
| 181 | // Generates the java code for the expression that returns the boolean value |
| 182 | // of the bit of the shared bitfields for the given bit index. |
| 183 | // Example: "((bitField1_ & 0x04) == 0x04)" |
| 184 | string GenerateGetBit(int bitIndex); |
| 185 | |
| 186 | // Generates the java code for the expression that sets the bit of the shared |
| 187 | // bitfields for the given bit index. |
| 188 | // Example: "bitField1_ = (bitField1_ | 0x04)" |
| 189 | string GenerateSetBit(int bitIndex); |
| 190 | |
| 191 | // Generates the java code for the expression that clears the bit of the shared |
| 192 | // bitfields for the given bit index. |
| 193 | // Example: "bitField1_ = (bitField1_ & ~0x04)" |
| 194 | string GenerateClearBit(int bitIndex); |
| 195 | |
| 196 | // Does the same as GenerateGetBit but operates on the bit field on a local |
| 197 | // variable. This is used by the builder to copy the value in the builder to |
| 198 | // the message. |
| 199 | // Example: "((from_bitField1_ & 0x04) == 0x04)" |
| 200 | string GenerateGetBitFromLocal(int bitIndex); |
| 201 | |
| 202 | // Does the same as GenerateSetBit but operates on the bit field on a local |
| 203 | // variable. This is used by the builder to copy the value in the builder to |
| 204 | // the message. |
| 205 | // Example: "to_bitField1_ = (to_bitField1_ | 0x04)" |
| 206 | string GenerateSetBitToLocal(int bitIndex); |
| 207 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 208 | } // namespace java |
| 209 | } // namespace compiler |
| 210 | } // namespace protobuf |
| 211 | |
| 212 | } // namespace google |
| 213 | #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ |